Trigger same event while scheduled job runs in 2 different condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 02:44 AM - edited 04-24-2025 04:27 AM
Hi All,
I have a schedule job which triggers an event . Now the recipients receives a reminder only when the capacity review due date was 14 days away. Want to send an additional reminder 7 days before the due date. I have tried the below script and actually I don't want to change anything on the highlighted part. Just want to add the the script for 7 days . Please correct me if my script is wrong. I have tried this and then observed the schedule log that it's last run is 22nd where the schedule job runs daily . For today there are no logs. Please let me know if you need more information
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 03:09 AM
Hi @1_DipikaD
Try the below script. It'll work.
var gr_user = new GlideRecord('u_capacity_reviews');
gr_user.addEncodedQuery('u_status=pending^active=true');
gr_user.query();
while (gr_user.next()) {
var reviewdate = new GlideDateTime(gr_user.u_capacity_review_due_date.toString());
reviewdate.addDaysLocalTime(parseInt(-13));
var duedate = reviewdate.getDisplayValue().split(' ')[0];
var gr_date = new GlideDate();
if (gr_date.toString() == duedate.toString()) {
gs.eventQueue('notify.capacity.reviewers', gr_user, gr_user.u_application_instance.u_support_owner, gr_user.u_application_instance.owned_by);
}
//-----ADDED------
var reminder = new GlideDateTime(gr_user.u_capacity_review_due_date.toString());
reminder.addDaysLocalTime(-6);
var duedate2 = reminder.getDisplayValue().split(' ')[0];
var currentDate = new GlideDate();
if (currentDate.toString() == duedate2.toString()) {
gs.eventQueue('notify.capacity.reviewers', gr_user, gr_user.u_application_instance.u_support_owner, gr_user.u_application_instance.owned_by);
}
//-----ADDED------
}
After reviewing your code, I observed that the following line is causing an issue. In your script, on the 6th line, you are subtracting 13 days. For example, if the capacity review date is on April 14th, the review date will be updated to April 1st. Then, on the 13th line, you are initiating a new GlideDateTime object using the review date and subtracting 6 days from it. As a result, the reminder date becomes March 26th.
Line 6: var reminder = new GlideDateTime(reviewdate);
Line 13: var reminder = new GlideDateTime(reviewdate);
Hope this helps.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 03:32 AM
Hi @J Siva,
Thanks for your response.
Want to be clear one thing .So if review date is 14 April then 1st reminder will go on 1st April and 2nd reminder will go on 7th April after making the changes. Am I right ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 03:51 AM
@1_DipikaD - Yes. it'll work.
If you want to test and understand the difference b/w my script and your script. Run the blelow scripts in the "Scripts - background".
Your script:
var reviewdate = new GlideDateTime('2025-04-30 10:03:16'); // Replace the date with '2025-05-07 10:03:16' to check 14 days validation
reviewdate.addDaysLocalTime(parseInt(-13));
var duedate = reviewdate.getDisplayValue().split(' ')[0];
if (new GlideDate().toString() == duedate.toString()) {
gs.print("Yes.. trigger - 14days");
}else {
gs.print("No.. Don't trigger-7days");
}
var reminder = new GlideDateTime(reviewdate);
reminder.addDaysLocalTime(-6);
var currentDate = new GlideDate().getDisplayValue();
if (currentDate == reminder.getDisplayValue()) {
gs.print("Yes.. trigger-7days");
} else {
gs.print("No.. Don't trigger-14days");
}
My script:
var reviewdate = new GlideDateTime('2025-04-30 10:03:16'); // Replace the date with '2025-05-07 10:03:16' to check 14 days validation
reviewdate.addDaysLocalTime(parseInt(-13));
var duedate = reviewdate.getDisplayValue().split(' ')[0];
if (new GlideDate().toString() == duedate.toString()) {
gs.print("Yes.. trigger-14days");
}else {
gs.print("No.. Don't trigger-7days");
}
var reminder = new GlideDateTime('2025-04-30 10:03:16');
reminder.addDaysLocalTime(-6);
var duedate2 = reminder.getDisplayValue().split(' ')[0];
var currentDate = new GlideDate();
if (currentDate.toString() == duedate2.toString()) {
gs.print("Yes.. trigger-7days");
} else {
gs.print("No.. Don't trigger-14days");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2025 03:58 AM
can you try this?
var gr_user = new GlideRecord('u_capacity_reviews');
gr_user.addEncodedQuery('u_status=pending^active=true');
gr_user.query();
while (gr_user.next()) {
var dueDate = new GlideDateTime(gr_user.u_capacity_review_due_date.toString());
var today = new GlideDate();
// 14 days before due date
var reminder14 = new GlideDateTime(dueDate);
reminder14.addDaysLocalTime(-14);
if (today.toString() == reminder14.getDate().toString()) {
gs.eventQueue('notify.capacity.reviewers', gr_user, gr_user.u_application_instance.u_support_owner, gr_user.u_application_instance.owned_by);
}
// 7 days before due date
var reminder7 = new GlideDateTime(dueDate);
reminder7.addDaysLocalTime(-7);
if (today.toString() == reminder7.getDate().toString()) {
gs.eventQueue('notify.capacity.reviewers', gr_user, gr_user.u_application_instance.u_support_owner, gr_user.u_application_instance.owned_by);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader