Fix Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2024 03:56 AM - edited ‎02-10-2024 03:58 AM
Hi All,
I have a requirement to clean up the approval records which are older than 12 months and cancel all the related RITMs and workflow.
I have written the code as below: I have one doubt when i am using gr.update() only once at the end to update the RITM record it is not updating the RITM's work note but cancelling the stage and state of it.
But when i use gr.update() after worknotes it is updating the RITM, can someone tell why it is happening?
var gr = new GlideRecord('sysapproval_approver');
gr.addEncodedQuery('state=requested^sysapproval.sys_updated_on<=javascript:gs.beginningOfLast12Months()');
gr.query();
if(gr.next()) {
var RITMgr = new GlideRecord('sc_req_item');
RITMgr.addQuery('sys_id', gr.document_id);
RITMgr.addQuery('state', '!=', '5');
RITMgr.query();
while (RITMgr.next()) {
RITMgr.work_notes = 'Cancelled by Scheduled Job - Housekeeping Job for RI/RQ';
RITMgr.update();
RITMgr.stage='Request Cancelled';
RITMgr.state='5';
RITMgr.setWorkflow(false);
RITMgr.update();
var workflow = new global.Workflow();
workflow.cancel(RITMgr);
}
gr.state='cancelled';
gr.update();
}
Thanks,
Rooma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2024 04:33 AM
@Rooma1 , Update the Script as below
var gr = new GlideRecord('sysapproval_approver');
gr.addEncodedQuery('state=requested^sysapproval.sys_updated_on<=javascript:gs.beginningOfLast12Months()');
gr.orderBy('approver');
gr.query();
while(gr.next()) {
var RITMgr = new GlideRecord('sc_req_item');
if(RITMgr.get(gr.sysapproval.sys_id)) {
var workflow = new Workflow();
workflow.cancel(RITMgr);
new UIActionUtils().approvalsNoLongerRequired(RITMgr.sys_id);
RITMgr.state = 5; // update correct state value
RITMgr.work_notes = "Cancelled by Scheduled Job - Housekeeping Job for RI/RQ";
RITMgr.update();
}
}
Regards,
Shyamkumar
Regards,
Shyamkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2024 05:37 AM
Hi @Rooma1
Please use the following script once-
var gr = new GlideRecord('sysapproval_approver');
gr.addEncodedQuery('state=requested^sysapproval.sys_updated_on<=javascript:gs.beginningOfLast12Months()');
gr.query();
while (gr.next()) {
var RITMgr = new GlideRecord('sc_req_item');
RITMgr.addQuery('sys_id', gr.document_id);
RITMgr.addQuery('state', '!=', '5');
RITMgr.query();
while (RITMgr.next()) {
// Update RITM work notes
RITMgr.work_notes = 'Cancelled by Scheduled Job - Housekeeping Job for RI/RQ';
RITMgr.update();
// Cancel RITM
RITMgr.stage = 'Request Cancelled';
RITMgr.state = '5';
var wf = new global.Workflow();
wf.cancel(RITMgr);
}
// Update approval record
gr.state = 'cancelled';
gr.update();
}
Please let me if it works.