If I call `result = record.load(...)` then `result...
# suitescript
g
If I call
result = 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?
c
``if (result.getValue() !== newValue) { result.setValue(newValue);`
changed = true;
}
`if (changed) result.save()``
g
Yea, that was my "Or do I have to..." track it myself. 😉 Darn.
c
Could be modularized where you pass a value and the field name too.
g
Yea, I'm considering that.
Copy code
/**
 * @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;
});
👏🏻 1