Pending Approval - older than 2 months? - set to closed cancel

Nani18
Tera Contributor

Hello Experts,

 

My requirement is 

 

Where Cost Centre Approval is not actioned  the Request goes into a stuck state in 'Pending Approval'.  After 2 months these should be set to Closed Cancelled. with a comment in the Request work notes "No action on Cost Centre Approval for over 2 months hence cancelling the request"

 

How to achieve this...?

 

Thanks in advance 

 

Best Regards,

Nani 

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Nani18 ,
I trust you are doing great.
You can achive this by creating a scheduled job .
Please find the code for the same

var twoMonthsAgo = new GlideDateTime();
twoMonthsAgo.addMonthsLocalTime(-2); // Adjust the time to 2 months back

var gr = new GlideRecord('request_table'); // Replace 'request_table' with the actual table name
gr.addQuery('state', 'Pending Approval'); // Adjust field names and values as per your configuration
gr.addQuery('sys_updated_on', '<', twoMonthsAgo); // Change to a field that tracks the last approval action
gr.query();

while (gr.next()) {
    gr.state = 'Closed Cancelled'; // Set the correct field and value for updating the state
    gr.work_notes = 'No action on Cost Centre Approval for over 2 months hence cancelling the request';
    gr.update();
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

3 REPLIES 3

Dibyaratnam
Tera Sage

So whenever the request is being sent for approval, there must be an approval record getting generated which should be approved by the user. So create a scheduled job which should run daily and check for the difference between approval record creation date and the date scheduled job is running. If it exceeds 2 months, update the request to cancelled state.

Tai Vu
Kilo Patron
Kilo Patron

Hi @Nani18 

You can set up a daily schedule to query for all Requested Approval records created more than two months ago, specifically those associated with the catalog item "Cost Centre." This process will provide you with a list of Requested Items that are eligible for cancellation.

Sample

var itemID = '<your_catalog_item_sys_id>'; //replace your catalog item sys_id
var arrRITM = [];
var grApproval = new GlideRecord('sysapproval_approver');
grApproval.addQuery('source_table', 'sc_req_item');
grApproval.addQuery('sysapproval.ref_sc_req_item.cat_item', itemID);
grApproval.addQuery('state', 'requested');
grApproval.addQuery('sys_created_on', '<', gs.monthsAgo(2));
grApproval.query();
while(grApproval.next()){
	var ritmID = grApproval.getValue('document_id');
	if(arrRITM.indexOf(ritmID) >= 0){
		continue;
	}
	arrRITM.push(ritmID);
}

var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('sys_id', 'IN', arrRITM.join(','));
grRITM.query();
while(grRITM.next()){
	
	//Cancel Workflow (Use this if your item using workflow)
	/*
	var workflow = new Workflow();
	workflow.cancel(grRITM);
	*/

	//Cancel Flow
	sn_fd.FlowAPI.cancel(grRITM.getUniqueValue(), 'Canceling Test Flows');

	//Update RITM
	grRITM.state = '<your_closed_cancelled_state>'; //replace your state value
	grRITM.work_notes = "No action on Cost Centre Approval for over 2 months hence cancelling the request";
	grRITM.update();
}

 

Cheers,

Tai Vu

Amit Gujarathi
Giga Sage
Giga Sage

HI @Nani18 ,
I trust you are doing great.
You can achive this by creating a scheduled job .
Please find the code for the same

var twoMonthsAgo = new GlideDateTime();
twoMonthsAgo.addMonthsLocalTime(-2); // Adjust the time to 2 months back

var gr = new GlideRecord('request_table'); // Replace 'request_table' with the actual table name
gr.addQuery('state', 'Pending Approval'); // Adjust field names and values as per your configuration
gr.addQuery('sys_updated_on', '<', twoMonthsAgo); // Change to a field that tracks the last approval action
gr.query();

while (gr.next()) {
    gr.state = 'Closed Cancelled'; // Set the correct field and value for updating the state
    gr.work_notes = 'No action on Cost Centre Approval for over 2 months hence cancelling the request';
    gr.update();
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi