- 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-11-2022 12:16 AM
I believe the change workflow is not getting cancelled as we used cr.setWorkflow(false); maybe you can try to see for one of the records using the show workflow related link where the workflow shows and if it is not cancelled cancel it using the below in the while loop
var workflow = new global.Workflow();
workflow.cancel(cr);
Please mark my answer as correct based on Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 01:18 AM
Hi @Saurav11 I ran the below script. Let me know if it is correct.
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');
var workflow = new global.Workflow();
workflow.cancel(cr);
cr.update();
}
Only few changes changes are cancelled and active false, Rest of the changes are still in draft and active is true.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 01:21 AM
Hello,
the encoded query you are using is it correct? as now the changes are already in cancelled state or draft state?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 01:27 AM
Hi, Yes the encoded query is correct. When running the above script out of 480 only few records are cancelled and active set to false. Remaining records are updated too like close notes, active is false but the state alone is in draft.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 01:35 AM
Hello
This is weird
Use the below once:-
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');
var workflow = new global.Workflow();
workflow.cancel(cr);
cr.update();
}