Priyanka_786
Tera Guru
Tera Guru

Hi Folks,

 

Hope you all are doing good!

 

Often we got the requirement to display related lists with some dynamic filter which are not directly available on table/ linked using reference field. In this article, we will see how to achieve it with following use case.

 

Use Case :-  Show all the incidents opened by same caller in the related list of incident form.

 

Analysis:-   There is caller field available on the incident form. We need to find out all other incidents by same caller as reference on the given incident form. This is not possible using simple related list. We need to define some logic to fetch this query. Let's dive into it now.

 

Step 1 :-  Type Relationships in application navigator. Navigate to System Definition - Relationships

Priyanka_786_0-1746280017421.png

Step 2:-  Click on New button to create new relationship. Fill out the required details as below and submit form.

1. Name - Give appropriate name. In this case it will be "Incidents by Same Caller".

2. Applies to table - This is the table where to show related list. In this case it is incident.

3. Queries from table - This is the table where we need to apply query and display result in related list. In this case it is incident only as we only want incidents filter by same caller.

4. Queries with (script section) - Here need to write query using script. Please check comments for explanation of code.

// Display all the incidents by same caller in the incident related list

(function refineQuery(current, parent) {

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

    // current- Object of the Queries from table
    // parent - Object of the Applies to table.

    // here is query applied to incident table using current object with field caller_id as parent menaning table where to show related list matches caller_id

})(current, parent);

Step 3:-   Copy the sys_id of the above relationship using option copy sys_id from context menu. Type Related list in application navigator and go to System UI - Related Lists module.

Priyanka_786_1-1746281253941.png

 

Step 4:-   In this case need to configure related list on incident table. So search the table name as incident. Open the record as per the required view. In this case it is  default view.

Step 5:-  Click on New button from related list entries. Fill the out the form with required details as below and submit.

1. Related list - Type the name in this format- REL: copied sys_id or relationship record.

2. Position - Provide the appropriate position to place this new related list

Priyanka_786_0-1746281874288.png

Step 6:- Go to the incident form- click on Configure-Related lists from context menu. Add this related list with name Incidents by Same Caller and save.

Priyanka_786_1-1746282082061.png

 

Output:- Configuration steps are done now. Incidents by same caller are displayed in related list !

 

Priyanka_786_2-1746282323581.png

Priyanka_786_3-1746282383640.png

Additional Notes:- 

1. ServiceNow server side scripting knowledge required as prerequisite.

2. Please note that this approach will come into the picture as per pre-analysis if following points are checked.

         1.  If it is not achieved by simple related list (Using reference and dot walking).

         2. No existing OOB related list/ relationship present for the same.

3. Relationships can be configured using more complex logic based on the requirement by                   enabling Advanced checkbox.

 

I hope it's useful.

Please mark my post helpful/correct if you find something valuable.

Regards,

Priyanka Salunke

                                 

Comments
VIJAY YACHAM1
Tera Contributor

Thank you  @Priyanka_786  for sharing, very helpful 

Version history
Last update:
‎05-03-2025 07:53 AM
Updated by:
Contributors