How to use 2 eventQueueSchedule() method for different condition(before 24hrs and 2hrs) in one 1 BR?

aakankshapl
Tera Expert

 

Hello 

Could you please help me with this notification trigger using eventQueueSchedule() method.

I have received a requirement related to push notifications generation before the 24 hours and 2 hours of windows start field on work order task. For this we've created a business rule on insert and on update. In this we are successfully able to display the notification for 24 hour before as per the condition. But for 2 hours event it display both notification 24 hrs and 2 hrs. The 24 hrs notification displays before some time of 2 hrs. I will provide the BR script below:

 

BR script:

 

var winStart1 = current.window_start ;

var gdt1 = new GlideDateTime(winStart1);

gdt1.addSeconds(-86400);

gs.eventQueueScheduled ("24.Hour.Push.Notification" , current , gs.getUserID(), gs.getUserName(), gdt1);

 

var winStart2 = current.window_start ;

var gdt2 = new GlideDateTime(winStart2);

gdt2.addSeconds(-7200);

gs.eventQueueScheduled ("2.Hour.Push.Notification" , current , gs.getUserID(), gs.getUserName(), gdt2);

 

 

Your help will be much appreciated as this requirement wants to be done on urgent basis.

1 REPLY 1

aakankshapl
Tera Expert

Hello

Answer to my question if some one needed:
1) Create a push notification.(do not forget to register it in Push application )
2)Create two events in Event Registry
4.) Create a Business Rule on after update and insert.
add condition if specific(my case windows star) field changes.
5) Below is the updated script for BR:

(function executeRule(current, previous /*null when async*/ ) {

var winStart = current.window_start;

// Cancel previously scheduled events for this record
cancelScheduledEvents(current);

// Trigger the 24-hour notification
var gdt24 = new GlideDateTime(winStart);
gdt24.subtract(86400000);
if (gdt24 > new GlideDateTime()) {
    gs.log("24-hour reminder date: " + gdt24);
    gs.eventQueueScheduled("24.Hour.Push.Notification", current, gs.getUserID(), gs.getUserName(), gdt24);
}

// Trigger the 2-hour notification
var gdt2 = new GlideDateTime(winStart);
gdt2.addSeconds(-7200);
if (gdt2 > new GlideDateTime()) {
    gs.log("2-hour reminder date: " + gdt2);
    gs.eventQueueScheduled("2.Hour.Push.Notification", current, gs.getUserID(), gs.getUserName(), gdt2);
}

 //Cancels any pending scheduled events for the current record
function cancelScheduledEvents(current) {
    var eventGr = new GlideRecord('sysevent');
    eventGr.addQuery('instance', current.sys_id);
    eventGr.addQuery('name', 'IN', '24.Hour.Push.Notification,2.Hour.Push.Notification');
    eventGr.query();

    while (eventGr.next()) {
        gs.log("Cancelling event: " + eventGr.name + " for record: " + current.sys_id);
        eventGr.deleteRecord(); // Deletes the scheduled event
    }
}

Do it. It will work smoothly.

Thanks and Regards
Aakanksha Lavande