Auto close incident when All incident tasks are closed

Jaya Chandra Re
Tera Contributor

Hello Folks,

 

I have a requirement on incident form, i want to close incident automatically when all associated incident tasks are closed. How can we do this with scripting. i tried with After Business rule but my script not working as expected. any help would be appreciated.

1 ACCEPTED SOLUTION

Ramz
Mega Sage

HI @Jaya Chandra Re ,

Write a After Update BR on incident task table

when to run: state changes to closed complete

Script:

(function executeRule(current, previous /*null when async*/ ) {
    var inc_task = new GlideRecord('incident_task');
    inc_task.addQuery('incident', current.incident);//field where incident is captured
    inc_task.addQuery('active', true);//check if any tasks are active
    inc_task.query();
    if (inc_task.next()) {
        //do nothing
        //you can also print any info message if you want
    } else {
        var inc = new GlideRecord('incident');
        inc.addQuery('sys_id', current.incident);
        inc.query();
        if (inc.next()) {
            inc.state = 6; //close state
            inc.close_code = 'successful';
            inc.close_notes = "Auto-Closed based on Incident Tasks being Closed"; //while closing incident check if there are any mandatory fields and pass value to those so that the script works
            inc.update();
        }
    }

})(current, previous);

Please mark my answer helpful/correct if it resolved your query

Thanks!

View solution in original post

11 REPLIES 11

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

You are not sharing anything.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hello Mark,

i am new to ServiceNow Development. i am practicing in my PDI some scenarios

i have written a script for incident task creation when incident created my code is working as expected. but in the same way i am trying to set incident closed  when all incident tasks are closed. could you please share with me script how to set automatically inc state as closed

Please share what you tried and is not working.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

JayaChandraRe_0-1714232215617.png

 

i have written above script with the reference of below community article.

https://www.servicenow.com/community/itsm-forum/auto-close-incident-when-incident-tasks-are-closed/m...