Issue regarding Bulk Import through catalog Item

Priyanka1997
Tera Contributor

Hi All,

 

I Have one catalog Item with attachment type variable(mandatory). It takes in as input an excel template with following columns

 

"Parent Name"     "Type"    &     "Child Name"

 

FYR, (Excel contains bulk records in below format)

Priyanka1997_0-1672741259123.png

 

My requirement is to Create the new entries on cmdb_rel_ci table (CI Relationship) between the parent CI identified by the 'Parent Name' value with the child CI identified by the 'Child Name' value. 

 

Kindly assist me.

3 REPLIES 3

Anish Reghu
Kilo Sage
Kilo Sage

Here is an example of a server-side script that could be used to create the new CI relationships in the cmdb_rel_ci table based on the input from the Excel template:

 

 

// Load the Excel file and parse the rows
var excelFile = new GlideScriptedInputStream(inputs.file.getInputStream());
var excel = new ExcelParser().parse(excelFile);

// Loop through the rows of the Excel sheet
for (var i = 0; i < excel.length; i++) {
  var row = excel[i];

  // Skip the first row (column headers)
  if (i == 0)
    continue;

  // Get the values from the current row
  var parentName = row[0];
  var type = row[1];
  var childName = row[2];

  // Look up the parent and child CIs based on their names
  var parentCI = new GlideRecord('cmdb_ci');
  parentCI.addQuery('name', parentName);
  parentCI.query();
  if (!parentCI.next()) {
    gs.error('Parent CI not found: ' + parentName);
    continue;
  }

  var childCI = new GlideRecord('cmdb_ci');
  childCI.addQuery('name', childName);
  childCI.query();
  if (!childCI.next()) {
    gs.error('Child CI not found: ' + childName);
    continue;
  }

  // Create a new CI relationship between the parent and child CIs
  var rel = new GlideRecord('cmdb_rel_ci');
  rel.initialize();
  rel.parent = parentCI.getUniqueValue();
  rel.child = childCI.getUniqueValue();
  rel.type = type;
  rel.insert();
}

 

 

This script assumes that the Excel file is passed in as an input variable called file. It also assumes that the first row of the sheet contains the column headers, and skips this row.

For each row in the sheet, the script looks up the parent and child CIs based on their names using GlideRecord queries, and then creates a new CI relationship between them using the type specified in the Excel template. If the parent or child CI is not found, the script will log an error message and skip the row.

 

Kindly mark the response as Correct or Helpful.

Cheers,

Anish

Hi @Anish Reghu ,

 

Thanks for your reply,

 

One modification Required, I want to insert excel sheet records in target table based on Model ID instead of Name of CI. Is It Possible??

Priyanka1997_0-1673252768182.png

Kindly assist me.

@Priyanka1997 - Hope the query asked in the question is addressed, kindly raise a new thread to address the new ask. If the previous comment did address the ask in the question, please mark it as a correct solution as well.

 

Regards,

Anish