Scheduled job to update the state of change record based on Incident Caused By Change field

Swati36
Tera Contributor

Hello!

 

We have a requirement where once a change request is closed, if we receive any incident caused by this change ( within 30 days of the change closure) and same is updated on the "Incident Caused By Change" related list of that change then the "state" of that change should be automatically updated from "Successful" to "Successful with issues".

 

I am trying to achieve this by creating a scheduled job on the incident table but I am stuck on a use case where a single change has many linked Incidents. How do I make sure that the change record that is already updated gets skipped in the next iteration?

 

Thank you!

3 REPLIES 3

Kieran Anson
Kilo Patron

An async business rule may be better placed for this rather than a scheduled job.

 

KieranAnson_0-1716205447597.png

 

and a script of

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

	var changeRec = current.getElement('caused_by').getRefRecord();
	if(!changeRec.isValidRecord())
	return;

	changeRec.setValue('close_code' , 'successful_issues');
	changeRec.update();

})(current, previous);

Community Alums
Not applicable

Hi @Swati36 ,

Please try below solution 

I created scheduled job which runs on 29 on every month you can change it as per your requirements and add below script

var gr = new GlideRecord('change_request');
gr.addQuery('active', 'true');
gr.addQuery('state', 3); //closed
gr.query();
while (gr.next()) {
    var incGr = new GlideRecord('incident');
    incGr.addQuery('caused_by', gr.sys_id);
    incGr.query();
    while (incGr.next()) {
        incGr.setValue('work_notes', 'Successful with issues');
        incGr.update();
    }
    gr.setValue('work_notes', 'Successful with issues');
    gr.update();
}

SarthakKashyap_0-1716207736155.png

 

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

Thanks and Regards

Sarthak

 

 

Deepak89
Tera Expert

Hello @Swati36 ,

 

i run below script in Background and its worked for me ,include it on scheduled script .

 

var cr=[];
var gr= new GlideRecord("change_request");
gr.addEncodedQuery('active=false^state!=4');
gr.query();
while(gr.next()){

cr.push(gr.sys_id);
gs.info("**8*"+cr);
for(var i in cr){
gs.info("line11");
var ir= new GlideRecord("incident");
ir.addQuery('caused_by',cr[i]);
gs.info("line14");
ir.query();
while(ir.next()){
gs.info("hello17"+ir.sys_id);
gs.info("change"+cr[i]);
 var chnagenew= new GlideRecord("change_request");
 if(chnagenew.get(cr[i])){
    gs.info("line21");
 chnagenew.description="Test";
 chnagenew.close_code="successful_issues";
 chnagenew.update();
 }
}

}

}
 
Mark correct if its worked for you