"While loop" is getting stop.

Prasnajeet1
Giga Guru

HI All

I have written one fix script and when it is executing "while loop" is getting stop after the first record got updated. When the first record got updated with gr.update() while loop is getting terminated. Can someone please help to find the reason and how we can avoid that. 

var status_record = 0;
var mac_record = 0;

var gr = new GlideRecord("cmdb_ci_network_adapter");
gr.addEncodedQuery('mac_addressLIKE-^install_status!=100');
gr.query();
gs.log("Total NA to be corrected"+gr.getRowCount(),"mac_log");

while (gr.next()) {
var tmp_mac = gr.mac_address.replace(/-/g, ':');
var tmp_ip = gr.ip_address;

var mac = new GlideRecord('cmdb_ci_network_adapter');
mac.addEncodedQuery('mac_address=' + tmp_mac + '^ip_address=' + tmp_ip + '^install_status!=100');
mac.query();
gs.log("Total mac to be corrected"+mac.getRowCount(),"mac_log");

if(mac.next())
{
gr.install_status = 100;
gr.update();   // When first record getting update "while loop" is getting terminated and it is not updating the complete table.
status_record++;
gs.log("absent" +gr.mac_address, "mac_log" );
}
else
{
gr.mac_address = tmp_mac;
gr.update();  // When first record getting update "while loop" is getting terminated and it is not updating the complete table.
mac_record++;
gs.log("MacUpdated" +gr.mac_address, "mac_log" );
}

}
gs.log("Total Absent record got updated- "+status_record,"mac_log");
gs.log("Total mac of record got updated- "+mac_record,"mac_log");

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please wrap your code with function

use try catch block to know the exception

Check the row count

updated as below and test once

updateRecord();

function updateRecord(){

try{
var status_record = 0;
var mac_record = 0;

var gr = new GlideRecord("cmdb_ci_network_adapter");
gr.addEncodedQuery('mac_addressLIKE-^install_status!=100');
gr.query();
gs.log("Total NA to be corrected"+gr.getRowCount(),"mac_log");

while (gr.next()) {
var tmp_mac = gr.mac_address.replace(/-/g, ':');
var tmp_ip = gr.ip_address;

var mac = new GlideRecord('cmdb_ci_network_adapter');
mac.addEncodedQuery('mac_address=' + tmp_mac + '^ip_address=' + tmp_ip + '^install_status!=100');
mac.query();
gs.log("Total mac to be corrected"+mac.getRowCount(),"mac_log");

if(mac.next()) 
{
gr.install_status = 100;
gr.update();   // When first record getting update "while loop" is getting terminated and it is not updating the complete table.
status_record++;
gs.log("absent" +gr.mac_address, "mac_log" );
} 
else
{
gr.mac_address = tmp_mac;
gr.update();  // When first record getting update "while loop" is getting terminated and it is not updating the complete table.
mac_record++;
gs.log("MacUpdated" +gr.mac_address, "mac_log" );
}

}
gs.log("Total Absent record got updated- "+status_record,"mac_log");
gs.log("Total mac of record got updated- "+mac_record,"mac_log");
}
catch(ex){
	gs.info('Exception'+ex);
}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Does your log show any errors right around the same time as your "Total mac to be corrected..." log?  Have you tried updating the first record manually with the same value as the script to see if there are any business rules or data policies interfering?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please wrap your code with function

use try catch block to know the exception

Check the row count

updated as below and test once

updateRecord();

function updateRecord(){

try{
var status_record = 0;
var mac_record = 0;

var gr = new GlideRecord("cmdb_ci_network_adapter");
gr.addEncodedQuery('mac_addressLIKE-^install_status!=100');
gr.query();
gs.log("Total NA to be corrected"+gr.getRowCount(),"mac_log");

while (gr.next()) {
var tmp_mac = gr.mac_address.replace(/-/g, ':');
var tmp_ip = gr.ip_address;

var mac = new GlideRecord('cmdb_ci_network_adapter');
mac.addEncodedQuery('mac_address=' + tmp_mac + '^ip_address=' + tmp_ip + '^install_status!=100');
mac.query();
gs.log("Total mac to be corrected"+mac.getRowCount(),"mac_log");

if(mac.next()) 
{
gr.install_status = 100;
gr.update();   // When first record getting update "while loop" is getting terminated and it is not updating the complete table.
status_record++;
gs.log("absent" +gr.mac_address, "mac_log" );
} 
else
{
gr.mac_address = tmp_mac;
gr.update();  // When first record getting update "while loop" is getting terminated and it is not updating the complete table.
mac_record++;
gs.log("MacUpdated" +gr.mac_address, "mac_log" );
}

}
gs.log("Total Absent record got updated- "+status_record,"mac_log");
gs.log("Total mac of record got updated- "+mac_record,"mac_log");
}
catch(ex){
	gs.info('Exception'+ex);
}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur

Thank you for the support.