Updating Object Values Without Changing the Original in NetSuite JavaScript

In JavaScript, when you assign an object to a new variable, it creates a reference, not a copy. Any changes made to the new variable will also affect the original object. To avoid this, use the spread operator { ...object } to create a separate copy.

For example:

let fields = { custrecord_project_status: 23, charityStatus: 5 };

let newProjectStatus = { …fields };

if (currentProjectStatus === 6) {

  newProjectStatus.custrecord_project_status = 6;

}

Now, newProjectStatus updates correctly while itfields remains unchanged. This approach prevents unintended modifications and ensures data integrity in NetSuite scripts.

So after assigning and updating the values:

fields = {

custrecord_project_status: 23,

charityStatus: 5

};

newProjectStatus = {

custrecord_project_status: 23,

charityStatus: 5

};

Leave a comment

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