The CreatorCon Call for Content is officially open! Get started here.

code is not working as expected

SAS21
Tera Guru
Its about calculating the no of pending days for an INC and if the incident is in pending, sending the same notification, like a followup to the user. The interval of the email is 2 business days and have to send 3 notifications. Hence took the values as 2, 4, 6 in the array.
 
var curDate = new GlideDateTime();
var DaysArray = [2,4,6];  // The no of days the INC is in pending state
var Days;
var gr = new GlideRecord('u_incident');
gr.addEncodedQuery('assignment_group=a57e93e38bf34e10de76b710fe4ee3ea^u_status=Pending');
gr.query();
while (gr.next()) {
// Calculating the Business days here using a script include and holding that value in 'Days' which is working as expected, not working is the below for loop.
    for (var i = 0; i < DaysArray.length; i++) {
gs.info(" DaysArray before if" + DaysArray[i]);
        if (Days == DaysArray[i]) {
gs.info("DaysArray in if" + DaysArray[i]);
            gs.eventQueue('event name', gr, '');
gs.info("in event loop");
 
        }
    }
}    
 
   I have tried with a single record which has pending days as 2 . The email triggered . But its not iterating and not entering the if  loop, for all the records , which has days value 2, 4, 6.
 
if i missed something/ Instead of hardcoding the days cant we use 'for' ? 
3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

I don't see any comparison with incident date fields.

Your script is not calculating days. What should be the criteria for no of Days? From Last updated or from Created date?

If last updated then you need t record the date in one field when ticket state changes to Pending and calculate the duration of Days from that date till today.

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi Anil,

here is the code. The days part is working as expected. but comparison in for loop is not iterating for all the records .

 

I have tried with a single record . it worked. 

 

var curDate = new GlideDateTime();
var DaysArray = [3,5,7];
var pendingDays;
var gr = new GlideRecord('u_incident');
gr.addEncodedQuery('assignment_group=a57e93e38bf34e10de76b710fe4ee3ea^u_status=Pending');
gr.query();
while (gr.next()) {
var pendingStartDate = gr.u_pend_start_date; (This field holds the date when the incident is inserted/updated to Pending status via a BR. Just making use of the existing field)

for (var i = 0; i < DaysArray.length; i++) {
gs.info(" DaysArray before if" + DaysArray[i]);
if (pendingDays == DaysArray[i]) {
gs.eventQueue('event name', gr, '');
}
}
}

Hi,

I couldn't test your script because of some script include calls you have made.

I believe you need to change this line:

dur.push(utils.getBDDuration(pendingStartDate, curDate)); // calculating the Business Days Duration

to below:

var dur = utils.getBDDuration(pendingStartDate, curDate); // calculating the Business Days Duration
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande