Does anyone know how to get the previous (old) val...
# suitescript
j
Does anyone know how to get the previous (old) value on a field change in a client script?
s
I don't beleive you can
t
You pretty much have to use a global variable for any "old" values you want to track. Set their initial value in the pageInit(), compare value and/or update on fieldChanged()
j
Gotcha, thanks I'll try that
t
if you need to do it for multiple fields, your best bet is to use an object to hold them all. That will keep the code cleaner, make updates easier, and make future maintenance easier.
e
You may want to also consider the user changing a field more than once. You could update the global variable (/object) on field change so the global variable stays “up to date” if the user changes the field more than once
t
var OLDVALS = {
    
entity: '',
    
trandate: '',
    
status: '',
    
location: ''
};
/**
* @param {ClientScriptContext.pageInit} context
*/
function pageInit(context) {
    
var rec = context.currentRecord;
    
Object.keys(OLDVALS).forEach(function (fieldId) {
        
OLDVALS[fieldId] = rec.getValue(fieldId);
    
});
}
/**
* @param {ClientScriptContext.fieldChanged} context
*/
function fieldChanged(context) {
    
var fieldId = context.fieldId;
    
var rec = context.currentRecord;
    
if (Object.keys(OLDVALS).indexOf(fieldId) > -1 && rec.getValue(fieldId) !== OLDVALS[fieldId]) {
        
// Do stuff
    
}
}
And like Eric said, update the value in the global in the fieldChanged() as well so it works when being changed multiple times
j
Thanks everyone, trying some of this out
238 Views