We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

Shivani29
Mega Guru

Hi,

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

Regards,
Shivani