From 9636417606c266f6882049ac4d47a74c174f544f Mon Sep 17 00:00:00 2001 From: ChandBasha-code Date: Sat, 11 Oct 2025 14:46:06 +0530 Subject: [PATCH 1/2] Create ModelManufacture.README.md Model Manufacture Creation --- .../ModelManufacture.README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Integration/Import Sets/Import sets overview/ModelManufacture.README.md diff --git a/Integration/Import Sets/Import sets overview/ModelManufacture.README.md b/Integration/Import Sets/Import sets overview/ModelManufacture.README.md new file mode 100644 index 0000000000..616626a4d4 --- /dev/null +++ b/Integration/Import Sets/Import sets overview/ModelManufacture.README.md @@ -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. From 5c3467f6b04e6348de6b65d37ac1ac76c84ed1e6 Mon Sep 17 00:00:00 2001 From: ChandBasha-code Date: Sat, 11 Oct 2025 14:46:44 +0530 Subject: [PATCH 2/2] Create ModelManufacture.js --- .../Import sets overview/ModelManufacture.js | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Integration/Import Sets/Import sets overview/ModelManufacture.js diff --git a/Integration/Import Sets/Import sets overview/ModelManufacture.js b/Integration/Import Sets/Import sets overview/ModelManufacture.js new file mode 100644 index 0000000000..d678de1478 --- /dev/null +++ b/Integration/Import Sets/Import sets overview/ModelManufacture.js @@ -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; + } + }