Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Need help to close bulk RITMs and SC Tasks efficiently through a fix script

Sunayana4
Tera Contributor

 

Hi Team,

 

I need to close around 1000 RITMs and around 1800 Catalog Tasks in bulk.

 

Some RITMs have only one SC Task (Fulfillment, Open, or On Hold) — these should be closed as Cancelled.

Some have multiple SC Tasks with mixed states (Closed Complete, Closed Cancelled, Fulfillment, Preparation, On Hold, etc.) — these should be closed as Complete.

All SC Tasks should be closed first, then the RITMs updated with state, stage, work notes, and inactive status.

 

I know AI can help with logic, but I trust the ServiceNow Community for standard and expert-level best practices to handle such cases smoothly and efficiently.

 

Also, since this is a bulk run, please suggest whether it’s better to run the fix script in batches (for example, 100 records per run) or if there’s a safer alternative approach to handle large updates.

 

Thanks,

Sunayana

5 REPLIES 5

Sunayana4
Tera Contributor

Tagging experts for guidance:

@Ankur Bawiskar — your scripting and workflow insights are always spot-on.

 @Ravi Gaurav — would love your take on batch safety and performance.

@Mark Stanger, @Aniket Chavan — your experience with large-scale RITM and task closures could be invaluable here. @Rohit Sahu1  — any tips to proceed further would be deeply appreciated.

To all the experts across the globe and any helping hand or suggestion would mean a lot. This task is time-sensitive and your support will truly help me move forward smoothly.

 

@Sunayana4 

My Thoughts

-> since 1000 RITMs so 1 fix script is sufficient as count is not huge

-> do make sure to cancel any pending approval since you are closing the RITM, also close REQs

-> share your sample script and we can share feedback

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Thanks so much for your prompt response. I’m really glad to hear from you and appreciate your support on this scenario.

Please find the fix script I’ve drafted below.

var ritmGR = new GlideRecord("sc_req_item");
ritmGR.addEncodedQuery("numberIN<ritmlist>"); 
ritmGR.setLimit(100);
ritmGR.query();

while (ritmGR.next()) {
    var openTaskCount = 0;
    var closedTaskCount = 0;
    var totalTaskCount = 0;

    var taskGR = new GlideRecord("sc_task");
    taskGR.addQuery("request_item", ritmGR.sys_id);
    taskGR.query();

    while (taskGR.next()) {
        totalTaskCount++;
        var state = taskGR.getValue("state");
//Open States
        if (["1", "2", "8", "9", "-5", "10", "-9"].indexOf(state) > -1) {
            openTaskCount++;

            // Close the task
            taskGR.setValue("state", "4"); // Closed Cancelled
            taskGR.setValue("work_notes", "Closed old requests");
            taskGR.update();
        } else if (["3", "4"].indexOf(state) > -1) {
            closedTaskCount++;
        }
    }

    ritmGR.autoSysFields(false);
    ritmGR.setWorkflow(false);

    if (totalTaskCount == 1 && openTaskCount == 1) {
        ritmGR.setValue("state", "4"); // Cancelled
        ritmGR.setValue("stage", "Request Cancelled");
    } else {
        ritmGR.setValue("state", "3"); // Complete
        ritmGR.setValue("stage", "complete");
    }

    ritmGR.setValue("active", false);
   
    ritmGR.update();
}

This is the current version I’m working with. Could you please take a look and let me know if there are any corrections or improvements you’d recommend.

I’d really appreciate your expert guidance to make sure I’m handling this scenario correctly and safely.

Thanks again! 

@Sunayana4 

looks good to me.

Please test it.

also add logic to cancel any pending approval for that RITM and also logic to close REQ

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader