Script to Change change request is not Working

rahuls1
Tera Contributor

Hi Experts,

 

I need your help. I have a requirement to cancel change requests Type=Standard, State=Draft and Active=True.

Then update close notes and Implementation status.

(There are 480 change records)

My Script:

var gr = new GlideRecord("change_request");
gr.addQuery('type=Standard^state=-22^active=true');
gr.query();
while(gr.next()){
gr.state = '4';
gr.u_implementation_status = 'cancelled';
gr.setValue( 'close_notes' , 'Cancelling change due to aging');
gr.update();
}

When I run the above background script only 1 change records is cancelled out of 480 records. If my script has any errors please help me out. I have attached the pic after running background script.

1 ACCEPTED SOLUTION

I believe there is an BR on your system which is stopping it from happening

 

var cr = new GlideRecord("change_request");
cr.addEncodedQuery('type=Standard^state=-22^active=true');
cr.query();
while(cr.next()){
cr.state = '4';
cr.u_implementation_status = 'cancelled';
cr.setValue( 'close_notes' , 'Cancelling change due to aging');
cr.active=false;
cr.update();
}

If the above does not work then try this:-

 

var cr = new GlideRecord("change_request");
cr.addEncodedQuery('type=Standard^state=-22^active=true');
cr.query();
while(cr.next()){
cr.setWorkflow(false);
cr.state = '4';
cr.u_implementation_status = 'cancelled';
cr.setValue( 'close_notes' , 'Cancelling change due to aging');
cr.active=false;

var workflow = new global.Workflow();
workflow.cancel(cr);
cr.update();
}

View solution in original post

20 REPLIES 20

rahuls1
Tera Contributor

Hi @Saurav11 This one runs good. State of all records is cancelled but active still true. The work flow doesn't gets cancelled. So when editing and saving, the state changes back to Draft.

I believe there is an BR on your system which is stopping it from happening

 

var cr = new GlideRecord("change_request");
cr.addEncodedQuery('type=Standard^state=-22^active=true');
cr.query();
while(cr.next()){
cr.state = '4';
cr.u_implementation_status = 'cancelled';
cr.setValue( 'close_notes' , 'Cancelling change due to aging');
cr.active=false;
cr.update();
}

If the above does not work then try this:-

 

var cr = new GlideRecord("change_request");
cr.addEncodedQuery('type=Standard^state=-22^active=true');
cr.query();
while(cr.next()){
cr.setWorkflow(false);
cr.state = '4';
cr.u_implementation_status = 'cancelled';
cr.setValue( 'close_notes' , 'Cancelling change due to aging');
cr.active=false;

var workflow = new global.Workflow();
workflow.cancel(cr);
cr.update();
}

Ankita19
Tera Guru
Tera Guru

 

Hi,

Instaed of addquery try to use addEncodedquery 

rahuls1
Tera Contributor

Hi Ankita19

 

Thanks for your response. But unfortunately it didn't work. Still only 1 records gets updated out of 480. I am not sure why.

Sagar Pagar
Tera Patron

Hi @rahuls1,

 

Could you please try to print the no of change request count before while loop?

 

var gr = new GlideRecord("change_request");
gr.addQuery('type=Standard^state=-22^active=true');
gr.query();

gs.info("count: " + gr.getRowCount());

 

if it prints only 1 then you have to correct your filter-query.

 

Thanks,
Sagar Pagar

The world works with ServiceNow