Assistance Needed: Business Rule to Prevent Catalog Task Closure Before Approval

Keang
Tera Contributor

Hello,

I need assistance with a business rule that prevents a catalog task from closing until all approvers have approved the request item.

The following code is not functioning correctly. Please review and provide suggestions.

Keang_0-1719552278196.png

 

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Query all related approval records
    count = 0;
    var approvalGR = new GlideRecord('sysapproval_approver');
    approvalGR.addQuery('sysapproval', current.sys_id);
    approvalGR.query();
   
    while (approvalGR.next()) {
        if (approvalGR.state != 'approved') {
            count++;
        }
    }
    if (count > 0) {
        gs.addErrorMessage('The task cannot be closed because approval is still pending.');
        current.setAbortAction(true);
    }


})(current, previous);
1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

Make sure the business rule runs on the catalog task table and is a before business rule.

 

Another correction you need to make to the script is replace current.sys_id with current.request_item

approvalGR.addQuery('sysapproval', current.request_item);

 


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

Make sure the business rule runs on the catalog task table and is a before business rule.

 

Another correction you need to make to the script is replace current.sys_id with current.request_item

approvalGR.addQuery('sysapproval', current.request_item);

 


Please mark this response as correct or helpful if it assisted you with your question.

Hi @SanjivMeher 

I have tried your code and it works. I also tried this code and it works as well. Are both codes works the same? Thank you

 

approvalGR.addQuery('sysapproval', current.request_item.sys_id);


I would like to trigger on state is "Closed Complete" only. Is the screenshot looks correct?

Keang_0-1719559071544.png

 

Yeah... Both will return the sys_id of the requested item.

I would change the condition to 

State - Changes To - Closed Complete


Please mark this response as correct or helpful if it assisted you with your question.