- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 07:35 PM
Hi All,
I have a requirement to create a cleanup job on 'sysapproval_approver' table so that it can clean the records pending for more than a year. I want to set the limit to 100 via system property and want to call that property in the script. I do not want to use setLimit method.
Given below is the snippet code:
var gr = new GlideRecord('sysapproval_approver');
gr.addEncodedQuery('sysapproval.number=RI0439973^state=requested');
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()) {
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);
gs.print('Numbers are '+ RITMgr.number);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 07:36 PM
HI @Rooma1 ,
I trust you are doing great.
You can modify the code as follow :
// Get the cleanup limit from the system property
var cleanupLimit = gs.getProperty('your_system_property_name', 100);
// Create a date object representing one year ago
var oneYearAgo = new GlideDateTime();
oneYearAgo.subtractYearsLocalTime(1);
// Create a GlideRecord query on 'sysapproval_approver' table
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval.state', 'requested');
gr.addQuery('sysapproval.sys_created_on', '<=', oneYearAgo);
gr.setLimit(cleanupLimit); // Setting the limit
gr.query();
while (gr.next()) {
// Your existing logic to process each record
var RITMgr = new GlideRecord('sc_req_item');
RITMgr.addQuery('sys_id', gr.document_id);
RITMgr.addQuery('state', '!=', '5');
RITMgr.query();
while (RITMgr.next()) {
// Your existing logic to update and cancel request items
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);
gs.print('Numbers are ' + RITMgr.number);
}
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2024 07:02 AM
Could you please apply some log and check where the code is getting failed, also try with below updated code:
cleanup: function() {
var cleanupLimit = gs.getProperty('housekeeping_cleanup.max_approval_record');
// Loop through all sysapproval_approver records
var gr = new GlideRecord("sysapproval_approver");
gr.addEncodedQuery("state=requested^source_table=sc_req_item^ORsource_table=sc_request");
gr.setLimit(cleanupLimit);
gr.query();
while (gr.next()) {
if (gr.source_table == 'sc_req_item') {
// Handle sc_req_item records
var ritm = new GlideRecord("sc_req_item");
if (ritm.get(gr.document_id)) {
ritm.work_notes = 'Cancelled by Scheduled Job - Housekeeping Job for RI/RQ';
ritm.stage = "Request Cancelled";
ritm.state = "5";
ritm.setWorkflow(false);
ritm.update();
var workflow = new Workflow();
workflow.cancel(ritm);
}
} else if (gr.source_table == 'sc_request') {
// Handle sc_request records
var req = new GlideRecord("sc_request");
if (req.get(gr.document_id)) {
var message = gs.getMessage('Request has been cancelled as the approval was not given within 90 days');
gs.addInfoMessage(message);
var workflow = new Workflow();
workflow.cancel(req);
req.request_state = 'closed_cancelled';
req.approval = 'rejected';
req.state = 5;
req.update();
}
}
// Cancel the approval request
gr.state = "cancelled";
gr.setWorkflow(false);
gr.update();
}
gs.log("End housekeeping", "RI/RQ Housekeeping");
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 04:02 AM
@Maddysunil Thanks for the suggestion. I want to add one more system property which will store the Approval record age like 365 days and i want to add it in the encoded query.
What is the syntax to get the no. of records from system property.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 04:25 AM
you can use something like this:
var maxApprovalAgeDays = gs.getProperty('housekeeping_cleanup.max_approval_age_days');
var encodedQuery = "state=requested^source_tableINsc_req_item,sc_request^sys_created_on<javascript:gs.daysAgoStart(" + maxApprovalAgeDays + ")";
gr.addEncodedQuery(encodedQuery);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2024 11:55 PM
@Maddysunil I am very thankful to you for all the solutions you have provided till now and it was very helpful for me.
There is one more requirement now where I am getting stuck and not sure why the code is not working.
Here, the requirement is once the RITM's approval and the stage gets cancelled and the worknotes get updated, I need to check if there are any more RITM open for that parent REQ. If none of the RITM is open, then I need to update the REQ's Approval state to Closed Incomplete.
Use Case, it should check:
1. If 1 RITM and it is cancelled, then the REQ state should get updated to Closed Incomplete.
2. If 2 RITMs, one gets closed cancelled and the other is already closed, then the REQ state should get updated to Closed Incomplete.
3. If 2 RITMs, one is closed cancelled and other is still open, then the REQ should not get updated.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 08:11 PM
HI @Rooma1 without set limit , you can use system property to set Batch size like below
Harish