Bulk Upload CI Relationship based on Child Class and Parent Class

Ishan4
Tera Contributor

I am bulk uploading relationships to cmdb_rel_ci table using Transform Map, the source table has Child Name, Child Class, Type, Parent Name, Parent Class fields but the target has only Child, Parent and Type fields and there is no direct way to target class of child and parent.

The issue is happening when there are CIs with same but different class of cmdb, so suppose if I want CI name 'ABC' of class 'Application Service' as child but transform is itself picking up CI named 'ABC' of class 'Business Service'.

It seems targeting sys_id is not allowed to script/manually using even admin.

What should be done to solve it ? Any onBefore script can be suggested ?

1 ACCEPTED SOLUTION

suvro
Mega Sage
Mega Sage

For each mapping use script instead of field to field mapping

For Parent field

var par = new GlideRecord (source.u_parent_class);

par.get('name', source.u_parent_name);

return par.sys_id;

 

For Child field

 

var child = new GlideRecord (source.u_child_class);

child.get('name', source.u_child_name);

return child.sys_id;

View solution in original post

5 REPLIES 5

suvro
Mega Sage
Mega Sage

For each mapping use script instead of field to field mapping

For Parent field

var par = new GlideRecord (source.u_parent_class);

par.get('name', source.u_parent_name);

return par.sys_id;

 

For Child field

 

var child = new GlideRecord (source.u_child_class);

child.get('name', source.u_child_name);

return child.sys_id;

Ishan4
Tera Contributor

Genius !

Thank you so much @suvro  ! I was stuck with onBefore() scripts, Thanks you so much.

Please see my reply on other question as well it worked fine on PDI but not where I wanted it to.  Must be issue with my roles.

Could you suggest onBefore() script ? Your script work perfectly, just I wanted to know if that way also same result could be achieved.

 

Thanks again.

Hi Ishan,

 

You can try an onBefore script below. No mapping will be required

 

var par = new GlideRecord (source.u_parent_class);

par.get('name', source.u_parent_name);

 

var child = new GlideRecord (source.u_child_class);

child.get('name', source.u_child_name);

 

var rel = new GlideRecord('cmdb_rel_ci');

rel.addQuery('parent', par.sys_id);

rel.addQuery('child', child.sys_id);

rel.addQuery('type.name', 'Depends on::Used by'); 

rel.query();

if (!rel.next()){

var relCreate = new GlideRecord('cmdb_rel_ci');

relCreate .initialize();

relCreate.parent = par.sys_id;

relCreate.child = child.sys_id;

relCreate.type = '1a9cb166f1571100a92eb60da2bce5c5';

relCreate.insert();

}

Ishan4
Tera Contributor

Hi

This also worked in PDI but two additional records were also created.

Note: In future source table could also be having named duplicate where I think I would include more queries to check on other ownership fields as well, your suggestion ?

find_real_file.png

The structure of CIs is:

find_real_file.png

 

Source in Map-> Mapped Application Service(cmdb_ci_service_discovered)

Target-> cmdb_rel_ci

 

Thanks.