Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions CMDB/TechTrekwithAJ-PopulateManufacturer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Predefined mapping of model names to manufacturer sys_ids or names
var modelManufacturerMap = {
'ThinkPad T14': 'Lenovo',
'EliteBook 840': 'HP',
'Latitude 7420': 'Dell',
'MacBook Pro 16': 'Apple',
'Surface Pro 7': 'Microsoft'
};

// Function to get the manufacturer sys_id from the manufacturer name
function getManufacturerSysId(name) {
var mfgGR = new GlideRecord('core_company');
mfgGR.addQuery('name', name);
mfgGR.query();
if (mfgGR.next()) {
return mfgGR.getUniqueValue();
}
return null;
}

// GlideRecord to loop through Configuration Items where manufacturer is empty
var ciGR = new GlideRecord('cmdb_ci');
ciGR.addNullQuery('manufacturer'); // Manufacturer is empty
ciGR.query();

var updatedCount = 0;

while (ciGR.next()) {
var model = ciGR.model.name.toString(); // Get model name

if (model && modelManufacturerMap.hasOwnProperty(model)) {
var manufacturerName = modelManufacturerMap[model];
var manufacturerSysId = getManufacturerSysId(manufacturerName);

if (manufacturerSysId) {
ciGR.manufacturer = manufacturerSysId;
ciGR.update();
updatedCount++;
gs.info('Updated CI: ' + ciGR.name + ' | Model: ' + model + ' | Manufacturer: ' + manufacturerName);
} else {
gs.warn('Manufacturer "' + manufacturerName + '" not found in core_company table.');
}
}
}

gs.info('Auto-populate complete. Total CIs updated: ' + updatedCount);
23 changes: 23 additions & 0 deletions CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.
Loading