Function and array help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 09:10 AM
Hi all,
I am struggling with this code. I am trying to collect the sys_ids of locations in the shipsLoc array, and then in the function use this to look at the cmdb_ci_server name (we have a naming convention which means the location of the device will be in the name as a three digit identifier, in this case either 'arc' or 'azu' (server name example: 123-ARC-001)).
So I want to look for this 3 digit identifier in the CI name, compare this to the three digit shortened name in the location table, and populate the record based on the same 3 digit identifier.
Please could someone kindly have a look at the code and tell me where I am going wrong, please?
It currently does populate the location field, just not with the correct location.
var ciRec = new GlideRecord('cmdb_ci_server');
function findShipLocation() {
var shipsLoc = [];
var abc = new GlideRecord('cmn_location');
abc.addEncodedQuery("cmn_location_type=Ship^u_shortened_nameISNOTEMPTY");
abc.query();
while (abc.next()) {
shipsLoc.push({
'name': abc.u_shortened_name + '',
'sys_id': abc.sys_id + ''
});
}
var myOutput = JSON.stringify(shipsLoc);
// gs.info(myOutput);
for (var i = 0; i < shipsLoc.length; i++) {
if (shipsLoc[i].name == ciRec.name.toLowerCase().indexOf('arc') > -1) {
ciRec.location = shipsLoc[i].sys_id;
} else if (shipsLoc[i].name.indexOf('azu') > -1 == ciRec.name.toLowerCase().indexOf('azu') > -1) {
ciRec.location = shipsLoc[i].sys_id;
}
ciRec.company = ciRec.location.company;
}
}
ciRec.addEncodedQuery("operational_statusNOT IN2,6");
ciRec.query();
while (ciRec.next()) {
ciRec.autoSysFields(false); //not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, sys_created_on
ciRec.setWorkflow(false);
if (!ciRec.assigned_to && ciRec.support_group) {
//Update if record has no Assigned To information but does have Support Group information.
ciRec.department = 'e87f6c331ba6b5508d18ec69b04bcb4b';
ciRec.owned_by = ciRec.support_group.u_cmdb_owner; //Set owned by to support group cmdb owner
findShipLocation();
} else if (ciRec.assigned_to && ciRec.support_group) {
//Update if record has Assigned To and Support Group information
ciRec.department = ciRec.assigned_to.department; // Set CI Department to Assigned To Department
ciRec.owned_by = ciRec.support_group.u_cmdb_owner; //set owned by to support group CMDB owner
findShipLocation();
} else if (ciRec.assigned_to && !ciRec.support_group) {
//Update if record has Assigned To and no Support Group information
ciRec.department = ciRec.assigned_to.department; // Set CI Department to Assigned To Department
findShipLocation();
} else {
findShipLocation();
ciRec.department = 'e87f6c331ba6b5508d18ec69b04bcb4b';
}
ciRec.update();
}
gs.info("Daily BR [server] Set assigned to attributes complete");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 09:29 AM
Hi @Will25
The issue with your code seems to be in the logic of how you're comparing the location identifier with the CI names and then assigning the location. I’ll walk you through the corrected and optimized version of your code.
var ciRec = new GlideRecord('cmdb_ci_server');
function findShipLocation(ciRec) {
var shipsLoc = [];
// Fetch locations with a non-empty shortened name
var abc = new GlideRecord('cmn_location');
abc.addEncodedQuery("cmn_location_type=Ship^u_shortened_nameISNOTEMPTY");
abc.query();
// Store the location names and sys_ids in an array
while (abc.next()) {
shipsLoc.push({
'name': abc.u_shortened_name.toLowerCase(), // Convert to lowercase for case-insensitive comparison
'sys_id': abc.sys_id + ''
});
}
// Iterate through the stored locations and compare with CI name
for (var i = 0; i < shipsLoc.length; i++) {
var locationName = shipsLoc[i].name;
// Check if the CI name contains the location name
if (ciRec.name.toLowerCase().indexOf(locationName) > -1) {
ciRec.location = shipsLoc[i].sys_id;
break; // Exit loop once the correct location is found
}
}
}
ciRec.addEncodedQuery("operational_statusNOT IN2,6");
ciRec.query();
while (ciRec.next()) {
ciRec.autoSysFields(false); // Don't update sys_updated_by, sys_updated_on, etc.
ciRec.setWorkflow(false); // Disable workflow
findShipLocation(ciRec); // Find and set the location based on CI name
if (!ciRec.assigned_to && ciRec.support_group) {
ciRec.department = 'e87f6c331ba6b5508d18ec69b04bcb4b'; // Static department ID
ciRec.owned_by = ciRec.support_group.u_cmdb_owner; // Set owned by to support group CMDB owner
} else if (ciRec.assigned_to && ciRec.support_group) {
ciRec.department = ciRec.assigned_to.department; // Set CI department to Assigned To's department
ciRec.owned_by = ciRec.support_group.u_cmdb_owner;
} else if (ciRec.assigned_to && !ciRec.support_group) {
ciRec.department = ciRec.assigned_to.department;
} else {
ciRec.department = 'e87f6c331ba6b5508d18ec69b04bcb4b'; // Fallback department ID
}
ciRec.update(); // Save changes to the record
}
gs.info("Daily BR [server] Set assigned to attributes complete");
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2024 02:14 AM
Thank you, this worked a treat! Appreciate the help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2024 05:58 AM
Hi @Wilwod @Will25
If this Solved your problem please Mark as Accepted and help us to close the thread and groww together
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 09:38 AM - edited 08-29-2024 09:41 AM
Whenever you push a sys_id to an array you must force it to a string or you will end up with an array of the same sys_id. I don't know if that's also the case for an array of objects, and/or since you are appending it with a space, but it can't hurt
'sys_id': abc.sys_id.toString() + ''
If you were seeing the data in the myOutput log that has location names that match the sys_ids, then disregard but keep this in mind for the future.
Beyond this, follow the logic through the script by adding more logs so that you can see which if conditions are satisfied, script variable values, etc. then you will see where it is going wrong.