Issue with the notification triggering using scheduled job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2023 10:46 PM
I need to trigger three notifications according to the following conditions, and the notifications will be triggered using a Scheduled Job.
Conditions:
- When the due date has just passed and it has not passed 14 days, trigger the first notification. The notification will be sent to the 'Assigned to'.
- When the due date has passed 14 days and it has not passed 28 days, trigger the second notification. The notification will be sent to the 'Assigned to' and 'Assigned to.manager'.
- When the due date has passed 28 days, trigger the third notification. The notification will be sent to the 'Assigned to', 'Assigned to.manager', and 'Assigned to.manager.manager'.
'Each notification should trigger once if the due date is not changed' --> I have created an extra field date/time type (u_last_notification_sent_date) to store the last notification sent date and to check if the notification has been triggered. If the due date is not changed, it will not trigger the notification repeatedly, as the job will run on a daily basis, and there is a chance that it will trigger every day.
These three conditions and the 'Each notification should trigger once' condition are set in the scheduled job.
For all the notifications, there is a common condition: MI Task = True & State = In Progress, which I have set in the notifications.
I am attaching all the necessary screenshots.
Code of the Scheduled Job -
var gr = new GlideRecord('problem_task');
gr.addQuery('due_date', '<=', gs.daysAgoStart(0)); // due_date is just passed
gr.query();
while (gr.next()) {
if (gr.due_date >= gs.daysAgoStart(14) && gr.u_last_notification_sent_date != gs.nowDateTime()) {
gs.eventQueue('ptsk.overdue.0', gr, gr.assignment_group, gr.assigned_to);
gr.u_last_notification_sent_date = gs.nowDateTime();
gr.update();
}
}
var gr2 = new GlideRecord('problem_task');
gr2.addQuery('due_date', '<=', gs.daysAgoStart(14)); // due_date is passed 14 days
gr2.addQuery('due_date', '>=', gs.daysAgoStart(28));
gr2.query();
while (gr2.next()) {
if (gr2.u_last_notification_sent_date != gs.nowDateTime()) {
gs.eventQueue('ptsk.overdue.14', gr2, gr2.assignment_group, gr2.assigned_to);
gr2.u_last_notification_sent_date = gs.nowDateTime();
gr2.update();
}
}
var gr3 = new GlideRecord('problem_task');
gr3.addQuery('due_date', '<=', gs.daysAgoStart(28)); // due_date is passed 28 days
gr3.query();
while (gr3.next()) {
if (gr3.u_last_notification_sent_date != gs.nowDateTime()) {
gs.eventQueue('ptsk.overdue.28', gr3, gr3.assignment_group, gr3.assigned_to);
gr3.u_last_notification_sent_date = gs.nowDateTime();
gr3.update();
}
}
Scheduled Job:
Sharing the one email and event (3rd Condition)
Event:
Email:
Could you please check and let me know why the three notifications are not triggering?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2023 11:13 PM
@Kaushik Ghosh
Because event was not triggered..
Use eventQueue api to trigger the event in your script job..
https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_GlideSystemAPI#r_GS-ev...
eventQueue(String name, Object glideRecord, String parm1, String parm2, String queue)
name | String | Name of the event being queued. |
glideRecord | Object | GlideRecord object, such as "current". |
parm1 | String | (Optional) Saved with the instance if specified. |
parm2 | String | (Optional) Saved with the instance if specified. |
queue | String | Name of the queue. |
void | Method does not return a value |
Scoped equivalent:
To use the eventQueue() method in a scoped application, use the corresponding scoped method: eventQueue().
Example
if (current.operation() != 'insert' && current.comments.changes()) { gs.eventQueue("incident.commented", current, gs.getUserID(), gs.getUserName()); }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2023 11:29 PM - edited 11-23-2023 11:31 PM
Hi @newhand ,
I am not good at the event thing; could you help me restructure any one of the eventQueue and email notification? So that it will trigger the notification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2023 11:50 PM
@newhand, Could you please let me know, the condition wise the code is correct or not?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2023 12:33 AM - edited 11-24-2023 12:53 AM
@Kaushik Ghosh
I'm sorry, my answer maybe not correct..
I think the main reason is : gs.daysAgoStart returns a datetime string ...
so convert it into glidedate object when using ...
I will write a test code ,and let you know the result.
well ...it seems can work ...
and now i'm not sure where is the problem.
can you put some log into you code (before calling gs.eventQueue)?
and two small suggestions。
1. the new field you added to the table , may can not work well.
I think a simple state flag may work better .
sample :
null or '' : no notification was sended
1 : <=14days notification has been sended
2: 14-28 days notification has been sended
3: >=28 day notification has been sended.
2. These two conditions are setted to the notification condition " MI Task = True & State = In Progress " , why not set them to the query conditions?