Set Default Shipping Carrier in Netsuite Sales Order

Requirement:

The client requirement is to set the shipping carrier in netsuite sales order from the shopify orders ship carrier during order import from shopify. The order import is performed by an order flow implemented using Celigo. 
Since value cannot be set directly to the Shipping Carrier field in NetSuite sales order using Celigo, we will need to create a custom list field in NetSuite that resembles the standard ‘Shipping Carrier’ field and will map the shipping carrier from Shopify to this custom field using Celigo. 
The field mapping between the shipping carrier in shopify and this custom shipping carrier field in netsuite orders will be handled by the celigo.
We need to set the value in this custom field to the standard shipping carrier field.

Solution:

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/search'],
    /**
     * @param{record} record
     * @param{search} search
     */
    (record, search) => {
        'use strict';

        /**
         * @description the function to check whether a value exists in parameter
         * @param parameter -passing parameter
         * @param parameterName - passing parameter name
         * @returns{Boolean}
         */
        function checkForParameter(parameter, parameterName) {
            if (
                parameter != "" &&
                parameter != null &&
                parameter != undefined &&
                parameter != "null" &&
                parameter != "undefined" &&
                parameter != " " &&
                parameter != false
            ) {
                return true;
            } else {
                if (parameterName)
                    log.debug("Empty Value found", "Empty Value for parameter " + parameterName);
                return false;
            }
        }
        function SetDefaultShippingCarrier(shopifyShippingCarrier, recSalesOrder) {
            try {
                let shippingCarrier, shippingMethod;
                if (shopifyShippingCarrier.toUpperCase() === "UPS" )
                {
                    shippingCarrier = "UPS"
                    shippingMethod = 3026
                }
                else
                {
                    shippingCarrier = "FedEx/USPS/More"
                    shippingMethod = 2731
                }
                log.debug("shippingCarrier", shippingCarrier)
                recSalesOrder.setText({
                    fieldId: 'shipcarrier',
                    value: shippingCarrier
                });
                recSalesOrder.setValue({
                    fieldId: 'shipmethod',
                    value: shippingMethod
                });
            } catch (e) {
                log.debug("Error@ SetDefaultShippingCarrier", e.message)
            }
        }

        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {
            try
            {
                let recSalesOrder = scriptContext.newRecord;
                let shopifyShippingCarrier = recSalesOrder.getValue({
                    fieldId: 'custbody_jj_shipping_carrier_spkb_133'
                });
                log.debug("shopifyShippingCarrier", shopifyShippingCarrier)
                const shopifyOrderId = recSalesOrder.getValue({
                    fieldId: 'custbody_celigo_shopify_order_no'
                });
                //if not a celigo created order, then stop execution - A celigo created order will have value in shopify order number
                if (!checkForParameter(shopifyOrderId))
                {
                    return;
                } else if ((scriptContext.type === scriptContext.UserEventType.CREATE) && checkForParameter(shopifyShippingCarrier))
                {
                    log.debug("CREATE");
                    SetDefaultShippingCarrier(shopifyShippingCarrier, recSalesOrder);
                }
                // if copy context, the custom shipping carrier field should set empty if it has value in it
                else if (scriptContext.type == scriptContext.UserEventType.COPY)
                {
                    log.debug("COPY")
                    if (checkForParameter(shopifyShippingCarrier)) {
                        recSalesOrder.setText({
                            fieldId: 'custbody_jj_shipping_carrier_spkb_133',
                            value: null
                        });
                    }
                    return;
                }
            }
            catch (e)
            {
                log.debug("Error@afterSubmit", e.message);
            }
        }
        return {beforeSubmit}
    });

Leave a comment

Your email address will not be published. Required fields are marked *