From ad4324f630d36dd730ed5b1ed0700428314dfc26 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 15:40:38 +0530 Subject: [PATCH 1/4] Create TechTrekwithAJ-PopulateManufacturer.js This code will help to populate the Manufacturer if that empty on CMDB_CI table --- CMDB/TechTrekwithAJ-PopulateManufacturer.js | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 CMDB/TechTrekwithAJ-PopulateManufacturer.js diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturer.js b/CMDB/TechTrekwithAJ-PopulateManufacturer.js new file mode 100644 index 0000000000..5fbb598dff --- /dev/null +++ b/CMDB/TechTrekwithAJ-PopulateManufacturer.js @@ -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); From 88951259d69802e97cf6e00950b247cc734bbc4e Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 15:43:15 +0530 Subject: [PATCH 2/4] Create TechTrekwithAJ-PopulateManufacturerReadME.md --- ...chTrekwithAJ-PopulateManufacturerReadME.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md b/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md new file mode 100644 index 0000000000..a02a97889d --- /dev/null +++ b/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md @@ -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. From 176e80dceacd9800398956fd3ce3b3f50e61519e Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 15:52:23 +0530 Subject: [PATCH 3/4] Delete CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md --- ...chTrekwithAJ-PopulateManufacturerReadME.md | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md b/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md deleted file mode 100644 index a02a97889d..0000000000 --- a/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md +++ /dev/null @@ -1,23 +0,0 @@ -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. From ef7e37e6c416df2e4383da1b6ee615aa05b4d476 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 16:37:15 +0530 Subject: [PATCH 4/4] Update TechTrekwithAJ-PopulateManufacturer.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- CMDB/TechTrekwithAJ-PopulateManufacturer.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturer.js b/CMDB/TechTrekwithAJ-PopulateManufacturer.js index 5fbb598dff..4da3c5d987 100644 --- a/CMDB/TechTrekwithAJ-PopulateManufacturer.js +++ b/CMDB/TechTrekwithAJ-PopulateManufacturer.js @@ -44,3 +44,7 @@ while (ciGR.next()) { } 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).