Function to check whether any fields are changed in suitescript

Function to check if there are any change in old and new record values

/**
        * Checks if there are changes between the new and old records.
        *
        * @param {Record} newRecord - The new record
        * @param {Record} oldRecord - The old record
        * @returns {boolean} - Returns true if there are changes, otherwise false
        */
        const hasChanges = (newRecord, oldRecord) => {
            const fieldsToCheck = ['otherrefnum', 'memo', 'status', 'custbody_jj_rma_old_stchg_cdus4166'];
            for (let fieldId of fieldsToCheck) {
                if (newRecord.getValue({ fieldId }) !== oldRecord.getValue({ fieldId })) {
                    return true;
                }
            }


            const lineCount = newRecord.getLineCount({ sublistId: 'item' });
            const oldLineCount = oldRecord.getLineCount({ sublistId: 'item' });


            if (lineCount !== oldLineCount) {
                return true;
            }


            for (let i = 0; i < lineCount; i++) {
                if (newRecord.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i }) !==
                    oldRecord.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i }) ||
                    newRecord.getSublistValue({ sublistId: 'item', fieldId: 'quantity', line: i }) !==
                    oldRecord.getSublistValue({ sublistId: 'item', fieldId: 'quantity', line: i }) ||
                    newRecord.getSublistValue({ sublistId: 'item', fieldId: 'isclosed', line: i }) !==
                    oldRecord.getSublistValue({ sublistId: 'item', fieldId: 'isclosed', line: i })) {
                    return true;
                }
            }


            return false;
        };

Leave a comment

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