Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
When importing or processing Configuration Items (CIs), especially hardware assets, missing model or manufacturer data can cause CI creation failures or incomplete relationships.
This script handles that automatically by:
* Checking if a manufacturer already exists in the core_company table.
* Checking if a model already exists in the cmdb_model hierarchy.
* Creating the manufacturer and/or model records if they are missing.

How It Works

1. Extracts model and manufacturer names from the data source (source.u_model and source.u_manufacturer).
2. Calls getOrCreateManufacturer():
* Searches the core_company table by name.
* Creates a new company record if not found.
3. Calls getOrCreateModel():
* Searches the cmdb_model table (including child tables).
* If no match exists, inserts a new record in cmdb_hardware_product_model.
4. Logs each action (found, created, or failed) for debugging and auditing.
70 changes: 70 additions & 0 deletions Integration/Import Sets/Import sets overview/ModelManufacture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var modelName = source.u_model;
var manufacturerName = source.u_manufacturer;

function getOrCreateModel(modelName, manufacturerName) {
try {
var manufacturerSysId = getOrCreateManufacturer(manufacturerName);
if (!manufacturerSysId) {
gs.error("MODEL SCRIPT: Failed to find or create manufacturer: " + manufacturerName);
return null;
}

// Query cmdb_model to check if any existing model (including child tables) exists
var modelGr = new GlideRecord('cmdb_model');
modelGr.addQuery('name', modelName);
modelGr.addQuery('manufacturer', manufacturerSysId);
modelGr.query();

if (modelGr.next()) {
gs.info("MODEL SCRIPT: Found existing model: " + modelGr.getUniqueValue());
return modelGr.getUniqueValue();
} else {
// Create in child table: cmdb_hardware_product_model
var newModel = new GlideRecord('cmdb_hardware_product_model');
newModel.initialize();
newModel.setValue('name', modelName);
newModel.setValue('manufacturer', manufacturerSysId);
var newModelSysId = newModel.insert();

if (newModelSysId) {
gs.info("MODEL SCRIPT: Created new hardware model: " + newModelSysId);
return newModelSysId;
} else {
gs.error("MODEL SCRIPT: Failed to insert new model for " + modelName);
return null;
}
}
} catch (e) {
gs.error("MODEL SCRIPT: Error in getOrCreateModel(): " + (e.message || e));
return null;
}
}

function getOrCreateManufacturer(name) {
try {
var companyGr = new GlideRecord('core_company');
companyGr.addQuery('name', name);
companyGr.query();

if (companyGr.next()) {
gs.info("MODEL SCRIPT: Found manufacturer: " + companyGr.getUniqueValue());
return companyGr.getUniqueValue();
} else {
companyGr.initialize();
companyGr.setValue('name', name);
//companyGr.setValue('manufacturer', true); // Ensure it’s marked as manufacturer
var newMfrSysId = companyGr.insert();

if (newMfrSysId) {
gs.info("MODEL SCRIPT: Created new manufacturer: " + newMfrSysId);
return newMfrSysId;
} else {
gs.error("MODEL SCRIPT: Failed to insert new manufacturer: " + name);
return null;
}
}
} catch (e) {
gs.error("MODEL SCRIPT: Error in getOrCreateManufacturer(): " + (e.message || e));
return null;
}
}
Loading