- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2024 08:46 AM
We are trying to add the relationship via the script, and the below script works but if we run it again, it insterts another duplicate relationship in the "relationship" table. Is there a way to skip the insert of the relationship if there is already a relationship with the "parentSYSID" ?
var parentSYSID = "7bcedacf1ba98a10ed7ca930604bcbde"; //
var parentSYSID2 = "dd1f968787a90a108a267556cebb3563"; //
var gr = new GlideRecord('cmdb_ci_wap_network');
gr.addQuery("manufacturer!=b7e831bdc0a80169015ae101f3c4d6cd^ORmanufacturer=NULL^nameSTARTSWITHASWAP");
gr.query();
while (gr.next()) {
var grIn = new GlideRecord('cmdb_rel_ci');
grIn.newRecord();
grIn.parent = parentSYSID;
grIn.child = gr.sys_id;
grIn.type = '9aac679137303100dcd445cbbebe5d7f'; // sys_id of the Relationship Type
grIn.insert();
gs.info(grIn);
var grIn2 = new GlideRecord('cmdb_rel_ci');
grIn2.newRecord();
grIn2.parent = parentSYSID2;
grIn2.child = gr.sys_id;
grIn2.type = '9aac679137303100dcd445cbbebe5d7f'; // sys_id of the Relationship Type
grIn2.insert();
gs.info(grIn2);
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2024 10:56 AM
Here's a format I use to check for the record first, then create it if it doesn't exist:
var parentSYSID = "7bcedacf1ba98a10ed7ca930604bcbde"; //
var parentSYSID2 = "dd1f968787a90a108a267556cebb3563"; //
var gr = new GlideRecord('cmdb_ci_wap_network');
gr.addQuery("manufacturer!=b7e831bdc0a80169015ae101f3c4d6cd^ORmanufacturer=NULL^nameSTARTSWITHASWAP");
gr.query();
while (gr.next()) {
var grIn = new GlideRecord('cmdb_rel_ci');
grIn.addQuery('parent', parentSYSID);
grIn.addQuery('child', gr.sys_id);
grIn.addQuery('type', '9aac679137303100dcd445cbbebe5d7f');
grIn.query();
if (!grIn.next()) {
grIn.newRecord();
grIn.parent = parentSYSID;
grIn.child = gr.sys_id;
grIn.type = '9aac679137303100dcd445cbbebe5d7f'; // sys_id of the Relationship Type
grIn.insert();
gs.info(grIn);
}
var grIn2 = new GlideRecord('cmdb_rel_ci');
grIn2.addQuery('parent', parentSYSID2);
grIn2.addQuery('child', gr.sys_id);
grIn2.addQuery('type', '9aac679137303100dcd445cbbebe5d7f');
grIn2.query();
if (!grIn2.next()) {
grIn2.newRecord();
grIn2.parent = parentSYSID2;
grIn2.child = gr.sys_id;
grIn2.type = '9aac679137303100dcd445cbbebe5d7f'; // sys_id of the Relationship Type
grIn2.insert();
gs.info(grIn2);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2024 10:56 AM
Here's a format I use to check for the record first, then create it if it doesn't exist:
var parentSYSID = "7bcedacf1ba98a10ed7ca930604bcbde"; //
var parentSYSID2 = "dd1f968787a90a108a267556cebb3563"; //
var gr = new GlideRecord('cmdb_ci_wap_network');
gr.addQuery("manufacturer!=b7e831bdc0a80169015ae101f3c4d6cd^ORmanufacturer=NULL^nameSTARTSWITHASWAP");
gr.query();
while (gr.next()) {
var grIn = new GlideRecord('cmdb_rel_ci');
grIn.addQuery('parent', parentSYSID);
grIn.addQuery('child', gr.sys_id);
grIn.addQuery('type', '9aac679137303100dcd445cbbebe5d7f');
grIn.query();
if (!grIn.next()) {
grIn.newRecord();
grIn.parent = parentSYSID;
grIn.child = gr.sys_id;
grIn.type = '9aac679137303100dcd445cbbebe5d7f'; // sys_id of the Relationship Type
grIn.insert();
gs.info(grIn);
}
var grIn2 = new GlideRecord('cmdb_rel_ci');
grIn2.addQuery('parent', parentSYSID2);
grIn2.addQuery('child', gr.sys_id);
grIn2.addQuery('type', '9aac679137303100dcd445cbbebe5d7f');
grIn2.query();
if (!grIn2.next()) {
grIn2.newRecord();
grIn2.parent = parentSYSID2;
grIn2.child = gr.sys_id;
grIn2.type = '9aac679137303100dcd445cbbebe5d7f'; // sys_id of the Relationship Type
grIn2.insert();
gs.info(grIn2);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2024 11:10 AM
Thanks @Brad Bowman it worked 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-24-2024 11:18 AM
Good to hear - you are welcome!