Recently, a NetSuite script I was working on involved rolling down the default address from a customer record to all associated contacts when the default address was changed or edited. At first, I thought it best to run the script as a field changed event. However, I quickly realized that if changes were made but the user didn’t submit them, I wouldn’t want those changes rolled down. Thus, in changing the script from a Client Side field changed event to a User Event After Submit event, I needed to add a simple function to my script which would compare the old record’s field value with the new record’s in my new After Submit script. Below is the code snippet for this example. Note that the fieldWasChanged function takes field as a parameter; when you call the script in your afterSubmit function, make sure that the internal ID of the field you want it to look at is correct and wrapped in quotes.

function afterSubmit() {

       if (fieldWasChanged(‘defaultaddress’) == true) {

              nlapiLogExecution(‘debug’,‘default address was changed’,);

       };

};

function fieldWasChanged(field)

       var rcdOld = nlapiGetOldRecord(); // loading the old record 

       var record = nlapiLoadRecord(nlapiGetRecordType(),nlapiGetRecordId()); // loading the new record

       if(rcdOld.getFieldValue(field) != record.getFieldValue(field)) { // compare field values 

              return true;

    } else {

              return false;

    }; 

};

For this example, if the default address on the customer record was changed, my afterSubmit script will note this. After I uploaded this script, I deployed it to the customer record, and performed a quick test, changing the customer’s default address and submitting the customer. Below is the execution log for the script after submitting:

NetSuite Execution Log

As you can see, the script correctly logged that the default address was changed. Overall, this simple function can allow you to set conditions for your script’s behavior based on whether or not a field was changed. This can be used to set whether the script should run, log any changes made to sensitive records, and many other applications.

As always, let us know what ways YOU would like to extend NetSuite and how MIBAR can help. Contact us today.