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.

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

No its not related to that i think if you built a scenerio at you end and try u will get it @Omkar Mone  Just try in one catalog item with date field and check before and after noti because its not working really

Any update @Omkar Mone ??

Hello @Priyanka Chaud1 ,

 

It's working just fine as per my knowledge. Can you please put some logs around the dates of the above working code, and share please

I modified the code put some logs as well but still its not working.......i submitted 2 request in 1 i kept expected date as 14 and in one 15...when i runned in background script ....email got triggerred for both...but as its 13 today only for one RITM email should have been triggered...talking about before script @Omkar Mone 

 

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 cdt =new GlideDateTime();
cdt.addDays(-1);
    gs.info('cdt' +cdt);
        var today = new GlideDateTime(dt);
       
        today.addDays(-1);
            gs.info('today' +today);
       
         if (cdt.getNumericValue()<expectedDate.getNumericValue() && today.getNumericValue()>cdt.getNumericValue()) {
             {
                gs.info('inside' +expectedDate);
                 gs.eventQueue('loaner.request.one.day.before', grBefore, grBefore.requested_for, '');  
             }
         }
    }
}

Hello Priyanka,

 

Till where did you get logs? Can you paste the output of your background script please?