- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2024 10:25 PM
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.
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);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2024 10:59 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2024 10:59 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 12:18 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 07:29 AM
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.