Send Follow-up Notifications

Balaji Munusamy
Tera Contributor

Hi,

@Ankur Bawiskar @Community Alums @Ratnakar 

I have a scenario when 'State' is 'InProgress' for 10days , scheduled job should trigger notification to team. I have created four custom True/False fields in the table i.e. Follow-up 1, Follow-up 2, Follow-up 3 and Follow-up 4. Once the team get notified as These are all the cases still 'InProgress' then team manually mark Follow-up 1 as true, again 10 days after the 1st follow-up if the state is still 'InProgress' then it should trigger 2nd follow-up and team manually mark the Follow-up2 and update the case. Likewise it continue until 4th follow-up. And also it should check day if it is Saturday and Sunday it should not trigger notification instead of that it should mark the record for 'Monday Notification'. Notification Should send only on Weekdays.

 

I have created Scheduled Job but it keeps on running in the loop and Follow-up 1 only working. Can anyone check the script

 

var gdToday = new GlideDateTime(); // get current date
var day = gdToday.getDayOfWeekUTC(); // get the week day numeric value
var gr1 = new GlideRecord('case');

gr1.addQuery('state', 'In Progress');

gr1.addQuery('inprogress_time', '!=', ' ');

gr1.addQuery('inprogress_time', '>=', gs.daysAgoStart(10));
gr1.query();

var recordFor10Days = [];
while (gr1.next()) {
 if (gr1.getValue('follow_up_1') == 0) {
if (day == 6 || day == 0) {

            gr1.setValue('monday_notification', true);

            gr1.update();

        } else if ((day == 1 || day == 2 || day == 3 || day == 4 || day == 5)) {
recordFor10Days.push(gr1.getValue('sys_id'));

        }

    }

}

if (recordFor10Days.length > 0) {
gs.eventQueue("x_snc_caase.ForFollowup", '', '', recordFor10Days, '1');
}
var gr2 = new GlideRecord('case');

gr2.addQuery('state', 'In Progress');

gr2.addQuery('inprogress_time', '!=', ' ');

gr2.addQuery('inprogress_time', '>=', gs.daysAgoStart(20));

gr2.query();

var recordFor20Days = [];

while (gr2.next) {

  // // Check for 20 days if the 2nd notification needs to be sent

    if (gr2.getValue('follow_up_2') == 0) {

if (day == 6 || day == 0) {

            gr2.setValue('monday_notification', true);

            gr2.update();

        } else if ((day == 1 || day == 2 || day == 3 || day == 4 || day == 5)) {

 recordFor20Days.push(gr2.getValue('sys_id'));

        }

    }
}

if (recordFor20Days.length > 0) {

    gs.info("followup2");
gs.eventQueue("x_snc_case.ForFollowup", '', '', recordFor20Days, '2');

}
var gr3 = new GlideRecord('case');
gr3.addQuery('state', 'In Progress');

gr3.addQuery('inprogress_time', '!=', ' ');

gr3.addQuery('inprogress_time', '>=', gs.daysAgoStart(30));

var recordFor30Days = [];

while (gr3.next()) {

    // Check for 30 days if the 3rd notification needs to be sent

    if (gr3.getValue('follow_up_3') == 0) {

        // For 30 days

        if (day == 6 || day == 0) {

            gr3.setValue('monday_notification', true);

            gr3.update();

        } else if ((day == 1 || day == 2 || day == 3 || day == 4 || day == 5)) {
 recordFor30Days.push(gr3.getValue('sys_id'));

        }

    }

}

if (recordFor30Days.length > 0) {

    gs.info("followup3");
gs.eventQueue("x_snc_case.ForFollowup", '', '', recordFor30Days, '3');

}

//Final Notification

var gr4 = new GlideRecord('case');

gr4.addQuery('state', 'In Progress');

gr4.addQuery('inprogress_time', '!=', ' ');

gr4.addQuery('inprogress_time', '>=', gs.daysAgoStart(40));

gr4.query();

var recordFor40Days = [];

while (gr4.next()) {

    if (gr4.getValue('follow_up_4') == 0) {

        if (day == 6 || day == 0) {

            gr4.setValue('monday_notification', true);

            gr4.update();

        } else if ((day == 1 || day == 2 || day == 3 || day == 4 || day == 5)) {
recordFor40Days.push(gr4.getValue('sys_id'));
 }

    }

}

if (recordFor40Days.length > 0) {
gs.eventQueue("x_snc_case.ForFollowup", '', '', recordFor40Days, '4');

}

Can anyone help me on the script part.

 

12 REPLIES 12

@Balaji Munusamy ,

 

now I see.

 

This line of code is running the infinite loop

 

if (recordFor10Days.length > 0) {
gs.eventQueue("x_snc_caase.ForFollowup", '', '', recordFor10Days, '1');
}

 

Because once the system find some records it will push them into array but never clear them out. Your condition is saying that if this array is not empty which it never will be since there always be some records.

 

Try to add this line of code

if (recordFor10Days.length > 0) {
gs.eventQueue("x_snc_caase.ForFollowup", '', '', recordFor10Days, '1');

recordFor10Days = [];
}

 

so it basically says that run this line of code if the recordFor10Days has some record, please trigger this event but then clear the array.

 

@IronPotato 

Let me try this. And in my script for each follow-up i have used separate while loop. Is it correct?

why because once the First follow-up condition satisfies  then other follow-up not executing

@Balaji Munusamy ,

 

let me know whether it worked.

 

As for the loops. To be honest I would separate this logic into 3 different script files with 3 different registered events to have it separated and easily modified in the future.

@IronPotato 

This works!

 if (recordFor10Days.length > 0) {
gs.eventQueue("x_snc_caase.ForFollowup", '', '', recordFor10Days, '1');

recordFor10Days = [];
}

Can you suggest better solution for the loops? 

@Balaji Munusamy ,

 

glad that helped. Please consider marking this as helpful please.

 

As for the loops. I'm suggesting separating the logic into 3 different business scripts.