Close the Requested Item record when all the associated Catalog Tasks are closed

shivatmika_19
Tera Guru

Requirement: Requested Item should be closed when all the respective catalog tasks are closed.

 

1. Create a Business rule on Catalog task (sc_task) table

2. Condition: Catalog task is closed complete

3. Script:

(function executeRule(current, previous /*null when async*/) {
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.request_item)) {
var tasks = new GlideRecord('sc_task');
tasks.addQuery('request_item', current.request_item);
tasks.addQuery('state', '!=', 3); // Adjust 'closed' as per your states
tasks.query();
 
if(tasks.getRowCount() > 0)
{
gs.addErrorMessage("cannot close ritm ");
}
else{
ritm.state = '3'; // Adjust 'closed' as per RITM states
ritm.update();
}
}
})(current, previous);
4. Save the record
 
Code Explanation:
Initially, we are finding out the requested item of the updated catalog task and then finding the catalog task records with respect to the requested item and filtering only those catalog tasks which are not closed complete and counting them, If they are >0 specifying message that Requested Item cannot be closed else closing the Requested Item.
6 REPLIES 6

Hi @Juhi Poddar ,

The script I have written is almost same as your's just if condition is different. Anyway thanks for the reply

PrashantLearnIT
Giga Sage

Hi @shivatmika_19 

 

Anyway flow doesn't moves until the catalog task is closed, so create one sub-flow for closure and add it after joining all catalog tasks to this particular sub-flow. No scripting is required to fulfill this one.

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************