RITM should mark as close once sc_tasks are closed

Rohith9676
Tera Contributor

Hi All,

 

I have created a catalog item and 5 sc_tasks will be created to different teams. once the sc_tasks are closed. The RITM should close automatically.

 

I tried the below business rules it's not working. Can someone please check and guide me.

 

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

// Add your code here
alert('REQUEST ITEM'+current.request_item);
var sct = new GlideRecord('sc_task');
sct.addQuery('request_item', current.request_item);
sct.query();
var totalCount = sct.getRowCount();

var sct1 = new GlideRecord('sc_task');
sct1.addQuery('state', IN, '3,4,7');
sct1.addQuery('request_item', current.request_item);
sct1.query();
var completedtotalcount = sct1.getRowCount();

if (totalCount == completedtotalcount) {
var ritm = current.request_item.getRefRecord();
ritm.state = 3;
ritm.update();
}

})(current, previous);
3 REPLIES 3

Maddysunil
Kilo Sage

@Rohith9676 

I think the 'IN' operator should be used within quotes.

 

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

    var requestItem = current.request_item;
    
    // Check if the request item exists
    if (!requestItem.nil()) {
        
        var scTaskCount = new GlideAggregate('sc_task');
        scTaskCount.addQuery('request_item', requestItem);
        scTaskCount.addEncodedQuery('stateIN3,4,7'); // Use of 'IN' operator within quotes
        scTaskCount.query();
        scTaskCount.addAggregate('COUNT');
        scTaskCount.next();
        var completedTaskCount = parseInt(scTaskCount.getAggregate('COUNT'));

        var totalTaskCount = new GlideAggregate('sc_task');
        totalTaskCount.addQuery('request_item', requestItem);
        totalTaskCount.query();
        totalTaskCount.addAggregate('COUNT');
        totalTaskCount.next();
        var totalTasks = parseInt(totalTaskCount.getAggregate('COUNT'));

        // Check if all SC tasks are completed
        if (totalTasks == completedTaskCount) {
            var ritm = new GlideRecord('sc_req_item');
            if (ritm.get(requestItem)) {
                // Update RITM state to closed
                ritm.state = 3; // Assuming '3' corresponds to a closed state
                ritm.update();
            }
        }
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Prince Arora
Tera Sage
Tera Sage

@Rohith9676 

 

You can refer this link:

 

https://www.servicenow.com/community/csm-forum/need-to-close-ritm-after-all-sc-tasks-are-completed/m...

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Community Alums
Not applicable

Hi @Rohith9676 ,

I tried your problem in my PDI and it's working for me. Please check the below code 

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

    // Add your code here
	gs.log('REQUEST ITEM' + current.request_item);
    var sct = new GlideRecord('sc_task');
    sct.addQuery('request_item', current.request_item);
    sct.query();
    var totalCount = sct.getRowCount();

    var sct1 = new GlideRecord('sc_task');
    sct1.addQuery('state', IN, '3,4,7');
    sct1.addQuery('request_item', current.request_item);
    sct1.query();
    var completedtotalcount = sct1.getRowCount();

    if (totalCount == completedtotalcount) {
        var ritm = current.request_item.getRefRecord();
        ritm.state = 3;
        ritm.update();
    }

})(current, previous);

 

Result - 

SarthakKashya2_0-1713185592661.png

SarthakKashya2_1-1713185616140.png

 

RITM got closed Completed when SC Task got closed completed 

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak