Skip to content

Commit 64d0268

Browse files
AutoPopulate Missing Manufacturer (#2402)
* Create TechTrekwithAJ-PopulateManufacturer.js This code will help to populate the Manufacturer if that empty on CMDB_CI table * Create TechTrekwithAJ-PopulateManufacturerReadME.md * Delete CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md * Update TechTrekwithAJ-PopulateManufacturer.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. Predefined Mapping: The script starts with a list of known model names and their corresponding manufacturer names.For example, a model called ThinkPad T14 is made by Lenovo, and MacBook Pro 16 is made by Apple 2. Look Up Manufacturer: * It defines a function that looks up the manufacturer’s record in the core_company table (based on the name) and gets its unique ID (sys_id). 3. Find CIs Missing a Manufacturer: * The script goes through all CIs in the cmdb_ci table where the manufacturer field is empty. 4. Update Missing Manufacturer: * For each of those CIs: * It checks the model name. * If the model is in the predefined mapping: * It looks up the correct manufacturer in the core_company table. * It updates the CI record by setting the manufacturer field with the correct sys_id. * It also logs that the update was successful. * If the manufacturer is not found in the system, it logs a warning. 5. Final Log: * After going through all matching CIs, it logs how many records were successfully updated. * 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). * Create TechTrekwithAJ-AutoPopulateMissingManufacturerReadME.md This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database). * Delete CMDB/TechTrekwithAJ-PopulateManufacturer.js * Rename TechTrekwithAJ-AutoPopulateMissingManufacturerReadME.md to ReadME.md
1 parent f8e1770 commit 64d0268

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database).
2+
3+
1. Predefined Mapping:
4+
The script starts with a list of known model names and their corresponding manufacturer names.For example, a model called ThinkPad T14 is made by Lenovo, and MacBook Pro 16 is made by Apple
5+
2. Look Up Manufacturer:
6+
* It defines a function that looks up the manufacturer’s record in the core_company table (based on the name) and gets its unique ID (sys_id).
7+
3. Find CIs Missing a Manufacturer:
8+
* The script goes through all CIs in the cmdb_ci table where the manufacturer field is empty.
9+
4. Update Missing Manufacturer:
10+
* For each of those CIs:
11+
* It checks the model name.
12+
* If the model is in the predefined mapping:
13+
* It looks up the correct manufacturer in the core_company table.
14+
* It updates the CI record by setting the manufacturer field with the correct sys_id.
15+
* It also logs that the update was successful.
16+
* If the manufacturer is not found in the system, it logs a warning.
17+
5. Final Log:
18+
* After going through all matching CIs, it logs how many records were successfully updated.
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)