Send notification 14 days before the expiry date (variable on record producer)

akash kaj
Tera Contributor

i want to send notification 14 days before the expiry date.Expiry date is the variable(type -Date) on record producer and it is a scoped application

9 REPLIES 9

Expiry date is the variable on record producer 

find_real_file.png

Hello Akash,

Could you please check with below script:

I have updated the query as mentioned below. Replace your variable sys_id in the below query:

var query = "variables.<variable_sys_id>ON" + expiryDate +"@javascript:gs.dateGenerate('" + expiryDate + "','start')@javascript:gs.dateGenerate('" + expiryDate + "','end')";

To get the Variable sys_id you can apply filter on Question and then select your record producer and then variable. 

find_real_file.png

Click on run and right click on Filter and click on copy query

find_real_file.png

in query you will get variables.55ab6ce3973d91104cadfb000153af3a (this is the variable sys_id) replace that in the below script

(function () {
	var expiryDateGDT = new GlideDateTime();
	expiryDateGDT.addDays(14);
	var expiryDate = expiryDateGDT.getDate();
	var query = "variables.<variable_sys_id>ON" + expiryDate +"@javascript:gs.dateGenerate('" + expiryDate + "','start')@javascript:gs.dateGenerate('" + expiryDate + "','end')";
	
	var incidentGR = new GlideRecord("Your Table Name");
	incidentGR.addActiveQuery();
	incidentGR.addEncodedQuery(query);
	while (incidentGR.next()) {
		gs.eventQueue("Your EVENT name", incidentGR, "Your Recepient", "");
	}
	
})();

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

I tried your code but still it is not working.let me know where i am going wrong

 

find_real_file.png

Always put your code snippet instead of screenshot. It is easy to modify the code.

Please check with below code I missed to add incidentGR.query():

Also make sure that a notification is created to trigger on sn_wsd_case.wc.expiredate event and the notification has atleast 1 recepient selected else you need to pass the recepient in gs.eventQueue() as shown below:

   gs.eventQueue("Your EVENT name", incidentGR, incidentGR.caller_id, "");

(function () {
    var expiryDateGDT = new GlideDateTime();
    expiryDateGDT.addDays(14);
    var expiryDate = expiryDateGDT.getDate();
    var query = "variables.<variable_sys_id>ON" + expiryDate +"@javascript:gs.dateGenerate('" + expiryDate + "','start')@javascript:gs.dateGenerate('" + expiryDate + "','end')";
    
    var incidentGR = new GlideRecord("Your Table Name");
    incidentGR.addActiveQuery();
    incidentGR.addEncodedQuery(query);
    incidentGR.query()
    while (incidentGR.next()) {
        gs.eventQueue("Your EVENT name", incidentGR, "Your Recepient", "");
    }
})();

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can achieve this using flow designer with no script

check this link where I shared solution for something similar and you will have to enhance it with proper filter condition

I cannot seem to get an Email Notification when a record's due date is reached

regards
Ankur

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