Scheduled job for approvals expiration

Poorva Bhawsar
Mega Sage

Hi Community,

 

I need to trigger an expiration email based on some conditions.

1. A notification needs to be triggered every day when an approval is expired yesterday along with some other filter conditions.

2. A notification needs to be triggered every 1st of a month when an approval is expiring in a month along with some other filter conditions.

This notifications needs to be triggered to product owner.

 

So, here is my scheduled jobs:

1. 

var app = new GlideRecord('cmdb_ci_business_app');
app.addEncodedQuery("operational_status=1^department.business_unit=a3fa3110dbc6f3405917fac6bf96196c^sys_tags.38372c381bb4e150b447a8aa274bcb0c=38372c381bb4e150b447a8aa274bcb0c^u_last_bia_approvalISEMPTY^ORu_last_bia_approvalRELATIVELT@month@ahead@1");
app.query();
while (app.next()) {
    gs.eventQueue('bia.approval.expiration', app, 'month', '');
}
 
var app = new GlideRecord('cmdb_ci_business_app');
app.addEncodedQuery("operational_status=1^department.business_unit=a3fa3110dbc6f3405917fac6bf96196c^sys_tags.38372c381bb4e150b447a8aa274bcb0c=38372c381bb4e150b447a8aa274bcb0c^u_last_bia_approvalISEMPTY^ORu_last_bia_approvalONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()");
app.query();
while (app.next()) {
    gs.eventQueue('bia.approval.expiration', app, 'yesterday', '');
}
 
I have an event registry with the name bia.approval.expiration.
 
I have an email script using which i want to show different email bodies based on the parameters.
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var month = event.parm1;
    //if (month) {
        template.print('This email is for bia approval expiring within a month.');
        template.print('<br>');

    //}

    var yesterday = event.parm1;
    //if (yesterday) {
        template.print('This email is for bia approval expired yesterday.');
        template.print('<br>');
    //}

})(current, template, email, email_action, event);
 
But nothing is working. The email is not getting triggered.
Kindly help me to sort it out.
 
Thanks
12 REPLIES 12

I checked adding logs, its not going inside the while loop. I am getting the value of app before while loop but not after entering the while loop.

@Ankur Bawiskar i was able to resolve the issue. The issue was with the encoded query, one filter condition was weird inside that which was not allowing it enter in the while loop.

 

Now, based on my email script i want to show different email bodies in the email but my email script is not showing any msg in the email body.

 

Can you plz help me with that?

@Poorva Bhawsar 

you can have email script and handle the logic

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I have that script which i have shared above in the question itself but i am not sure about the logic is correct or not?

 

Community Alums
Not applicable

Hi @Poorva Bhawsar ,

 

Looks like there is an issue with the encodedquery- Please double check

please find the below updated scripts-

Schedule Job 1-

var app = new GlideRecord('cmdb_ci_business_app');
app.addEncodedQuery("operational_status=1^department.business_unit=a3fa3110dbc6f3405917fac6bf96196c^sys_tags.38372c381bb4e150b447a8aa274bcb0c=38372c381bb4e150b447a8aa274bcb0c^u_last_bia_approvalONYesterday@javascript&colon;gs.beginningOfYesterday()@javascript&colon;gs.endOfYesterday()");
app.query();
while (app.next()) {
    gs.eventQueue('bia.approval.expiration', app, 'yesterday', '');
}

 

Schedule Job 2-

var app = new GlideRecord('cmdb_ci_business_app');
app.addEncodedQuery("operational_status=1^department.business_unit=a3fa3110dbc6f3405917fac6bf96196c^sys_tags.38372c381bb4e150b447a8aa274bcb0c=38372c381bb4e150b447a8aa274bcb0c^u_last_bia_approvalRELATIVELT@month@ahead@1");
app.query();
while (app.next()) {
    gs.eventQueue('bia.approval.expiration', app, 'month', '');
}

 

Ensure you have an event registered with the name bia.approval.expiration

 

Email Script-

(function runMailScript(current, template, email, email_action, event) {
    var param = event.parm1;
    
    if (param === 'month') {
        template.print('This email is for BIA approval expiring within a month.');
    } else if (param === 'yesterday') {
        template.print('This email is for BIA approval expired yesterday.');
    }
})(current, template, email, email_action, event);

Also ensure that you have a notification set to be triggered by the event bia.approval.expiration

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar