Updating Ship to address value using Suitescript

/**

 * @NApiVersion 2.1

 * @NScriptType UserEventScript

 */

/********************************************************************

 *

 * All Craft Trade Solutions

 * ACTA-101 : Customization to separate Shipping Address fields

 *

 * ******************************************************************

 * Author: Jobin & Jismi IT Services LLP

 *

 * Date: 12 November 2024

 *

 * Description:  Customize Ship to field

 *

 * Revision History

 * @version 1.0 initial build :Customize ship to field without addressee and phone number- JJ0304

 *

 *

 ********************************************************************/

define([‘N/record’, ‘N/search’], function (record, search) {

    function afterSubmit(context) {

        try{

        if (context.type === context.UserEventType.CREATE  || context.type === context.UserEventType.EDIT) {

            var salesOrderId = context.newRecord.id;

            log.debug(“salesOrderId”, salesOrderId);

            // Create a sales order search with consolidation type and specified filters

           var salesOrderSearchObj = search.create({

                type: “salesorder”,              

                filters: [

                    [“type”, “anyof”, “SalesOrd”],

                    “AND”,

                    [“internalidnumber”, “equalto”, salesOrderId],

                    “AND”,

                    [“mainline”, “is”, “T”]

                ],

                columns: [

                    search.createColumn({ name: “shipaddress1”, label: “Shipping Address 1” }),

                    search.createColumn({ name: “shipaddress2”, label: “Shipping Address 2” }),

                    search.createColumn({ name: “shipcity”, label: “Shipping City” }),

                    search.createColumn({ name: “shipcountry”, label: “Shipping Country” }),

                    search.createColumn({ name: “shipzip”, label: “Shipping Zip” }),

                    search.createColumn({ name: “shipstate”, label: “Shipping State/Province” })

                ]

            });

            // Run the search and get the first result

            var searchResult = salesOrderSearchObj.run().getRange({ start: 0, end: 1 });

            if (searchResult.length > 0) {

                // Retrieve each field value

                var shipAddr1 = searchResult[0].getValue({ name: “shipaddress1” });

                //log.debug(“shipAddr1”, shipAddr1);

                var shipAddr2 = searchResult[0].getValue({ name: “shipaddress2” });

                //log.debug(“shipAddr2”, shipAddr2);

                var shipCity = searchResult[0].getValue({ name: “shipcity” });

                //log.debug(“shipCity”, shipCity);

                var shipState = searchResult[0].getValue({ name: “shipstate” });

                //log.debug(“shipState”, shipState);

                var shipZip = searchResult[0].getValue({ name: “shipzip” });

                //log.debug(“shipZip”, shipZip);

                var shipCountry = searchResult[0].getValue({ name: “shipcountry” });

                //log.debug(“shipCountry”, shipCountry);

                // Construct formatted address without addressee

                var formattedAddress = [

                    shipAddr1,

                    shipAddr2,

                    shipCity + ‘, ‘ + shipState + ‘ ‘ + shipZip,

                    shipCountry

                ].filter(Boolean).join(n); // Filter out any empty values and join with line breaks

                // Log the formatted address for debugging

                log.debug(“Formatted Address (without addressee)”, formattedAddress);

                // Load the sales order record to update it

                var salesOrderRecord = record.load({

                    type: record.Type.SALES_ORDER,

                    id: salesOrderId

                });

                // Update the ‘shipaddress’ field with the formatted address

                salesOrderRecord.setValue({

                    fieldId: ‘shipaddress’,

                    value: formattedAddress

                });

                // Save the record with the updated formatted address

                salesOrderRecord.save();

            }

        }  

} catch (error) {

    log.error(‘Error occurred in afterSubmit event’, error);

}

}

    return {

        afterSubmit: afterSubmit

    };

});

Leave a comment

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