How to compare old and new values of a field in a ...
# ask-stanley-ai
s
How to compare old and new values of a field in a client suitescript
c
AI generated answer (using internal Slack threads):
To compare old and new values of a field in a client SuiteScript, you can use the
oldRecord
and
newRecord
objects available in the script context of the event script. These objects represent the state of the record before and after the event, respectively. Here is a simple example of how you can compare old and new values:
Copy code
javascript
function afterSubmit(context) {
    var oldRecord = context.oldRecord;
    var newRecord = context.newRecord;

    var oldValue = oldRecord.getValue('fieldname');
    var newValue = newRecord.getValue('fieldname');

    if (oldValue !== newValue) {
        // The field value has changed
    }
}
In a client script, you can use a global variable to store the old value of the field. You can set the initial value in the
pageInit
function and then compare or update it in the
fieldChanged
function. Here is an example:
Copy code
javascript
var OLDVALS = {
    entity: '',
    trandate: '',
    status: '',
    location: ''
};

function pageInit(context) {
    var rec = context.currentRecord;

    Object.keys(OLDVALS).forEach(function (fieldId) {
        OLDVALS[fieldId] = rec.getValue(fieldId);
    });
}

function fieldChanged(context) {
    var fieldId = context.fieldId;
    var rec = context.currentRecord;

    if (Object.keys(OLDVALS).indexOf(fieldId) > -1 && rec.getValue(fieldId) !== OLDVALS[fieldId]) {
        // The field value has changed
    }
}
In this example,
OLDVALS
is a global object that stores the initial values of the fields. In the
pageInit
function, it sets the initial values. In the
fieldChanged
function, it checks if the field value has changed. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.84387207
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.832611084
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.830871403
Please provide your feedback on this answer.