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

Simon Christens
Kilo Sage

Or you can use the flow that creates the catalog tasks to close the RITM once the last task have been closed ?

The above script doesnt make sense to use

Hi @Simon Christens,

The above script does make sense. It works absolutely fine. Yes, you are correct it can be done using flow designer. 

 

Hi @Simon Christens,

Though the requirement can be done with the flow, I need a info message when the catalog task associated to a particular ritm is not closed. This is not possible using flow designer as flows are async. So, according to the requirement it is recommended to use a after update business rule rather than flow designer. (Using Logs can only create a record but doesn't display the message on the form level)

Juhi Poddar
Kilo Patron

Hello @shivatmika_19 

Please try this script:

(function executeRule(current, previous /*null when async*/) {
    // Fetch the associated Requested Item (RITM)
    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get(current.request_item)) {
        // Check if any open tasks remain for the RITM
        var tasks = new GlideRecord('sc_task');
        tasks.addQuery('request_item', current.request_item); // Match the RITM
        tasks.addQuery('state', '!=', 3); // Replace '3' with the state value for Closed Complete
        tasks.query();

        if (!tasks.hasNext()) {
            // No open tasks remain, close the RITM
            ritm.state = 3; // Replace '3' with the appropriate Closed Complete state value
            ritm.update();
        }
    }
})(current, previous);

"If you found my answer helpful, please give it a like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar