Skip to content

Commit a3765d0

Browse files
authored
Create Reference field value update from CI relationship connection.js (#1532)
* 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. * 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.
1 parent 0a2b035 commit a3765d0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var updatedSysIds = [];
2+
var notUpdatedSysIds = [];
3+
4+
var gr = new GlideRecord('< table_name >');
5+
gr.addQuery('< query condition >');
6+
gr.query();
7+
8+
while (gr.next()) {
9+
10+
var relCi = new GlideRecord('cmdb_rel_ci');
11+
relCi.addQuery('child', gr.sys_id);
12+
relCi.addQuery('parent.sys_class_name', '< backend name of the table to which the reference field is referred to >');
13+
relCi.query();
14+
15+
if (relCi.next()) {
16+
// Update the reference field with the referenced table's sys_id
17+
gr.< reference field backend name > = relCi.parent.sys_id;
18+
gr.setWorkflow(false);
19+
gr.update();
20+
updatedSysIds.push(gr.sys_id.toString()); // Add to updated list
21+
}
22+
else {
23+
notUpdatedSysIds.push(gr.sys_id.toString()); // Add to not updated list
24+
}
25+
}
26+
27+
// Print the sys_ids of the records updated and not updated
28+
gs.print("Updated records sys_ids: " + updatedSysIds.join(', '));
29+
gs.print("Not updated records sys_ids: " + notUpdatedSysIds.join(', '));

Background Scripts/readME.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
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.
2+
We are also printing the sys_ids pf the records which would be updated and the ones that would be skipped.

0 commit comments

Comments
 (0)