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.

Scheduled jobs to send emails between date ranges

gnewuser
Tera Contributor

Hi 

I have a scheduled jobs in a scoped application which gets record from a table. The table has end date, reminder date, warning date all date fields. 

 

The jobs needs to run everyday and

 

1. check if reminder date is greater than 30 then send a notification every 30 days until the warning date

2. check if reminder date is less than 30 then send a notification every 7 days until the warning date

 

in the code below today is the current date . This is what I have but its not triggering the monthly notice correctly and the weekly is triggering almost every day. What am I doing incorrectly?

 

 

while (gr.next()) {
     var reminderDate = gr.getValue('reminder_date');
     reminderDate = new GlideDateTime(reminderDate);
 
 var warningdate= gr.getValue('warning_date');
warningdate= new GlideDateTime(warningdate);

var duration = GlideDateTime.subtract(today1, reminderDate); 
 
 var daysDifference = duration.getDayPart();

  

    if (daysDifference > 0 && daysDifference % 30 === 0 && reminderDate <= warningdate) {
    gs.eventQueue('monthly email', gr);
     } else if (daysDifference < 30 && reminderDate <= warningdate) {
         gs.eventQueue('weekly email', gr);
		 
      }

 }

 

 

 

2 REPLIES 2

Community Alums
Not applicable

Hi @gnewuser ,

Use encoded query instead of writing so many functions.



Write you script like :



var qrystr = "active=true^expiry_dateBETWEENjavascript&colon;gs.now()@javascript&colon;gs.daysAgo(-30)";

 

var expdt = new GlideRecord("table name");

 

expdt.addEncodedQuery(qrystr);

 

expdt.query();

 

while(expdt.next()){

 

gs.eventQueue('expiration.date',expdt,'','');//pass receipients in the parameter

 

}

 

gnewuser
Tera Contributor

Thanks Sandeep for your response. So you are saying have only one event and pass the expiry date as the parameter for the event? But I need to compare it against warning date also.