- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2025 03:08 AM - edited ‎03-07-2025 03:10 AM
I have a script where we are checking the Computer table and seeing if we have associated records via MAC and if we do, we want to update some custom fields
In the example below, we are searching 5 computers, which should match with 2 records and 3 that have no linked table so should not update.
The 2 are working fine and updating correctly, the 3 are adding undefined. Is there a way I can script this better, so where no match is found there is no update, so no undefined added.
Thanks in Advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2025 03:37 AM
try this -> ensure you give the correct table name for some_table
var Query = "mac_address=xxxxx1^ORmac_address=xxxxx2^ORmac_address=xxxxx3^ORmac_address=xxxxx4^ORmac_address=xxxxx5";
var gr = new GlideRecord('cmdb_ci_computer');
gr.addEncodedQuery(Query);
gr.query();
while (gr.next()) {
var end = new GlideRecord('some_table'); // Replace 'some_table' with the actual table name
end.addQuery('name', gr.mac_address);
end.setLimit(1);
end.query();
if (end.next()) {
var fabric = end.fabric_interface;
if (fabric) {
fabric = fabric.substring(0, fabric.indexOf('||S'));
}
var inter = end.fabric_interface;
if (inter) {
inter = inter.substring(0, inter.indexOf(','));
}
var s = end.fabric_interface;
if (s) {
s = s.substring(s.indexOf("|S") + 1);
s = s.substring(0, s.indexOf(","));
}
var v = end.dn;
if (v) {
v = v.substring(v.indexOf("epg-") + 4);
v = v.substring(0, v.indexOf("-EPG"));
}
if (end.sys_id) {
gr.setValue('u_aci_endpoint', end.sys_id);
}
if (s) {
gr.setValue('u_aci_switch_port', s);
}
if (v) {
gr.setValue('u_vlan', v);
}
gr.update();
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2025 03:37 AM
try this -> ensure you give the correct table name for some_table
var Query = "mac_address=xxxxx1^ORmac_address=xxxxx2^ORmac_address=xxxxx3^ORmac_address=xxxxx4^ORmac_address=xxxxx5";
var gr = new GlideRecord('cmdb_ci_computer');
gr.addEncodedQuery(Query);
gr.query();
while (gr.next()) {
var end = new GlideRecord('some_table'); // Replace 'some_table' with the actual table name
end.addQuery('name', gr.mac_address);
end.setLimit(1);
end.query();
if (end.next()) {
var fabric = end.fabric_interface;
if (fabric) {
fabric = fabric.substring(0, fabric.indexOf('||S'));
}
var inter = end.fabric_interface;
if (inter) {
inter = inter.substring(0, inter.indexOf(','));
}
var s = end.fabric_interface;
if (s) {
s = s.substring(s.indexOf("|S") + 1);
s = s.substring(0, s.indexOf(","));
}
var v = end.dn;
if (v) {
v = v.substring(v.indexOf("epg-") + 4);
v = v.substring(0, v.indexOf("-EPG"));
}
if (end.sys_id) {
gr.setValue('u_aci_endpoint', end.sys_id);
}
if (s) {
gr.setValue('u_aci_switch_port', s);
}
if (v) {
gr.setValue('u_vlan', v);
}
gr.update();
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2025 03:42 AM - edited ‎03-07-2025 03:42 AM
@Ankur Bawiskar Thanks you are a superstar, that is working!!!