- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 09:56 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 01:58 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 11:16 PM
Hi Saurav11,
That almost worked. All 480 records were cancelled. But active flag remains true for all the records. And the weird part is when description of any cancelled record is edited and saved, Once the form reloads the state changes back to Draft.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 11:24 PM
Which one did you try setworkflow(false) and change the variable gr both?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 11:40 PM - edited 10-10-2022 11:47 PM
I tried the below script.
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.update();
}
All 480 records are cancelled. Active flag is still in true. When edited description of one cancelled change record and saved it, the state changes back to Draft only for the edited record. Please help out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2022 11:44 PM
Hello, I believe because we have set the workflow to false hence the active flag did not change.
Can you try updating the active flag also for the 480 records through background script it should do the trick.
Let me know if it worked.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 12:08 AM
Hi @Saurav11 I tried the below script.
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;
cr.update();
}
All works good now. Only one issue is the state changes back to Draft when any record is edited and saved. Why does it changes ?