Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Trigger Notification before 60 days based on specific field

vivek110
Tera Contributor

Hello All,
I got an requirment that we need to trigger notification before 60 days of the contract expiry date (Date field).

Can anyone help me how to configure this requirment.

Regards,
Hemanth

4 REPLIES 4

ersureshbe
Giga Sage
Giga Sage

Hi,

It is advisable to utilize a scheduled job, as this will facilitate the specification of the time frequency. Furthermore, consider using a field or flag from the table to establish the trigger condition. Additionally, prepare a notification template and invoke it through the use of a script.

 

Regards,
Suresh.

Hello ersureshbe,
i have tried the scheduled job but the notification is not triggering 
var gr = new GlideRecord('ast_contract');
gr.addQuery('active', true);
gr.query();

while(gr.next()){

var todayDate = new GlideDateTime();

var expiry = new GlideDateTime(gr.getValue('ends'));

var duration = gs.dateDiff(todayDate.getDisplayValue(),expiry.getDisplayValue()t, true);

if(days==60){

gs.eventQueue('sn_pr.expiry.notification', gr , gr.sys_id, null);

}

}

please correct my code if i have gone wrong

 

Regards,
Hemanth

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @vivek110 ,

PLease give a try to the script below and see how it works for you!!!

// Query the 'ast_contract' table for active contracts
var gr = new GlideRecord('ast_contract');
gr.addQuery('active', true);  // Only process active contracts
gr.query();

while (gr.next()) {
    // Get today's date
    var todayDate = new GlideDateTime();
    
    // Get the contract's expiry date
    var expiry = new GlideDateTime(gr.getValue('ends'));

    // Calculate the difference in milliseconds
    var duration = gs.dateDiff(todayDate.getDisplayValue(), expiry.getDisplayValue(), true);

    // Convert the difference from milliseconds to days
    var daysDifference = parseInt(duration) / (1000 * 60 * 60 * 24); 

    // Trigger notification if there are exactly 60 days left until expiry
    if (daysDifference === 60) {
        gs.eventQueue('sn_pr.expiry.notification', gr, gr.sys_id, null);  // Queue the notification event
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

Hi Aniket Chavan,

I have tried your code but it is not working notification is not triggering

Preview