netsuite

NetSuite Workflow Action Script to Set Address Sublist Fields on Sales Order Based on Custom Record Type Values


As the title suggests, I'm trying to create a workflow action script to set the address on the Sales order through a custom record type on create.

So essentially, I have the following pieces in place:

  1. Custom body field on the SO with a warehouse code (if this field is not empty, it triggers the workflow action to run on order create)
  2. Custom record type that has various address values I am trying to set on the sublist of the SO

Essentially, I have a search that runs and pulls the values from the custom record type, gets the subrecord values for the address and then tries to set them and commit the line. The script runs all the way through until the line commit where I get the following error: "The subrecord line has already been committed or cancelled. The previous subrecord reference is no longer valid. You must get another reference to the subrecord in order to perform this operation."

I have the following code:

/**
 * @NApiVersion 2.x
 * @NScriptType WorkflowActionScript
 */
define(['N/record', 'N/search'], function(record, search) {

    function onAction(context) {
        var salesOrder = context.newRecord;
        isDynamic: true

        log.debug({
            title: 'Start Script'
        });

        // Get the value of the custom field on the Sales Order
        var warehouseCode = salesOrder.getValue({ fieldId: 'custbody_nsc_amz_warehouse_cod' });

        log.debug({
            title: 'Custom Field Value',
            details: warehouseCode
        });

        if (warehouseCode) {
            // Search for the custom record with the matching name
            var warehouseSearch = search.create({
                type: 'customrecord_amazon_warehouse_codes',
                filters: [
                    ['name', 'is', warehouseCode]
                ],
                columns: [
                    'custrecord_amz_addr1',
                    'custrecord_amz_addr2',
                    'custrecord_amz_city',
                    'custrecord_amz_state',
                    'custrecord_amz_zip',
                    'custrecord_amz_country'
                ]
            });

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

            if (searchResult && searchResult.length > 0) {
                var result = searchResult[0];

                log.debug({
                    title: 'Search Results Found',
                    details: result
                });

                // Set the shipping address fields on the Sales Order
                var shipaddr = salesOrder.getSubrecord({fieldId: 'shippingaddress'});

                log.debug({
                    title: 'Shipping Address',
                    details: shipaddr
                });

                 salesOrder.setValue({
            fieldId: 'shipaddresslist',
            value: null // To override default address
            });
              
               log.debug({
                    title: 'Unset Shipping Address List',
                    details: 'Address should be unset'
                });

                // Set country field first when using dynamic mode
                shipaddr.setValue({
                    fieldId: 'country',
                    value: result.getValue('custrecord_amz_country') || 'US'  // Default to 'US' if not available
                });

                shipaddr.setValue({
                    fieldId: 'addr1',
                    value: result.getValue('custrecord_amz_addr1')
                });

                shipaddr.setValue({
                    fieldId: 'addr2',
                    value: result.getValue('custrecord_amz_addr2')
                });

                shipaddr.setValue({
                    fieldId: 'city',
                    value: result.getValue('custrecord_amz_city')
                });

                shipaddr.setValue({
                    fieldId: 'state',
                    value: result.getValue('custrecord_amz_state')
                });

                shipaddr.setValue({
                    fieldId: 'zip',
                    value: result.getValue('custrecord_amz_zip')
                });

                log.debug({
                    title: 'Address Line Line Fields Complete',
                    details: 'Address lines have been set'
                });

                salesOrder.commitLine({ sublistId: 'shippingaddress' });

                log.debug({
                    title: 'Shipping Address Set Successfully'
                });
            } else {
                log.debug({
                    title: 'No Matching Warehouse Record Found'
                });
            }
        } else {
            log.debug({
                title: 'No Warehouse Code Specified'
            });
        }
    }

    return {
        onAction: onAction
    };
});

Any help on this would be appreciated.


Solution

  • You do not need the commit and this was removed in SS2.

    Given the order of your code you may be are updating the wrong address or at least throwing away your edits. Change to this:

    
    salesOrder.setValue({
       fieldId: 'shipaddresslist',
       value: null // To override default address
    });
    
    // Set the shipping address fields on the Sales Order
    var shipaddr = salesOrder.getSubrecord({fieldId: 'shippingaddress'});
    
    

    Note In fact you may not need to clear the shipaddresslist as I believe the very process of updating the address subrecord now creates a custom address on the transaction. I know I've had to remove the clearing process from a couple of live scripts.