Skip to content
Closed
Changes from all 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
50 changes: 50 additions & 0 deletions CMDB/TechTrekwithAJ-PopulateManufacturer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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);



//This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database).
Loading