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

Hey @Omkar Mukesh Mo again both notifications are getting triggered if i keep my expected_return_date as 11 and fill the form and submit i kept both schedule job to run periodically 1 min to check...so both notifications coming....after noti should be triggred on 12 not today

Hello

 

I see, then you need to create new fields in order to keep a track.

notification_sent_before and notification_sent_after to determine if a notification has already been sent for the given record.

 

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

var grBefore = new GlideRecord('sc_req_item'); 
grBefore.addQuery('cat_item', catalogItemSysId); 
grBefore.addQuery(variableDateField, '=', gs.daysAgoStart(1)); 
grBefore.addQuery('notification_sent_before', '!=', true); // Check if notification already sent
grBefore.query();

while (grBefore.next()) {
    gs.eventQueue('event.before', grBefore, grBefore.requested_for, '');
    grBefore.notification_sent_before = true; // Set a flag to indicate notification was sent
    grBefore.update(); // Update the record to save the flag
}

var grAfter = new GlideRecord('sc_req_item');
grAfter.addQuery('cat_item', catalogItemSysId); 
grAfter.addQuery(variableDateField, '=', gs.daysAheadStart(1)); 
grAfter.addQuery('notification_sent_after', '!=', true); // Check if notification already sent
grAfter.query();

while (grAfter.next()) {
    gs.eventQueue('event.after', grAfter, grAfter.requested_for, '');
    grAfter.notification_sent_after = true; // Set a flag to indicate notification was sent
    grAfter.update(); // Update the record to save the flag
}

 

Do i have to define these fields somewhere or just use in schedule job?  notification_sent_before and notification_sent_after

Create 2 new fields on sc_req_item table.

We can't create custom fields on that table @Omkar Mone