From 80252a85c668c9b1c98fc95e508c127be7c37892 Mon Sep 17 00:00:00 2001 From: KUSHAL <81311270+KUSHAL1912@users.noreply.github.com> Date: Tue, 28 Oct 2025 02:57:23 +0530 Subject: [PATCH 1/2] Create post-script.js Created folder and added post-script file to the folder --- .../post-script.js | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes/post-script.js diff --git a/Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes/post-script.js b/Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes/post-script.js new file mode 100644 index 0000000000..6aa1c6ba0b --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes/post-script.js @@ -0,0 +1,60 @@ +var impGR = new GlideRecord(data_source.import_set_table_name); +impGR.addQuery('sys_import_set', import_set.sys_id); +impGR.query(); + +while (impGR.next()) { + var classDisplayName = impGR.getValue('u_class');//column name as in staging table + var supportGroupName = impGR.getValue('u_support_group');//column name as in staging table + var managedByGroupName = impGR.getValue('u_managed_by_group');//column name as in staging table + + if (!classDisplayName) { + gs.warn('[Import] Skipping row with empty class.'); + continue; + } + + var classTable = ''; + var dbObjGR = new GlideRecord('sys_db_object'); + dbObjGR.addQuery('label', classDisplayName); +dbObjGR.addEncodedQuery('nameSTARTSWITHu_cmdb_ci^ORnameSTARTSWITHcmdb_ci');//custom CMDB table names are prepended with u_cmdb_ci + dbObjGR.query(); + if (dbObjGR.next()) { + + classTable = dbObjGR.getValue('name'); + } + + if (!classTable) { + gs.warn('[Import] Could not find table for class: ' + classDisplayName); + continue; + } + + var supportGroupId = ''; + var managedByGroupId = ''; + + var groupGR = new GlideRecord('sys_user_group'); + groupGR.addQuery('name', 'IN', supportGroupName + ',' + managedByGroupName); + groupGR.query(); + while (groupGR.next()) { + var name = groupGR.getValue('name'); + if (name === supportGroupName) supportGroupId = groupGR.getUniqueValue(); + if (name === managedByGroupName) managedByGroupId = groupGR.getUniqueValue(); + } + + if (!supportGroupId || !managedByGroupId) { + gs.warn('[Import] Missing group sys_id for: ' + supportGroupName + ' or ' + managedByGroupName); + continue; + } + + + var ciGR = new GlideRecord(classTable); + ciGR.addEncodedQuery('Optional-Filters'); + ciGR.query(); + if (!ciGR.hasNext()) { + gs.warn('[Import] No CI found in ' + classTable + ' with name: ' + classDisplayName); + } + + while (ciGR.next()) { + ciGR.setValue('support_group', supportGroupId); + ciGR.setValue('managed_by_group', managedByGroupId); + ciGR.update(); + } +} From 84cc4dd7d1ef762236a71ac8abed35d84554cebb Mon Sep 17 00:00:00 2001 From: KUSHAL <81311270+KUSHAL1912@users.noreply.github.com> Date: Tue, 28 Oct 2025 02:58:18 +0530 Subject: [PATCH 2/2] Uploaded Readme file Added ReadMe file to the folder --- .../ReadMe.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes/ReadMe.md diff --git a/Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes/ReadMe.md b/Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes/ReadMe.md new file mode 100644 index 0000000000..15bdb309b2 --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes/ReadMe.md @@ -0,0 +1,18 @@ +This is a Post-Import Script designed for a Scheduled Import . Its purpose is to cleanly map and update the Support Group and Managed By Group fields on Configuration Items (CIs) after data has been loaded into the staging table. + +Script Functionality +The script iterates through the records of an Import Set and performs the following core steps for each row: + + i)Extract Data: Reads the CI Class Display Name (u_class), Support Group Name (u_support_group), and Managed By Group Name (u_managed_by_group) from the staging table record. + + ii)Validate Class: Uses the u_class(name from staging table) value to look up and confirm the correct target CMDB table name (e.g., finding cmdb_ci_server from the display name "Server"). + + iii)Resolve Groups: Finds the system-unique sys_ids for both the Support Group and Managed By Group names. + + iv)Update CIs: Queries the determined CMDB table, filters the CIs based on the Optional-Filters placeholder, and sets the support_group and managed_by_group fields using the resolved sys_ids. + +**Points to note : +1) The schedule-import should be linked to a Data Source which has the spreadsheet attached(where groups and classes info is present) +2) This script populates the groups based on Group Name given in the spreadsheet(Make sure they are present in the instance and are following the appropriate naming convention) +3) The script provided is a post script ,which executes after the data is imported.** +