SuiteScript: Trigger to Delete Sublist Line After Another is Deleted

I’m working on a Client Script for managing product UPCs. One of the pieces of this is to delete additional lines from a sublist if one with certain criteria is deleted.

Currently, I’m using validateDelete to search the remaining items in the sublist and delete any that meet the criteria. The issue is that I’m causing an infinite loop as every time I try to delete another line it kicks off validateDelete again.

 

Code:

function validateDelete(scriptContext) {
    const delRecord = scriptContext.currentRecord;
    const delSublist = scriptContext.sublistId;

    if (delSublist === primaryListID) {
        const delValue = delRecord.getCurrentSublistValue({
            sublistId: delSublist,
            fieldId: primaryFieldID
        });

        const otherExists = delRecord.findSublistLineWithValue({
            sublistId: delSublist,
            fieldId: primaryFieldID,
            value: UPCmod.checkDigit(delValue)
        });

        if (otherExists !== -1){
            delRecord.removeLine({
                sublistId: delSublist,
                line: otherExists,
                ignoreRecalc: true
            });
        }
        
        return true;
    }
}

Any ideas for me? I couldn’t puzzle through getting fieldChanged to trigger on a sublist line being deleted, so that’s why I landed on validateDelete. I had hoped ignoreRecalc would do the trick, but it didn’t seem to make any difference.

Thank you!

Rookie Asked on November 14, 2022 in SuiteScript.
Add Comment
2 Answer(s)
Best answer

You could take 2 approaches.

1. You could get the unique line ref of the lines you have already deleted and skip over them when you are deleting other lines, preventing the infinite loop.

2. The other option would be attempting to run this functionality on sublistChanged rather than validate delete, then in the context check if the line has been removed. As this triggers on sublist edit insert or removed

Beginner Answered on November 14, 2022.
Add Comment

Thanks, George! Opted for the second and it works swimmingly.

Rookie Answered on November 14, 2022.

Glad I could help 🙂

on November 14, 2022.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor   Become a Sponsor