Business rule is updating two times

siroja1
Tera Contributor

I written a for a requirement for sending email notifications for Change tasks when the planned end time five min before and after 5 min. the following script I used, BUt it is sending twice the mails. please help me..

BR rule :  Before update

when: state changes to implementation in progress

table: change_request.

 

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

    // Add your code here

    var caseTask = new GlideRecord("change_task");
    caseTask.addQuery("parent", current.sys_id);
    caseTask.query();
    while (caseTask.next()) {
        if(caseTask.u_type == "Ad hoc" || caseTask.u_type == "general"){
        var gdt = new GlideDateTime(current.end_date);
        gs.log("planned end time: " +gdt);
        gdt.addSeconds(-300);
        var gdt1 = new GlideDateTime(current.end_date);
        gdt1.addSeconds(300);
        caseTask.update();
        gs.eventQueueScheduled("change_task.reminder", caseTask, """",gdt);
        gs.eventQueueScheduled("change_task.reminder", caseTask, """",gdt1);
        }
    }
 

})(current, previous);
3 REPLIES 3

Kartik Magadum
Kilo Sage

Hello @siroja1 

 

The issue you're facing with your script sending duplicate emails seems to be related to the fact that you're using gs.eventQueueScheduled twice within the loop for each caseTask record. To resolve this issue, you should update the caseTask record once and then schedule the email notifications outside of the loop.

Here's an updated version of your script:

 

(function executeRule(current, previous /*null when async*/) {
    // Add your code here

    var caseTask = new GlideRecord("change_task");
    caseTask.addQuery("parent", current.sys_id);
    caseTask.query();
    
    // Create arrays to store GlideDateTime objects for notifications
    var notificationTimes = [];

    while (caseTask.next()) {
        if (caseTask.u_type == "Ad hoc" || caseTask.u_type == "general") {
            var gdt = new GlideDateTime(current.end_date);
            gs.log("planned end time: " + gdt);
            gdt.addSeconds(-300);
            var gdt1 = new GlideDateTime(current.end_date);
            gdt1.addSeconds(300);

            // Store the GlideDateTime objects in the array
            notificationTimes.push(gdt);
            notificationTimes.push(gdt1);
        }
    }

    // Now, update the caseTask record once outside of the loop
    if (notificationTimes.length > 0) {
        caseTask.update();

        // Schedule email notifications for each GlideDateTime in the array
        for (var i = 0; i < notificationTimes.length; i++) {
            gs.eventQueueScheduled("change_task.reminder", caseTask, "", "", notificationTimes[i]);
        }
    }
})();

 

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum

 

Hello @Kartik Magadum 

Thanks for your reply, I tried this script, but it is also executing twice. I think this is happening because I used Ctask.update(); can you help me with it or can we stop while loop after the record found.

Hello @siroja1 

 

Please try below script

 

(function executeRule(current, previous /*null when async*/) {
    // Add your code here

    var caseTask = new GlideRecord("change_task");
    caseTask.addQuery("parent", current.sys_id);
    caseTask.addQuery("u_type", "IN", ["Ad hoc", "general"]);
    caseTask.query();

    // Create arrays to store GlideDateTime objects for notifications
    var notificationTimes = [];

    var gdt = new GlideDateTime(current.end_date);
    gdt.addSeconds(-300);
    var gdt1 = new GlideDateTime(current.end_date);
    gdt1.addSeconds(300);

    while (caseTask.next()) {
        // Store the GlideDateTime objects in the array
        notificationTimes.push(gdt);
        notificationTimes.push(gdt1);
    }

    // Check if there are any records that match the conditions
    if (notificationTimes.length > 0) {
        // Now, update the caseTask record once outside of the loop
        caseTask.update();

        // Schedule email notifications for each GlideDateTime in the array
        for (var i = 0; i < notificationTimes.length; i++) {
            gs.eventQueueScheduled("change_task.reminder", caseTask, "", "", notificationTimes[i]);
        }
    }
})();

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum