Skip to content

Commit a3e54e0

Browse files
Create TechTrekwithAJ-AutoPopulateMissingManufacturer.js
This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database).
1 parent 8dc35c2 commit a3e54e0

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Predefined mapping of model names to manufacturer sys_ids or names
2+
var modelManufacturerMap = {
3+
'ThinkPad T14': 'Lenovo',
4+
'EliteBook 840': 'HP',
5+
'Latitude 7420': 'Dell',
6+
'MacBook Pro 16': 'Apple',
7+
'Surface Pro 7': 'Microsoft'
8+
};
9+
10+
// Function to get the manufacturer sys_id from the manufacturer name
11+
function getManufacturerSysId(name) {
12+
var mfgGR = new GlideRecord('core_company');
13+
mfgGR.addQuery('name', name);
14+
mfgGR.query();
15+
if (mfgGR.next()) {
16+
return mfgGR.getUniqueValue();
17+
}
18+
return null;
19+
}
20+
21+
// GlideRecord to loop through Configuration Items where manufacturer is empty
22+
var ciGR = new GlideRecord('cmdb_ci');
23+
ciGR.addNullQuery('manufacturer'); // Manufacturer is empty
24+
ciGR.query();
25+
26+
var updatedCount = 0;
27+
28+
while (ciGR.next()) {
29+
var model = ciGR.model.name.toString(); // Get model name
30+
31+
if (model && modelManufacturerMap.hasOwnProperty(model)) {
32+
var manufacturerName = modelManufacturerMap[model];
33+
var manufacturerSysId = getManufacturerSysId(manufacturerName);
34+
35+
if (manufacturerSysId) {
36+
ciGR.manufacturer = manufacturerSysId;
37+
ciGR.update();
38+
updatedCount++;
39+
gs.info('Updated CI: ' + ciGR.name + ' | Model: ' + model + ' | Manufacturer: ' + manufacturerName);
40+
} else {
41+
gs.warn('Manufacturer "' + manufacturerName + '" not found in core_company table.');
42+
}
43+
}
44+
}
45+
46+
gs.info('✅ Auto-populate complete. Total CIs updated: ' + updatedCount);

0 commit comments

Comments
 (0)