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

while loop is executing only one time though it it inside for loop

Shivani29
Mega Guru

Hi All,

I have a requirement where I need to send 1 notifications to vendor's primary contact when due date is less than equal to 90 days from today. I created scheduled job where first I am getting the vendors list, then unique vendors. After that I am checking the primary contact and inside using while loop. Then calling the event. But while loop is executing only once and not checking records for next vendor. I am badly stuck in it from last 1 week. Below is the code:


var ven = [];
var newVen = [];

var primary=[];
var pven = [];
var now = new GlideDateTime();
gs.info('Now '+now);

//glide record vendor risk assessment table to get the vendor list
var gr = new GlideRecord('sn_vdr_risk_asmt_assessment');
gr.addEncodedQuery('state=2^vendor.status=active^questionnaire_due_dateBETWEENjavascript:gs.beginningOfLast3Months()@javascript:gs.endOfToday()');
gr.query();
gs.log('Total Number --'+gr.getRowCount()); // getting row count
while(gr.next()){
ven.push(gr.getValue('vendor'));
au = new ArrayUtil();
//var newVen = [];
newVen = au.unique(ven); //getting unique vendors
}
gs.log('Vendor SM----'+ven);
gs.log('vendor SM length--'+ven.length);
gs.log('new ven SM length--'+newVen.length);
var count = newVen.length;
gs.log('count newVen '+count);
var i=0;
//glide record contact table to get primary contact email id to send the notification
var contact = new GlideRecord('vm_vdr_contact');
for(i=0;i<=count;i=i+1){
gs.log("inside for loop"); //getting correct number of time (667)
contact.addEncodedQuery('company='+newVen[i]+'^primary_contact=true');
contact.query();
while(contact.next()){
gs.log('inside while sm---new'); // it is giving log only for 1 vendor
primary.push(contact.getValue('email'));

}
gs.eventQueue("vendor.event",gr,newVen[i],primary[i]);
}
gs.log("pven sm---"+primary);
gs.log("pven length sm----"+primary.length);

 

Kindly help as I am stuck on this from more than a week.

Regards,
Shivani

1 ACCEPTED SOLUTION

Shivani29
Mega Guru

Hi,

This issue is fixed by replacing while loop inside for loop with if loop.

Regards,
Shivani

View solution in original post

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hi,

Your eventQueue line is outside your while loop. You're closing the bracket } from that while loop so it's not going to loop through any GlideRecord results. You'd need to move it up a line and close the bracket after it.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Shivani29
Mega Guru

Hi,

Tried this as well, but no luck. Problem is that it is going inside while loop only for 1 vendor and for remaining vendors it is not going inside while itself.

Regards,
Shivani

Hi Shivani,

Can you update the script as below, it seems you are calling the gr object which does not make any value.

var ven = [];
var newVen = [];

var primary=[];
var pven = [];
var now = new GlideDateTime();
gs.info('Now '+now);

//glide record vendor risk assessment table to get the vendor list
var gr = new GlideRecord('sn_vdr_risk_asmt_assessment');
gr.addEncodedQuery('state=2^vendor.status=active^questionnaire_due_dateBETWEENjavascript:gs.beginningOfLast3Months()@javascript:gs.endOfToday()');
gr.query();
gs.log('Total Number --'+gr.getRowCount()); // getting row count
while(gr.next()){
ven.push(gr.getValue('vendor'));
au = new ArrayUtil();
//var newVen = [];
newVen = au.unique(ven); //getting unique vendors
}
gs.log('Vendor SM----'+ven);
gs.log('vendor SM length--'+ven.length);
gs.log('new ven SM length--'+newVen.length);
var count = newVen.length;
gs.log('count newVen '+count);
var i=0;
//glide record contact table to get primary contact email id to send the notification
var contact = new GlideRecord('vm_vdr_contact');
for(i=0;i<=count;i=i+1){
gs.log("inside for loop"); //getting correct number of time (667)
contact.addEncodedQuery('company='+newVen[i]+'^primary_contact=true');
contact.query();
while(contact.next()){
gs.log('inside while sm---new'); // it is giving log only for 1 vendor
primary.push(contact.getValue('email'));
}
gs.eventQueue("vendor.event",newVen[i],primary[i],'');
}
gs.log("pven sm---"+primary);
gs.log("pven length sm----"+primary.length);

Let me know if you face any issues.

Regards,

Deepankar Mathur

 

Hi Deepankar,

No luck. It is triggering the event but not sending the notification at all. The problem what I am facing is for loop is running 667 times, which is correct, so logically while loop also run for these many vendors, but it is running only for 1 vendor.

Regards,
Shivani