1

I have Po. I want to check if the item lines have changed on after submit

 function onAction(context) {
        var newRec = context.newRecord;
        var oldRec = context.oldRecord;


        var newCount = newRec.getLineCount({
            sublistId: 'item'
        });

        var oldCount = oldRec.getLineCount({
            sublistId: 'item'
        });

        if (newCount != oldCount) {
            log.debug('result', 'true')
        } else {
            log.debug('result', 'false')
        }


    }

    return {
        onAction: onAction
    }

I did it with count line but I want also to check if there any changes in the line and not only the number of the lines

Davidna
  • 47
  • 10

1 Answers1

1

You would need to compare each field you care about on each line. Here's an example function that returns true/false if any lines were changed by comparing line count, then item, quantity and rate for each line. You can modify it to meet your needs.

Using some() this way, the first field on the first line that is different will stop the comparison and return true.

function linesChanged(context) {
    const fields = ['item', 'quantity', 'rate'];

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

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

    for (let i = 0; i < newLineCount; i++) {
        var lineChanged = fields.some(field => {
            const oldValue = oldRecord.getSublistValue({ sublistId: 'item', fieldId: field, line: i });
            const newValue = newRecord.getSublistValue({ sublistId: 'item', fieldId: field, line: i });

            return oldValue !== newValue;
        });

        if (lineChanged) {
            return true;
        }
    }

    return false;
}
Mike Robbins
  • 3,184
  • 15
  • 20