SlightlyLoony
Tera Contributor

find_real_file.pngMany tables in Service-now have relationships to other tables. For example, records in the CMDB's network adapter table (cmdb_ci_network_adapter) are generally related to other CIs (such as a computer). This relationship reflects the real-world relationship of (say) a couple network adapters installed in a server.

find_real_file.pngIf we were drawing an ERD of this relationship, it might look like the diagram at left. There are many different graphic notations for ERDs, so what you're used to might look a bit different — but the general idea is the same in any notation. Here our little ERD is showing that there is a many-to-one relationship between network adapters and computers — a network adapter belongs to a single CI, but a single CI may have any number of network adapters.

GlideRecord automagically handles the pesky details of object hierarchies, but when you want to find or manipulate related records, you're on your own. For example, suppose that in a script you've got a GlideRecord for a computer (which is a CI), and you want to find the network adapters that belong to it. How do you do that?

The first thing you have to do is figure out exactly what kind of relationship your two tables have. On the Service-now platform there are three distinct types of relationships between records. Today we'll talk about one of those: the related list, which the CI/network adapter relationship is a good example of.

In related lists, the table on the many side of the relationship (related lists are always a many-to-one relationship) has a reference field in it that "points" to the one side of the relationship. In the case of network adapters, the cmdb_ci_network_adapter has a field named cmdb_ci that is a reference to cmdb_ci (meaning to any CI). If you had a record for computer "Benonio" and it had two associated network adapters, your network adapter table would contain two records whose cmdb_ci field pointed to "Benonio". It's a simple as that.

How could we generalize this and make it easy? Wouldn't it be nice if I could write code like this?


var gru = new GRUtil();
var nics = gru.getRelatedList(computer, 'cmdb_ci_network_adapter', 'cmdb_ci');


The idea here is that if my computer variable contains a GlideRecord with a particular computer in it, then I can call getRelatedList() and it will return another GlideRecord containing the network adapters that belong to that computer. Here's how I can extend GRUtil to add that method. I started with the version of GRUtil defined here, then added this new method:

/*
* Returns a GlideRecord instance containing the records related to the given parent (a GlideRecord)
* in the given related table through the given reference field.
*/
getRelatedList: function(parent, related_table, reference_field) {
var result = new GlideRecord(related_table);
result.addQuery(reference_field, parent.sys_id);
result.query();
return result;
},


When I run this test code (I have a computer named "labapp02"):

var computer = new GlideRecord('cmdb_ci_computer');
computer.addQuery('name', 'labapp02');
computer.query();
if (computer.next()) {
var gru = new GRUtil();
var nics = gru.getRelatedList(computer, 'cmdb_ci_network_adapter', 'cmdb_ci');
while (nics.next())
gs.log(nics.name);
}


I get this result:

eth0
eth1
eth2


Perfect.

See why that kid is all excited? Aren't you?

7 Comments
Mahira
Tera Guru

Hi Looney,

Is it possible to show an 1:m relationship in a related list.

For eg: I would like to show the related CI as a related list (with one record) on the network Interface form?

Thanks and Regards,
Mahira


SlightlyLoony
Tera Contributor

Hi, Mahira...

Absolutely you can do this. There's a feature you'll need to learn how to use, called "Relationships" (here the word is used for a different meaning). I wrote a blog post on this about a year ago, and that post points to a wiki article as well.

Tom...


Mahira
Tera Guru

Hey Thanks Tom!

I had one more question...


On my CI form i have a related list which shows the all the related network devices (many to one relationship). Lets say I have a CI which has 2 network devices.

On my network devices form i have a reference field which contains the related CI. Also I have a related list on the network devices form which shows the related CI . For this related list I used the relationship CI --> Network Devices which comes through the reference field. When i did this i could see that the CI is populated in the related list for only one network device. For the second network device the related list is empty although the reference field has the same CI value...

Why is this so?

Could not find a logical reason for this 😞 . But this problem is solved when i created a relationship as mentioned in your blog.

Could you help me out here?

Thanks a bunch!


SlightlyLoony
Tera Contributor

Hi, Mahira...

Can you send me an email (tom.dilatush -at- service-now.com) with the instance and a pointer to the CI where you're seeing this? I'll take a look and see what's going on...

Tom..


Mahira
Tera Guru

HI Tom,

I see what the problem is. I had a normal reference field, referring to network devices on my CI form . And i was trying to relate more than one network devices to a single CI. To hold more than one network device refernce i should have used a glide list instead of a reference field.
This was actually causing the problem.

But that is a round about way of making the related list.But a neater solution is by using Defined Rekated list as mentioned in your blog.

Thanks for your time Tom!

~Regards,
Mahira


mturner2
Kilo Contributor

Hi, I'd love to be able to take a look at your rekationships blog, but for some reason the link doesnt work.....?


lawrence_eng
Administrator

Hi,

Unfortunately, the blog post you're referring to isn't available anymore.

Here are some Wiki links that Tom pointed to, however:

http://wiki.service-now.com/index.php?title=Using_Related_Lists
http://wiki.service-now.com/index.php?title=Business_Service_Map
http://wiki.service-now.com/index.php?title=Creating_Defined_Related_Lists

Hope that helps,
Lawrence