Need a script to check  If incident is marked as resolved then if resolved all the incident task

jayaprakash1998
Tera Contributor

Need a script to check 

If incident is marked as resolved

then

if resolved

all the incident task should be marked as closed complete

 

 

Cheers,

Jayaprakash

2 ACCEPTED SOLUTIONS

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

    // Check if the incident has been moved to "Resolved" state
    if (current.incident_state == '6') { // 6 represents "Resolved" state in ServiceNow

        // Query all incident tasks related to this incident
        var taskGr = new GlideRecord('incident_task');
        taskGr.addQuery('incident', current.sys_id);
        taskGr.query();

        // Loop through incident tasks and close them
        while (taskGr.next()) {
            taskGr.setValue('incident_state', '7'); // 7 represents "Closed" state in ServiceNow
            taskGr.setValue('close_code', 'Solved'); // You can set an appropriate close code
            taskGr.update();
        }
    }

})(current, previous);

try the above sample code and adjust according to your requirement.

View solution in original post

Rajesh_Bhise
Tera Guru

Hello @jayaprakash1998 -  

 

Please find below script that you can use in include function.

 

CloseTasks:Function(incRC){
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',incRC);
inc.addQuery('state', '6'); //6 represents "Resolved" state of incident
inc.query()
if(inc.next()){
var inctask = new GlideRecord('incident_task');
inctask.addQuery('incident', inc);
inctask.query();
while(inctask.next()){
inctask.setValue('state', '7');//7 represents "closed complete" state of incident task
inctask.setValue('close_notes', 'solved');
inctask.update();
}
}
}

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Thank You,

Rajesh

 

View solution in original post

6 REPLIES 6

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

    // Check if the incident has been moved to "Resolved" state
    if (current.incident_state == '6') { // 6 represents "Resolved" state in ServiceNow

        // Query all incident tasks related to this incident
        var taskGr = new GlideRecord('incident_task');
        taskGr.addQuery('incident', current.sys_id);
        taskGr.query();

        // Loop through incident tasks and close them
        while (taskGr.next()) {
            taskGr.setValue('incident_state', '7'); // 7 represents "Closed" state in ServiceNow
            taskGr.setValue('close_code', 'Solved'); // You can set an appropriate close code
            taskGr.update();
        }
    }

})(current, previous);

try the above sample code and adjust according to your requirement.

Hi @varaprasad123 

 

Thankyou, I am grateful for your help.

 

Cheers,

Jayaprakash

Rajesh_Bhise
Tera Guru

Hello @jayaprakash1998 -  

 

Please find below script that you can use in include function.

 

CloseTasks:Function(incRC){
var inc = new GlideRecord('incident');
inc.addQuery('sys_id',incRC);
inc.addQuery('state', '6'); //6 represents "Resolved" state of incident
inc.query()
if(inc.next()){
var inctask = new GlideRecord('incident_task');
inctask.addQuery('incident', inc);
inctask.query();
while(inctask.next()){
inctask.setValue('state', '7');//7 represents "closed complete" state of incident task
inctask.setValue('close_notes', 'solved');
inctask.update();
}
}
}

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Thank You,

Rajesh

 

Hi @Rajesh_Bhise 

 

Thankyou, I am grateful for your help.

 

Cheers,

Jayaprakash