- 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-10-2024 12:16 AM
I see, please try the below code then -
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()) {
var expectedDate = new GlideDateTime(grBefore.getValue(variableDateField));
var today = new GlideDateTime();
if (expectedDate.getDate().getDisplayValue() == today.getDate().getDisplayValue()) {
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()) {
var expectedDateAfter = new GlideDateTime(grAfter.getValue(variableDateField));
var todayAfter = new GlideDateTime();
if (expectedDateAfter.getDate().getDisplayValue() == todayAfter.getDate().getDisplayValue()) {
gs.eventQueue('event.after', grAfter, grAfter.requested_for, '');
}
}
The code now includes a check that compares the date part of the expected return date to today's date. This ensures that notifications are sent only when the current date matches the expected date minus one day or plus one day.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 12:31 AM
No luck ! @Omkar Mone now both emails are not getting triggered...Can you try once at your end by using one date field in catalog item.....Thank you so much for taking out time and helping me till here but now notifications are only not getting triggered
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 12:33 AM
I don't think this condition should be equal to for both ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 12:12 AM
I think the conditions gs.daysAgostart and gs.daysAhead start is not working if it would work it would have given exact results for notifications @Omkar Mone

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 12:46 AM
Right that was just some typo -
Check this. I think it should work -
var catalogItemSysId = '6830ece6937056109458f44e1dba10ee';
var variableDateField = 'u_variable_date_field';
// Handle one day before the variable date
var grBefore = new GlideRecord('sc_req_item');
grBefore.addQuery('cat_item', catalogItemSysId);
grBefore.query();
while (grBefore.next()) {
var expectedDate = new GlideDateTime(grBefore.getValue(variableDateField));
var today = new GlideDateTime();
today.addDays(-1); // Subtract 1 day to compare with the date field
// Compare the date part only (ignores the time)
if (expectedDate.getDate().getDisplayValue() == today.getDate().getDisplayValue()) {
gs.eventQueue('event.before', grBefore, grBefore.requested_for, '');
}
}
// Handle one day after the variable date
var grAfter = new GlideRecord('sc_req_item');
grAfter.addQuery('cat_item', catalogItemSysId);
grAfter.query();
while (grAfter.next()) {
var expectedDateAfter = new GlideDateTime(grAfter.getValue(variableDateField));
var todayAfter = new GlideDateTime();
todayAfter.addDays(1); // Add 1 day to compare with the date field
// Compare the date part only (ignores the time)
if (expectedDateAfter.getDate().getDisplayValue() == todayAfter.getDate().getDisplayValue()) {
gs.eventQueue('event.after', grAfter, grAfter.requested_for, '');
}
}
I have just tried logging in my PDI without catalog item.