- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 06:21 PM
I am currently creating a Business Rule to check if a Catalog Task is 'Closed Completed', 'Closed Incompleted' or 'Closed Skipped' if the 'State' field in 'sc_req_item' table is changed during a 'Confirmation of Completion'.
The purpose of this business rule is to prevent the status of request item table from being changed while the catalog task is not closed.
I have tried the following script, but the 'State' field of 'sc_req_item' table is not automatically updated by the business rule.
(function executeRule(current, previous /*null when async*/) {
/* Check if 'State' field in Catalog Task table is
“Closed Completed”, “Closed Incompleted”, or “Closed Skipped”. */
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', current.sys_id);
taskGR.addQuery('state', '!=', '3'); //state is not Closed Completed
taskGR.addOrCondition('state', '!=', '4'); //state is not Closed Incompleted
taskGR.addOrCondition('state', '!=', '7'); //state is not Closed Skipped
taskGR.query();
/* If any Catalog Task is not “Closed Completed”, “Closed Incompleted”, or “Closed Skipped”
restore the 'state' field of 'sc_req_item' to 'Open' */
if (taskGR.next()) {
current.state = '-5'; //-5 is 'Open'
}
})(current, previous);
I would like to know if anyone can tell me what the problem is.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 06:37 PM
Hi @Yugo Sakuma,
Let's simplify the script, try the following:
var taskGr = new GlideAggregate('sc_task');
taskGr.addAggregate('COUNT');
taskGr.addQuery('request_item', current.getUniqueValue());
taskGr.addActiveQuery();
taskGr.query();
taskGr.next();
if (taskGr.getAggregate('COUNT') > 0) {
current.setAbortAction(true);
}
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 08:16 PM - edited 05-13-2024 08:18 PM
That is the expected behaviour, the Invlid Update message is from the last line of script -
current.setAbortAction(true);
The script prevents any changes to the record, so although the 'state' looks as if it has changed on the client-side, the underlying data has not been saved/updated.
If you refresh the page, you will see that the 'state' has not been updated.
You can also add a message (e.g. gs.addErrorMessage("Close all the child SCTASKs") ) within the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 06:37 PM
Hi @Yugo Sakuma,
Let's simplify the script, try the following:
var taskGr = new GlideAggregate('sc_task');
taskGr.addAggregate('COUNT');
taskGr.addQuery('request_item', current.getUniqueValue());
taskGr.addActiveQuery();
taskGr.query();
taskGr.next();
if (taskGr.getAggregate('COUNT') > 0) {
current.setAbortAction(true);
}
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 07:30 PM
Thank you for your response.
I tried changing 'state' of 'sc_req_item' table from 'Open' to 'Confirmation of Completion' while the catalog task was 'Open' and executing a business rule, but the update was not interrupted and 'state' field of the 'sc_req_item' did not return to 'Open'. I also received an Invalid Update message.
What do you think is the problem?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 08:16 PM - edited 05-13-2024 08:18 PM
That is the expected behaviour, the Invlid Update message is from the last line of script -
current.setAbortAction(true);
The script prevents any changes to the record, so although the 'state' looks as if it has changed on the client-side, the underlying data has not been saved/updated.
If you refresh the page, you will see that the 'state' has not been updated.
You can also add a message (e.g. gs.addErrorMessage("Close all the child SCTASKs") ) within the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 09:33 PM
Thank you so much!
It worked perfectly.