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,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.**

Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading