From a17daa7c4cd60d8b61e70b9575611b6d5dc52ef5 Mon Sep 17 00:00:00 2001 From: Soumyadeep10 <78016846+Soumyadeep10@users.noreply.github.com> Date: Mon, 28 Oct 2024 19:03:54 +0530 Subject: [PATCH 1/2] Create Reference field value update from CI relationship connection.js In this piece of code, we are querying a table for some records and then updating the reference field value of those records to the value of the specific parent class to which it has a cmdb_rel_ci relationship. We are also printing the sys_ids pf the records which would be updated and the ones that would be skipped. --- ... update from CI relationship connection.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Background Scripts/Reference field value update from CI relationship connection.js diff --git a/Background Scripts/Reference field value update from CI relationship connection.js b/Background Scripts/Reference field value update from CI relationship connection.js new file mode 100644 index 0000000000..d666dff457 --- /dev/null +++ b/Background Scripts/Reference field value update from CI relationship connection.js @@ -0,0 +1,29 @@ +var updatedSysIds = []; +var notUpdatedSysIds = []; + +var gr = new GlideRecord('< table_name >'); +gr.addQuery('< query condition >'); +gr.query(); + +while (gr.next()) { + + var relCi = new GlideRecord('cmdb_rel_ci'); + relCi.addQuery('child', gr.sys_id); + relCi.addQuery('parent.sys_class_name', '< backend name of the table to which the reference field is referred to >'); + relCi.query(); + + if (relCi.next()) { + // Update the reference field with the referenced table's sys_id + gr.< reference field backend name > = relCi.parent.sys_id; + gr.setWorkflow(false); + gr.update(); + updatedSysIds.push(gr.sys_id.toString()); // Add to updated list + } + else { + notUpdatedSysIds.push(gr.sys_id.toString()); // Add to not updated list + } +} + +// Print the sys_ids of the records updated and not updated +gs.print("Updated records sys_ids: " + updatedSysIds.join(', ')); +gs.print("Not updated records sys_ids: " + notUpdatedSysIds.join(', ')); From 6be9c2895f5f6424bd37b409e72113c0029629fe Mon Sep 17 00:00:00 2001 From: Soumyadeep10 <78016846+Soumyadeep10@users.noreply.github.com> Date: Mon, 28 Oct 2024 19:18:11 +0530 Subject: [PATCH 2/2] Create readME.md In this piece of code, we are querying a table for some records and then updating the particular reference field value of those records to the value of the specific parent class to which it has a cmdb_rel_ci relationship. We are also printing the sys_ids of the records which would be updated and the ones that would be skipped. --- Background Scripts/readME.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Background Scripts/readME.md diff --git a/Background Scripts/readME.md b/Background Scripts/readME.md new file mode 100644 index 0000000000..e98badb702 --- /dev/null +++ b/Background Scripts/readME.md @@ -0,0 +1,2 @@ +In this piece of code, we are querying a table for some records and then updating a particular reference field's value of those records to the value of the specific parent class to which it has a cmdb_rel_ci relationship. +We are also printing the sys_ids pf the records which would be updated and the ones that would be skipped.