Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

In your script under if statement change the variable name of updation

inc.update() to Inc_Gr.update();

Also remove the else part it is not needed

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

It's my fault. After updating its working fine.