GrayLeopard
06/24/2021, 9:02 PMresult = record.load(...)
then result.setValue(...)
on a dozen values. Then I want to only save the record if any of the values were changed -- is there a clean way to do that? Or do I have to result.getValue(...)
on every value, and keep a boolean hasAnyChanges = false
that I set to true prior to calling setValue, and then only save if hasAnyChanges is true?Chris
06/24/2021, 9:06 PMchanged = true;
}
`if (changed) result.save()``GrayLeopard
06/24/2021, 9:07 PMChris
06/24/2021, 9:07 PMGrayLeopard
06/24/2021, 9:07 PMGrayLeopard
06/24/2021, 9:25 PM/**
* @NApiVersion 2.0
* @NModuleScope SameAccount
*/
define([], function () {
function RecordWrapper(record) {
var hasChanges = false;
this.setIfChanged = function (fieldId, value) {
var current = record.getValue({
fieldId: fieldId,
});
log.debug({
title: 'Check: ' + fieldId,
details: {
before: current,
after: value,
shouldChange: current !== value,
}
});
if (current !== value) {
hasChanges = true;
record.setValue({
fieldId: fieldId,
value: value,
});
}
};
this.saveIfChanged = function () {
if (hasChanges) {
log.debug('Found changes, saving');
return record.save();
}
log.debug('No changes, not saving');
return null;
};
}
return RecordWrapper;
});