- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2024 02:45 AM
Hello all.
I'm trying to populate relations between CIs by script, but no success on them (always empty items created).
I'm using the code:
function create_new_relation(ci_parent_id,rel_type_id,ci_child_id){
var gr_ci_parent = new GlideRecord('cmdb_ci');
gr_ci_parent.get('sys_id',ci_parent_id);
var gr_ci_child = new GlideRecord('cmdb_ci');
gr_ci_child.get('sys_id',ci_child_id);
var gr_rel_type = new GlideRecord('cmdb_rel_type');
gr_rel_type.get('sys_id',rel_type_id);
//creating the relation
var gr_create_rel = new GlideRecord('cmdb_rel_ci');
gr_create_rel.initialize();
gr_create_rel.parent(gr_ci_parent);
gr_create_rel.child(gr_ci_child);
gr_create_rel.type(gr_rel_type);
gr_create_rel.setWorkflow(false);
gr_create_rel.insert();
}
with the result:
Then, my two questions:
- Must I use any particular Script Include already existing in ServiceNow instead of this custom code?
- In CI Class definition, the relations who appears there are the only availables for the CI Class? or any suggestion, but could be other ones? or the ones who will be handled by Discovery process (I'm not using Discovery with these CIs I wan't to relate)?
Thanks in advance.
BR,
Sandro
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2024 03:25 AM
At the end I solved it by using GR.setValue(variable,value) in the inputs instead of directly adding values:
gr_create_rel.setValue('parent',ci_parent_id);
gr_create_rel.setValue('child',ci_child_id);
gr_create_rel.setValue('type',rel_type_id);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2024 06:27 PM
Inserting the records into the cmdb_rel_ci table does not require GlideRecord objects, it just needs the sys_ids, so pass those function parameters straight into the new cmdb_rel_ci record.
The below may not be perfect, but should get you where you need to be.
function create_new_relation(ci_parent_id,rel_type_id,ci_child_id){
//creating the relation
var gr_create_rel = new GlideRecord('cmdb_rel_ci');
gr_create_rel.initialize();
gr_create_rel.parent(ci_parent_id);
gr_create_rel.child(ci_child_id);
gr_create_rel.type(rel_type_id);
gr_create_rel.setWorkflow(false);
gr_create_rel.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2024 12:53 AM
Hi.
I already tried this way and the result was the same than defining the glide records inside the function. Because of this was my second question regarding if the relations defined are forced to be like there or simply are the expected ones, because I believe this is the reason of unproperly creation of the items.
Thanks anyway for your anwers 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2024 03:25 AM
At the end I solved it by using GR.setValue(variable,value) in the inputs instead of directly adding values:
gr_create_rel.setValue('parent',ci_parent_id);
gr_create_rel.setValue('child',ci_child_id);
gr_create_rel.setValue('type',rel_type_id);