Defined Related Lists can be a very simple and useful tool to provide users with information in a related list directly on a form (even if that information is not directly associated with the record being viewed). The Service-now wiki contains documentation on this topic so I won’t cover that here. The point of this article is to point out something that often gets overlooked when working with Defined Related Lists and to share a few Defined Related Lists that I’ve used in the past.
One of the familiar defined related lists is the ‘Incidents by Same Caller’ list that you can display at the bottom of your incident form. The name says it all, but it looks something like this…

If you navigate to ‘System Definition -> Relationships’ and open up the ‘Incidents by Same Caller’ relationship record you’ll see that it uses the following query to produce the resultant related list…
But what happens when we set the Caller field with an empty value?

The image above shows that you’ll get exactly what you’re supposed to get, but probably not what you want. Depending on your setup, there might not be a whole lot of value in showing a list of incidents with no caller…at least at the bottom of your incident form. The solution to this is to filter out any records where the field you are querying is empty. In this case, we want to filter out any records where the Caller field is empty like this…
current.addNotNullQuery('caller_id');
Adding the ‘addNotNullQuery’ line gives us the result we would expect at the bottom of our incident form.

Here are a few other examples of defined related lists I have implemented in the past. Please share any defined related lists you have come up with by commenting on this post below!
–Shows a list of Affected CIs for the task being approved at the bottom of an approval form.
Name: Affected CIs
Applies to table: Approval (sysapproval_approver)
Queries from table: CIs Affected (task_ci)
Query with:
current.addNotNullQuery('task');
–Shows a list of attachments from the parent request, request item, and catalog task records on a catalog task form.
Name: All Attachments
Applies to table: Catalog task (sc_task)
Queries from table: Attachment (sys_attachment)
Query with:
qc.addOrCondition('table_sys_id', parent.request_item.sys_id);
qc.addOrCondition('table_sys_id', parent.request_item.request.sys_id);
current.addNotNullQuery('table_sys_id');
–Shows a list of attachments from the parent request, request item, catalog task, and approval records on a request item form.
Name: All Attachments
Applies to table: Requested item (sc_req_item)
Queries from table: Attachment (sys_attachment)
Query with:
var qc = current.addQuery('table_sys_id', parent.sys_id);
//Request Attachments
qc.addOrCondition('table_sys_id', parent.request.sys_id);
//Catalog Task Attachments
var tsk = new GlideRecord('sc_task');
tsk.addQuery('request_item', parent.sys_id);
tsk.query();
var tskIDArr = [];
while(tsk.next()){
tskIDArr.push(tsk.sys_id.toString());
}
var tskIDStr = tskIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', tskIDStr);
//Approval Attachments
var app = new GlideRecord('sysapproval_approver');
app.addQuery('document_id', parent.sys_id);
app.query();
var appIDArr = [];
while(app.next()){
appIDArr.push(app.sys_id.toString());
}
var appIDStr = appIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', appIDStr);
//Do not include attachments not associated with a record
current.addNotNullQuery('table_sys_id');
–Shows a list of attachments from the associated approval task(s) on an approval form. In addition to displaying attachments for the specific task, this also shows attachments from the parent change request (for change tasks), the parent request item and request (for catalog tasks), the parent request (for catalog items), and the associated child items (for catalog requests).
Name: Approval Task Attachments
Applies to table: Approval (sysapproval_approver)
Queries from table: Attachment (sys_attachment)
Query with:
current.addNotNullQuery('table_sys_id');
var qc = current.addQuery('table_sys_id', parent.sysapproval);
//Add parent task attachments based on the approval task type
var appTask = '';
if(parent.sysapproval.sys_class_name){
appTask = parent.sysapproval.sys_class_name;
}
if(appTask != ''){
var tsk = new GlideRecord(appTask);
tsk.get(parent.sysapproval);
//Check for approval task type and add necessary queries
if(appTask == 'change_task'){
//Query for attachments from the associated change
qc.addOrCondition('table_sys_id', tsk.change_request);
}
else if(appTask == 'sc_task'){
//Query for attachments from the associated item and request
qc.addOrCondition('table_sys_id', tsk.request_item);
qc.addOrCondition('table_sys_id', tsk.request_item.request);
}
else if(appTask == 'sc_req_item'){
//Query for attachments from the associated request
qc.addOrCondition('table_sys_id', tsk.request);
}
else if(appTask == 'sc_request'){
//Set up a variable to store item sys_ids
var items = '';
//Query for attachments from the associated request items
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', parent.sysapproval);
ritm.query();
while(ritm.next()){
items = items + ',' + ritm.sys_id.toString();
}
if(items != ''){
qc.addOrCondition('table_sys_id', 'IN', items);
}
}
}
Hi Mark,
Is there a way to restrict by role the removal of an item from a related list?
Thanks
With defined related lists you can sometimes control access with an ACL. It all depends on the type of query you’re setting up because usually there is no direct relationship to the parent record. For other lists you just have to set up the correct ACL or list control. Check out the wiki for more help on those topics. If you have specific questions on those you should direct them to the Service-now forums.
Hi Mark,
I’ve been trying to figure out how use a field from the displayed form for the “query from”. in the documentation it says “When scripting your condition, current is the record to which you want to add queries while parent is the main record being displayed.”
but using: answer = parent.u_table doesn’t work. however using answer = “cmdb_ci_computer” does.
what am I missing?
Thanks
What am I missing…? I took the request script and tweaked it for us in the Change_request / Change task relam, however even my new records are showing the same content? I’m trying to script this so that the Change_Request see all the attachments that might have been added from the Change_task?
var qc = current.addQuery(‘table_sys_id’, parent.sys_id);
qc.addOrCondition(‘table_sys_id’, parent.change_request.sys_id);
//Query for associated tasks
var tsk = new GlideRecord(‘change_task’);
tsk.addQuery(‘change_task’, parent.sys_id);
tsk.query();
var tskIDArr = new Array();
while(tsk.next()){
tskIDArr.push(tsk.sys_id.toString());
}
var tskIDStr = tskIDArr.join();
qc.addOrCondition(‘table_sys_id’, ‘IN’, tskIDStr);
current.addNotNullQuery(‘table_sys_id’);
I think the problem is on this line…
tsk.addQuery(‘change_task’, parent.sys_id);
Should use ‘change_request’ there instead of ‘change_task’.
Your suggestion worked, so lets see if you have any thoughts on how to configure sysapproval_approver the same way. My issue is that my current change_request has a CM phase (not the out of box field), that will not work correctly on the sysapproval_approver, likewise I have a field on the Task called CM_Task that is the name defined by workflow for the task. I can’t get either to work correctly on the sysapproval_approver, however I have been able to make a relationship and get see the content I’m needing, however again it is showing me all request approvals and not just the one for the current sys_id? Without any modification I can use the corrected CM attachment script to show me all the cm approvals, but I haven’t been able to limit this list the current sys_id?
Thanks for your help!
You should be able to use the portion of the script above for the request table. Just change everything that points to ‘request’ to point to ‘change_request’, and everything that points to ‘sc_req_item’ to point to ‘change_task’.
Thanks for the help! The scripts gave me what I needed.
No problem Troy, I’m glad it helped! Keep Crossfuze in mind in the future if you need additional ServiceNow assistance.
Is there a way to get all the task records related to a parent task? For example, on the change request you can have a related list for Incidents caused by the change, problem, incidents pending change request, other changes, etc. Would it be or could it be more effectient to have a single related list that would show all these records? It seems performance on the load of the form is slow because of multiple related list and wanted to test with a single related list – any suggestions on how I could do this would be appreciated. Thanks.
@Kurt. You could do that by querying the task table for those records. You would have to drill into each of the specific incident and problem tables to get to those values though. Even then, the list UI actions probably wouldn’t work because there wouldn’t be a way to tell which task type to deal with. You might also take a look at the Many-to-many task relationship plugin (which helps to consolidate these relationships into a single table.
http://wiki.servicenow.com/index.php?title=Many_to_Many_Task_Relations_Plugin
Thanks Mark. I was looking at the many-to-many in our non-prod environment but when I tried to set one up it created it in German – so was a bit worried that it was not working correctly as the standard is english.
Curious about an “issue” seen with Related list. I have been working on several pieces with regards to Related Lists since my last question and saw something I am not quite sure about. When you related one change request to another…you will see it listed as the “parent” in the list view of open change requests but when you go into the related list of the “child” change request – the related list view there will say Parent = “itself” Why does this not recognize that there is a true parent, the other change request and show that instead of itself?
I want to have a related list for showing related child incidents under each child incident.
for example if i have 4 child incidents under 1 parent. if i open any one of child incident it should show remaining three child incidents;.
Any help is much appreciated.
I was able to implement the All Request Attachments as you described – thank you for that!! Now I’m trying to display all of the catalog tasks (and their status) related to an item in the catalog tasks themselves. How would I modify one of these examples to do that?
That’s probably a new relationship record that queries from and applies to the ‘sc_task’ table. Your script would look like this I think…
current.addQuery(‘request_item’, parent.request_item);
current.addNotNullQuery(‘request_item’);
That was too easy, thank you!!
I am running into a dead end. I tried the All Request Attachments script and my attachments from RITMs are still not carrying over to my TASK. Am i missing something here?
var qc = current.addQuery(‘table_sys_id’, parent.sys_id);
qc.addOrCondition(‘table_sys_id’, parent.request_item.sys_id);
qc.addOrCondition(‘table_sys_id’, parent.request_item.request.sys_id);
current.addNotNullQuery(‘table_sys_id’);
John, I’m having a similar issue. Did you find a solution?
Mark, I’ve been trying the related list “All Request Attachments” query that shows a list of attachments from the parent request, request item, and catalog task records on a catalog task form, and am not able to get it to work, except on the Task that actually owns the attachment(s). We’re on Eureka Patch 8 currently, but I first tried it in Calgary Patch 5, where it also wasn’t working. Do you have any insights?
I’ve got it working fine right now on a demo instance. Just for clarification though, that one doesn’t show attachments from the other tasks, just from its own task, request item, and request. If there were an attachment on another task that needed to be shared, I would think that should be attached at the item or request level.
Thanks for the reply Mark. Yeah, it works fine for me too, as long as I’m viewing at the Task record from which the attachment is added. I guess my perception was that with said Related List added to the Catalog Task form, attachments from any Task with the Request Item would be visible in the same Related List on any other Task within the the same Request Item.
Hi Paul,
I am adding an attachment to the catalog submission form and getting it to show up on the RITM form. But then using the above code that you’ve used I am not able to get it to show up in the Catalog task form. Is this what you had tried and succeeded or were you adding your attachment elsewhere.
Thanks,
Deb
Hi Mark,
I am attaching a file during item submission and get it to show up in the RITM form. Then I am using your code:
var qc = current.addQuery(‘table_sys_id’, parent.sys_id);
qc.addOrCondition(‘table_sys_id’, parent.request_item.sys_id);
qc.addOrCondition(‘table_sys_id’, parent.request_item.request.sys_id);
current.addNotNullQuery(‘table_sys_id’);
But it does not show up in the catalog task form. What could I be possibly doing wrong?
Will appreciate your help very much!
Deb
As long as you’re using the code above in a ‘Relationship’ record, you should just have to add the related list. Its name should be the same as the name you gave to the ‘Relationship’ record where you placed the script.
Thanks a lot Mark! I wasn’t adding the correct related list to the task form. It works as expected!!!
Hi Mark,
Need one last help. Now that the attachments are showing under the related lists, I would like to hide them showing up at the top of the
forms. the paper clip needs to stay but just the attachments should not be displayed there anymore. Would you know of some way to
get this done?
Many thanks for all the help!
Deb
I added a related list to the User profile, to align with ServiceNow licensing.
So in the User record, to see all user activity in the past 90 days:
var start = gs.daysAgoStart(90);
current.addQuery(‘sys_updated_by’, parent.user_name);
current.addQuery(“sys_created_on”, “>”, start);
Thank you,
Shay
This is fantastic. We had started down the path of copying attachments between Items and Tasks, and I knew that was a bad idea. I copied the “Query for associated tasks” bit into the “All Request Attachments” Relationship to show sister tasks in that view as well, changing:
tsk.addQuery(‘request_item’, parent.sys_id);
to:
tsk.addQuery(‘request_item’, parent.request_item.sys_id);
Worked well. Thanks for sharing!
Thank you, I’m glad it helped!
Hi Mark,
We have a scenario where a catalog task is created first in the workflow and after the task has been closed an approval is created. Should the Approval Task Attachments allow an attachment that has been added to a catalog task be visible on the sys_approval record? I tried the query but did not succeed.
Thank you,
Heather
I haven’t really seen that type of thing before, but a slight modification to the final part of the script above should be all you need. You just need to change the ‘sc_req_item’ section to look like the ‘sc_request’ section but substitute everything to query the ‘sc_task’ table.
That worked great. Have you seen anything similar to this for adding new attachments from the approval to the request item?
Sure. Check out the ‘All Request task and Approval Attachments’ script above. I just added a section to that script showing how you could pull the attachments from any approval associated to the record being viewed.
This works just perfectly for us. Now is there a way to have this in the Service Portal?
Not sure what you mean exactly. You can have standard forms and related lists in the Service Portal using the form widget. There are more details on that here if that’s what you’re looking for.
https://community.servicenow.com/community/develop/blog/2016/08/31/adventures-in-service-portaling-the-form-widget
Add on… The approval refers to the change request approvals (example approval from change coordinator, CAB) for Normal, Emergency changes
Thanks.
Hi Mark,
Here is my scenario. We have a problem record that has associated incidents to it. However, my customer wants to be able to put the problem record on the parent incident and be able to see all of the children of that parent incident. They do not want to have to put the problem ID on each child incident.
Is it possible to create the relationship between Problem and Incident and find all of the children incidents of a parent incident that has a problem id in it?
Thanks,
Stacy
Hi, Stacy.
There is already an existing relationship between the problem id and the incident. You can add the ‘Problem’ field on the form layout and there is no need to create a related list for this since there can only be one Problem ID per incident.
With regards to showing a related list of child incidents from the parent incident form. You can show this by doing the following:
Right click on the menu header > configure > related list > “Incident>Parent Incident”