- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2020 07:08 AM
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");
Solved! Go to Solution.
- Labels:
-
Orchestration (ITOM)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2020 07:51 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2020 07:42 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2020 07:51 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2020 12:15 AM
Hi Ankur
Thank you for the support.