Schedule job condition not working

Priyanka Chaud1
Tera Contributor

Hi team,

I have requirement to send notification one day before and one day after a variable date field in catalog item. I used scheduled job,event and notification for that...but when its running daily its sending notification everyday and it seems not working....like if i keep date as 9oct in date field in catalog form so one noti should be send on 8 and one on 10th but it seems not working can anyone please help me attaching schedule job script here:- @Sandeep Rajput @Dr Atul G- LNG 

Before:-

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=6830ece6937056109458f44e1dba10ee');
gr.addQuery('expected_return_date', '=', gs.daysAgoStart(-1));
gr.query();
while (gr.next()) {
    gs.eventQueue('event.before', gr, gr.requested_for, '');
}
After:-
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=6830ece6937056109458f44e1dba10ee');
gr.addQuery('expected_return_date', '=', gs.daysAgoStart(-1));
gr.query();
while (gr.next()) {
    gs.eventQueue('event.after', gr, gr.requested_for, '');
}
1 ACCEPTED SOLUTION

Hello Priyanka,

 

Can you try this below, 

 

I have executed this in background script, also I see that this is a date/time field so getDisplayValue will not work properly, just getDate should do that job.

 

var catalogItemSysId = '6830ece6937056109458f44e1dba10ee';
var variableDateField = 'expected_return_date';

var grBefore = new GlideRecord('sc_req_item');
grBefore.addQuery('cat_item', catalogItemSysId);
grBefore.query();
while (grBefore.next()) {
    var dt = grBefore.variables[variableDateField];
    if (dt) {  
        var expectedDate = new GlideDateTime(dt);  
        gs.info('expectedDate' +expectedDate);
var today =new GlideDateTime();
var oneDayBefore = new GlideDateTime(dt);
oneDayBefore.addDays(-1);
    gs.info('today' +today);
         if (today.getDate().getNumericValue() == oneDayBefore.getDate().getNumericValue()) {
             {
                 gs.eventQueue('loaner.request.one.day.before', grBefore, grBefore.requested_for, '');  
             }
         }
    }
}

 

 

View solution in original post

34 REPLIES 34

For logs its printing all values because its checking all dates....but ill attach my scenario here.....today is oct 14 I submitted 2 request in one i kept expected return date 15 and in one 16...now when I runned the back script for both RITM  before email got triggered I have attached the email also for you....for request having 16 date that email should have been triggered tomorrow not today that's the issue.... @Omkar Mone 

Hello Priyanka,

 

Can you try this below, 

 

I have executed this in background script, also I see that this is a date/time field so getDisplayValue will not work properly, just getDate should do that job.

 

var catalogItemSysId = '6830ece6937056109458f44e1dba10ee';
var variableDateField = 'expected_return_date';

var grBefore = new GlideRecord('sc_req_item');
grBefore.addQuery('cat_item', catalogItemSysId);
grBefore.query();
while (grBefore.next()) {
    var dt = grBefore.variables[variableDateField];
    if (dt) {  
        var expectedDate = new GlideDateTime(dt);  
        gs.info('expectedDate' +expectedDate);
var today =new GlideDateTime();
var oneDayBefore = new GlideDateTime(dt);
oneDayBefore.addDays(-1);
    gs.info('today' +today);
         if (today.getDate().getNumericValue() == oneDayBefore.getDate().getNumericValue()) {
             {
                 gs.eventQueue('loaner.request.one.day.before', grBefore, grBefore.requested_for, '');  
             }
         }
    }
}

 

 

Tysm @Omkar Mone for the detailed help on this by considering this problem as yours only !! Really tysm for all the efforts! Just one last ques i should run this Schedule job daily at 12 am right?

Hello Priyanka, 

 

It depends, does your customer want email to be received at midnight? If yes, you can, else I would suggest having it just before the day starts, around 830am, whenever you run, it's fine.

Ok tysm @Omkar Mone great idea