Help with Script - Trigger approval reminder email in every 3 Business days till 30 Buisness Days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 06:17 AM
Dear Team,
I have a requirement to trigger an approval reminder email to approver in every 3 Business days and continue to trigger till 30 Business Days if no approver not approves the respective RITM, if RITM not approved/rejected till 30th day, then RITM will be cancelled.
I have wrote the below Scheduled Job script, but it triggers reminder emails in every business days
notifyApprover();
function notifyApprover() {
var notifydays = ['3', '6', '9', '12', '15', '18', '21', '24', '27'];
var apprv = new GlideRecord('sysapproval_approver');
apprv.addEncodedQuery('sysapproval.ref_sc_req_item.cat_item=ba2d4df71b77fd5063b0a60bbc4bcbaa^ORsysapproval.ref_sc_req_item.cat_item=f4e78c311b0c46d063b0a60bbc4bcb1d^state=requested');
apprv.query();
while (apprv.next()) {
var createdDateinit = apprv.sysapproval.sys_created_on.toString();
var createdDateinitarr = createdDateinit.split(' ');
var createdfinal = createdDateinitarr[0] + " 00:00:00";
var currentDate = String(new GlideDateTime());
var currentDatearr = currentDate.split(' ');
var currentDatefinal = currentDatearr[0] + " 00:00:00";
var schid = '28a5b8ff1b1e641081f8dd77cc4bcb07';
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var resp = plannedTaskAPI.calculateDuration(createdfinal, currentDatefinal, schid);
var response = new JSON().decode(resp);
var duration = response.duration;
var durationdays = duration.split(' ')[0];
var ritmObj = apprv.sysapproval.getRefRecord();
if (notifydays.indexOf(durationdays) > -1) {
gs.eventQueue('approval.notify.consultant', ritmObj, apprv.getValue('approver'), apprv.getDisplayValue('approver'));
}
if (durationdays == '30') {
cancelRequest(apprv.sysapproval.toString());
}
}
}
function cancelRequest(ritmid) {
var ritmObj = new GlideRecord('sc_req_item');
if (ritmObj(ritmid)) {
ritmObj.state = 1000;
ritmObj.update();
}
}
O/p of Emails
Kindly help to modify the above script so that it can trigger in every 3 business days not every business days
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 08:05 AM
@rishabh31 Pls try this,
notifyApprover();
function notifyApprover() {
// Notify on these business days
var notifydays = ['3', '6', '9', '12', '15', '18', '21', '24', '27'];
var scheduleID = '28a5b8ff1b1e641081f8dd77cc4bcb07'; // Your business schedule ID
// Query the approvers for the specific catalog items where approval is pending
var apprv = new GlideRecord('sysapproval_approver');
apprv.addEncodedQuery('sysapproval.ref_sc_req_item.cat_itemINba2d4df71b77fd5063b0a60bbc4bcbaa,f4e78c311b0c46d063b0a60bbc4bcb1d^state=requested');
apprv.query();
while (apprv.next()) {
// Get the created date
var createdDate = apprv.sysapproval.sys_created_on.getDisplayValue();
var currentDate = new GlideDateTime().getDisplayValue();
// Calculate the duration between the created date and the current date in business days
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var durationResult = plannedTaskAPI.calculateDuration(createdDate, currentDate, scheduleID);
var durationDays = parseInt(new JSON().decode(durationResult).duration.split(' ')[0]);
// Get the RITM object
var ritmObj = apprv.sysapproval.getRefRecord();
// Send reminder emails on specific days
if (notifydays.indexOf(durationDays.toString()) > -1) {
gs.eventQueue('approval.notify.consultant', ritmObj, apprv.approver.toString(), apprv.approver.getDisplayValue());
}
// Cancel RITM if the 30th business day is reached without approval
if (durationDays === 30) {
cancelRequest(ritmObj);
}
}
}
function cancelRequest(ritmObj) {
if (ritmObj.isValidRecord()) {
ritmObj.state = 1000; // State to 'Cancelled'
ritmObj.stage = 'Closed'; // Stage to 'Closed'
ritmObj.update();
gs.addInfoMessage('RITM ' + ritmObj.number + ' has been cancelled after 30 business days of inactivity.');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 08:48 PM
@User400849 thanks for your response, not working could you pls try this in your instance and help me with the script modification.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 09:16 AM
Hi @rishabh31 ,
Use below approach to send the notification.
You could do that with just a single event with a scheduled job.
Just create a scheduled job that runs daily. Then have it loop through your certificate table and for each record compare today to their expiry date. If the day is 90, 60, 30, 15 or 1 then you'll simply trigger the event with the amount of days left as one of the parameters.
In your notification you can just use the parm in the subject or in a script to decide how the notifications content looks like. Or if you want you can just trigger different events for each day and have separate notifications for them.
Here's a sample script. I've added a single if check but you can add more ifs for other dates if you want.
var certGr = new GlideRecord('sys_certificate');//update your table name
certGr.query();
while(certGr.next()) {
var expires= new GlideDateTime(certGr.expires);
var now = new GlideDateTime();
var dur = new GlideDuration();
dur = GlideDateTime.subtract(now, expires);
var days = dur.getDayPart();
if(days==1 ||days==30 || days == 60 || days == 30 || days==90){
gs.eventQueue("your_event_name", gr, "recipient", days);
}
}
You could also add an query to only check for records that are expiring after today with
addQuery("expires>javascript:gs.endOfToday()");
Please mark as helpful/correct if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2024 08:41 PM
Thanks @Sumanth16 for your response but in your script where to place schedule id for business days as it has placed in my script.
I shall be grateful if you help with the modifying part.
Thanks