Changing the state of the state using background script

Devika3
Tera Guru

Hi All,

I have used the below script for closing the change request associated with the ritm,

 

var gr2 = new GlideRecord('sc_request');
gr2.addActiveQuery();
gr2.addQuery('sys_id','***********');

gr2.query();

 

while(gr2.next()){
gr2.request_state ='closed_cancelled';
gr2.state =4;
gr2.stage ='closed_incomplete';
gr2.update();

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request','********');
ritm.query();
while(ritm.next()){

var chg = new GlideRecord('change_request');
chg.addQuery('parent','#######3');
chg.query();

chg.state=7;

chg.update();
while(chg.next()){


var workflow = new Workflow();


workflow.cancel(chg);

}

ritm.update();

}


}

 

The workflow is getting cancelled but the state of the change request is not getting updated.

Thanks in advance.

Regards,

Devika. 

1 ACCEPTED SOLUTION

@Devika 

Thanks for marking my response as helpful.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What are you trying to achieve here? Can you explain?

var chg = new GlideRecord('change_request');
chg.addQuery('parent','#######3');
chg.query();

chg.state=7;

chg.update();

That part looks odd to me. Not performing a chg.next for example, if(chg.next), etc..

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as below; you should put those 2 lines inside the while

I assume you are having state with value as 7 for your change_request table

var gr2 = new GlideRecord('sc_request');
gr2.addActiveQuery();
gr2.addQuery('sys_id','***********');

gr2.query();
while(gr2.next()){
gr2.request_state ='closed_cancelled';
gr2.state =4;
gr2.stage ='closed_incomplete';
gr2.update();

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request','********');
ritm.query();
while(ritm.next()){

var chg = new GlideRecord('change_request');
chg.addQuery('parent','#######3');
chg.query();
while(chg.next()){

chg.state=7;
chg.update();

var workflow = new Workflow();
workflow.cancel(chg);
}

ritm.update();

}    
}

Regards
Ankur

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

@Devika 

Thanks for marking my response as helpful.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Any input on my response?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn