Skip to content

Commit be8de35

Browse files
authored
Add Scheduled Data Import Script for Groups Population in CMDB Classes (#2554)
* Create post-script.js Created folder and added post-script file to the folder * Uploaded Readme file Added ReadMe file to the folder
1 parent 59ddb1e commit be8de35

File tree

2 files changed

+78
-0
lines changed
  • Server-Side Components/Scheduled Jobs/Scheduled Data Import for Groups Population(Support and Managed By) for CMDB Classes

2 files changed

+78
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
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.
2+
3+
Script Functionality
4+
The script iterates through the records of an Import Set and performs the following core steps for each row:
5+
6+
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.
7+
8+
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").
9+
10+
iii)Resolve Groups: Finds the system-unique sys_ids for both the Support Group and Managed By Group names.
11+
12+
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.
13+
14+
**Points to note :
15+
1) The schedule-import should be linked to a Data Source which has the spreadsheet attached(where groups and classes info is present)
16+
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)
17+
3) The script provided is a post script ,which executes after the data is imported.**
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var impGR = new GlideRecord(data_source.import_set_table_name);
2+
impGR.addQuery('sys_import_set', import_set.sys_id);
3+
impGR.query();
4+
5+
while (impGR.next()) {
6+
var classDisplayName = impGR.getValue('u_class');//column name as in staging table
7+
var supportGroupName = impGR.getValue('u_support_group');//column name as in staging table
8+
var managedByGroupName = impGR.getValue('u_managed_by_group');//column name as in staging table
9+
10+
if (!classDisplayName) {
11+
gs.warn('[Import] Skipping row with empty class.');
12+
continue;
13+
}
14+
15+
var classTable = '';
16+
var dbObjGR = new GlideRecord('sys_db_object');
17+
dbObjGR.addQuery('label', classDisplayName);
18+
dbObjGR.addEncodedQuery('nameSTARTSWITHu_cmdb_ci^ORnameSTARTSWITHcmdb_ci');//custom CMDB table names are prepended with u_cmdb_ci
19+
dbObjGR.query();
20+
if (dbObjGR.next()) {
21+
22+
classTable = dbObjGR.getValue('name');
23+
}
24+
25+
if (!classTable) {
26+
gs.warn('[Import] Could not find table for class: ' + classDisplayName);
27+
continue;
28+
}
29+
30+
var supportGroupId = '';
31+
var managedByGroupId = '';
32+
33+
var groupGR = new GlideRecord('sys_user_group');
34+
groupGR.addQuery('name', 'IN', supportGroupName + ',' + managedByGroupName);
35+
groupGR.query();
36+
while (groupGR.next()) {
37+
var name = groupGR.getValue('name');
38+
if (name === supportGroupName) supportGroupId = groupGR.getUniqueValue();
39+
if (name === managedByGroupName) managedByGroupId = groupGR.getUniqueValue();
40+
}
41+
42+
if (!supportGroupId || !managedByGroupId) {
43+
gs.warn('[Import] Missing group sys_id for: ' + supportGroupName + ' or ' + managedByGroupName);
44+
continue;
45+
}
46+
47+
48+
var ciGR = new GlideRecord(classTable);
49+
ciGR.addEncodedQuery('Optional-Filters');
50+
ciGR.query();
51+
if (!ciGR.hasNext()) {
52+
gs.warn('[Import] No CI found in ' + classTable + ' with name: ' + classDisplayName);
53+
}
54+
55+
while (ciGR.next()) {
56+
ciGR.setValue('support_group', supportGroupId);
57+
ciGR.setValue('managed_by_group', managedByGroupId);
58+
ciGR.update();
59+
}
60+
}

0 commit comments

Comments
 (0)