CI Relationship creation through transform map or Automatically

mdeepu433
Tera Contributor

Hi Team,

 

I have 2 ci classes, instead of creation manual entries in cmdb_rel_ci table. i just wanted to build the relation ship automatically while inserting the records.

for example: let us say

parent: parent class

few records are there in this class

child: child class

only two records are there in this class

i am inserting the records in parent, after insertion the relationship needs to be create automatically. there is common value for parent and child is ip address. if it is matched then attach this parent to the respective child.

parent Runs on::Runs Child like this.

i have the below script but i failed in usagfind_real_file.png

please give the information if you have anyyyy......

 

Thanks

 

 

5 REPLIES 5

Gurpreet07
Mega Sage

Use transform map scripts to create relationships. I would suggest to use onComplete transform script for this. Use glideRecord to create relationships.

Please show us with an example script. How can we segregate parent and child record's

https://docs.servicenow.com/bundle/jakarta-platform-administration/page/script/server-scripting/concept/c_TransformMapScripts.html

Anvesha
Tera Contributor

I had a similar use case and I tried an On After Transform Script to create the relationship record in cmdb_rel_ci table. 

 

Below is the sample you can try:

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

//Create relationship


var app = new GlideRecord('cmdb_ci');
if (app.get('Field abc', source.reference field)) {

var rel = new GlideRecord('cmdb_rel_ci');
rel.addQuery('child', target.sys_id);
rel.addQuery('type', 'sys_id value');
rel.addQuery('parent', app.sys_id);
rel.query();

if (!rel.hasNext()) {
rel = new GlideRecord('cmdb_rel_ci');
rel.child = target.sys_id;
rel.type = 'sys_id value';
rel.parent = app.sys_id;
rel.insert();
}
}

})(source, map, log, target);