Mohit Kaushik
Mega Sage
Mega Sage

Defined Related lists in ServiceNow

Today I learned something new. I learned about Relationships (sys_relationship) table. I know many of you might already be aware of this functionality but for me it was new, and I thought of sharing it in my article below.

Related Lists: These are the lists which appear at the bottom of a form when there is some relationship present in between those two tables. A relationship could be of any type. One to one, one to many, many to many and scripted relationship.

So far, I was aware of One to one/One to many and Many to Many relationships only and for these relationships to be present there should be a direct connection between the two tables. Like a parent child relationship where one table is directly referencing other.

Then I came across a use case where I had to create a related list on a table which is not directly connected to the other table. For e.g. I want to display the list of requests as related list on incident table which were requested for the same caller. Now in this case Incident and Requests are not directly connected to each other, but they have a common reference to ‘sys_user’ table with the help of ‘caller_id’ and ‘requested_for’ fields respectively. So I could not configure a related list directly and had to go with the scripted approach.

Here comes the Defined Related lists into picture. In order to achieve this we need to create a related list in Relationships(sys_relationship) table. Below are the steps we will follow to achieve the same.

  1. Go to left Nav and look for System Definition->Relationships.
  2. Click New and fill the form as shown below and hit submit:find_real_file.png

Here, Applies to table: will be the table on which you want to create the related list.

Queries from table: will be the table from which we want to display the data as a related list.

Query with: In this script section we will write the query or logic we want to perform to get the data from sc_requests. Clearly it is possible to add multiple queries here or write encoded query as well.

  1. Once done then we need to go to incidents and configure this related list on incident table. In order to configure the related list, we need to right click on the form header and select Configure-> Related lists. After this we need to pick the specific related list from left slush bucket and place it into right slush bucket and save as shown in below steps:

          find_real_file.png

         find_real_file.png

         find_real_file.png

  1. After this you can see the related list will appear on incident form related lists section as shown below:find_real_file.png

 

Note: There is a limitation to this approach, you can not get Edit button on these kind of related lists as the relationship is scripted.

 

Below are some useful links you can refer while working on related lists:

Related Lists

Many to Many Relationships

Create defined related lists

An Example: Add incidents by same caller related list

 

 

Please mark this helpful if it helps and you can subscribe it or bookmark it for future reference and updates on this article.

I am open for the suggestions and comments on this article.

 

Thanks,
Mohit Kaushik
Community Rising Star 2022

Linkedin - Mohit

Comments
Shiva Thomas
Kilo Sage

Thanks for this article Kaushik. ^_^

Mohit Kaushik
Mega Sage
Mega Sage

Glad it helped you @Shiva Thomas :).

Mohit Kaushik
Mega Sage
Mega Sage

Edit: Modified the links to the latest release version.

Souful Isha
Tera Expert

I would like to create a related list in the Users table to list all the employees who are reporting to that user. In other words, all list of all employees who have this current user as manager. I am struggling with the scripting part. 

Mohit Kaushik
Mega Sage
Mega Sage

Hi @Souful Isha,

 

You can write the script as below to populate the list. I am not able to test this code as my PDI is very slow. But this should work. Here both the tables should be sys_user table.

(function refineQuery(current, parent) {

    // Add your code here, such as current.addQuery(field, value);
	
    current.addQuery('manager', parent.sys_id);
})(current, parent);

 

Suvetha S
Tera Contributor

Hi @Mohit Kaushik ,

 

Can we create Relationship between 2 table which are from different Application scopes?

Mohit Kaushik
Mega Sage
Mega Sage

Hi @Suvetha S,

I have never tried that, but if the table's configuration is set in a way that you can perform configurations in different scopes using scripts then it should let you have the list with the data. Just try that and let me know if it works.

Suvetha S
Tera Contributor

Hi @Mohit Kaushik ,

 

I created a relationship between Discovery Schedule(Global) & Network Site (CMDB CI Class Models). both the tables had a reference to Location table and created it to show the Discovery schedules which are running on the same Location as the Network Site. 

Version history
Last update:
‎02-25-2022 02:04 AM
Updated by: