How to auto close changes older than 30 days

Carol2
Tera Contributor

Hi, please assist with the below script. 

I tried the below code it closes changes that are new. I want to close changes that are in 'scheduled' ,'implement' and 'review' state where the planned implementation date is older than 30 days and the related
Task should be closed as well.

var gr = new GlideRecord('change_request');
gr.addQuery('u_change_phase', 3);
gr.addQuery('active', true);
gr.addQuery('opened_at', '>', gs.daysAgoStart(30));
gr.query();
while (gr.next()) {
gr.u_change_phase = 4;
gr.change_state = 5;
gr.active = false;
gr.update();
}

6 REPLIES 6

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What exactly do you mean by changes that are new? Does that has to do with:

u_change_phase? Is 3 = new?

Question:
Are you also using Change Tasks? What should happen to those? You are now only handling the Change Request.

Here's also a nice article. If you can make your selection of which Change to close on a list, then you could use the breadcrumb on your query:
Utilizing the breadcrumb on lists to generate your query

Then simply paste that breadcrumb within here:

var gr = new GlideRecord('change_request');
gr.addEncodedQuery('encoded_query_from_breadcrumb_goes_here');

gr.setValue('u_change_phase', 4);
gr.setValue('change_state', 5);
gr.updateMultiple();

I left active=false out, because this should be handled by you state change. If not, you need to investigate why that's not working.

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

Mark Roethof
Tera Patron
Tera Patron

Can you also verify change_state. Is this the correct field? Out-of-the-box, this doesn't excist.

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

Anil Shewale
Mega Guru

hi

you have to use the Scheduled job  Script for that

glide change request

fetch the record only having state implement,scheduled and review using addquery

and close the state// the state=resolve;//closed

for finding the duration of created change use gs.diff method

 

excution time :monthly

 

If it help mark helpful or correct 

Thanks and regards

Anil

hi i'm new to scripting how do i go about doing this?