- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2024 04:40 AM - edited ‎10-09-2024 05:19 AM
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:-
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-14-2024 02:08 AM
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, '');
}
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2024 06:49 AM
Hi,
To query on variables, you need to prefix with "variables.". Try the below code
To send email one day earlier
To send email on next day
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2024 09:04 AM
hey @palanikumar i tried the script you provided then i submitted one request by keeping expected return date as 10 oct but when i executed both scheduled jobs so for both emails got triggered but it should be only one the before one because after one should be triggered on 11 so why did both emails got triggered that's the main issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2024 11:22 PM
Updated Scheduled Job Script:
Before Notification (One day before the expected date):
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=6830ece6937056109458f44e1dba10ee');
gr.addQuery('expected_return_date', '=', gs.daysAgoStart(-1)); // One day before
gr.addNullQuery('notified_before'); // Only pick records where notification hasn't been sent
gr.query();
while (gr.next()) {
gs.eventQueue('event.before', gr, gr.requested_for, '');
// Update to indicate the notification was sent
gr.setValue('notified_before', true);
gr.update();
}
After Notification (One day after the expected date):
var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('cat_item=6830ece6937056109458f44e1dba10ee');
gr.addQuery('expected_return_date', '=', gs.daysAgoStart(1)); // One day after
gr.addNullQuery('notified_after'); // Only pick records where notification hasn't been sent
gr.query();
while (gr.next()) {
gs.eventQueue('event.after', gr, gr.requested_for, '');
// Update to indicate the notification was sent
gr.setValue('notified_after', true);
gr.update();
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2024 11:27 PM
Hey,
Can you check if this works?
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.query();
while (grBefore.next()) {
gs.eventQueue('event.before', grBefore, grBefore.requested_for, '');
}
var grAfter = new GlideRecord('sc_req_item');
grAfter.addQuery('cat_item', catalogItemSysId);
grAfter.addQuery(variableDateField, '=', gs.daysAheadStart(1));
grAfter.query();
while (grAfter.next()) {
gs.eventQueue('event.after', grAfter, grAfter.requested_for, '');
}