Updating Shipping Address Fields in Bulk

The phone number is not displayed in the SHIP TO address. But if we open the address sub-record it is there in the address fields. Unmark the Override Checkbox and hit save it sets the address phone number in the SHIP TO address.

Instead of reselecting every address in each transaction, we are doing the custom mass update.

/**
 * @NApiVersion 2.x
 * @NScriptType MassUpdateScript
 * @NModuleScope SameAccount
 /***********************************************************************************
 * MAR-889 - Script updates all the Address fields inorder to recover unshown values
 * **********************************************************************************
 *
 * Created Date: 25-01-2022
 *
 * Author: Jobin & Jismi IT Services LLP
 *
 * Revision  History:
 * Revision 1.0 {25/01/2022} - Created
 *************************************************************************************
 **/
define( [ 'N/search', 'N/record', 'N/runtime' ],

    function ( search, record, runtime )
    {
        function each ( params )
        {
            try
            {
                var recId = params.id;

                var objRecord = record.load( { //load the Shipment items 
                    type: record.Type.SALES_ORDER,
                    id: recId

                } );

                //Get current Address's address field values
                var sub = objRecord.getSubrecord( {
                    fieldId: "shippingaddress"
                } );
                var overRide = sub.getValue( { fieldId: "override" } ) || ""
                var country = sub.getValue( { fieldId: "country" } ) || ""
                var attention = sub.getValue( { fieldId: "attention" } ) || ""
                var addressee = sub.getValue( { fieldId: "addressee" } ) || ""
                var addrphone = sub.getValue( { fieldId: "addrphone" } ) || ""
                var addr1 = sub.getValue( { fieldId: "addr1" } ) || ""
                var addr2 = sub.getValue( { fieldId: "addr2" } ) || ""
                var city = sub.getValue( { fieldId: "city" } ) || ""
                var state = sub.getValue( { fieldId: "state" } ) || ""
                var zip = sub.getValue( { fieldId: "zip" } ) || ""

                log.debug( "overRide", overRide )
                log.debug( "addrphone", addrphone )
               

                // Add the fetched address as new one.
                sub.setValue( {
                    fieldId: "override",
                    value: false
                } );
                sub.setValue( {
                    fieldId: "country",
                    value: country
                } );

                sub.setValue( {
                    fieldId: "attention",
                    value: attention
                } );
                sub.setValue( {
                    fieldId: "addressee",
                    value: addressee
                } );
                sub.setValue( {
                    fieldId: "addrphone",
                    value: addrphone
                } );
                sub.setValue( {
                    fieldId: "addr1",
                    value: addr1
                } );
                sub.setValue( {
                    fieldId: "addr2",
                    value: addr2
                } );
                sub.setValue( {
                    fieldId: "city",
                    value: city
                } );
                sub.setValue( {
                    fieldId: "state",
                    value: state
                } );
                sub.setValue( {
                    fieldId: "zip",
                    value: zip
                } );

                objRecord.save();

                var scriptObj = runtime.getCurrentScript();
                log.debug( 'Remaining governance units: ' + scriptObj.getRemainingUsage() );

            } catch ( e )
            {
                log.debug( 'error@params', e )
            }
        }
        return {
            each: each
        };
    } );

Leave a comment

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