Defined Related Lists

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…

current.addQuery('caller_id', parent.caller_id);

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.addQuery('caller_id', parent.caller_id);
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!

Approval – Affected CIs
–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.addQuery('task', parent.sysapproval);
current.addNotNullQuery('task');
All Request Attachments
–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:

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');
All Request task and Approval Attachments
–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:

//Current record Attachments
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');
Approval Task Attachments
–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:

//Add attachments for the approval task
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);
}
}
}

Date Posted:

July 30, 2010

Share This:

39 Comments

  1. Quyen October 1, 2010 at 9:19 am

    Hi Mark,

    Is there a way to restrict by role the removal of an item from a related list?

    Thanks

    • Mark Stanger October 1, 2010 at 9:25 am

      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.

  2. Ron November 14, 2012 at 5:37 pm

    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

  3. Andrew December 19, 2012 at 1:14 pm

    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’);

    • Mark Stanger December 19, 2012 at 1:36 pm

      I think the problem is on this line…
      tsk.addQuery(‘change_task’, parent.sys_id);

      Should use ‘change_request’ there instead of ‘change_task’.

  4. Andrew December 20, 2012 at 5:12 am

    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!

    • Mark Stanger December 20, 2012 at 5:50 am

      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’.

  5. Troy February 14, 2013 at 2:51 pm

    Thanks for the help! The scripts gave me what I needed.

    • Mark Stanger February 14, 2013 at 2:58 pm

      No problem Troy, I’m glad it helped! Keep Crossfuze in mind in the future if you need additional ServiceNow assistance.

  6. Kurt Lutz March 13, 2013 at 8:32 am

    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.

    • Mark Stanger March 13, 2013 at 9:56 am

      @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

      • Kurt Lutz March 13, 2013 at 10:11 am

        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.

  7. Kurt Lutz April 9, 2013 at 12:11 pm

    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?

  8. Pavan June 22, 2013 at 10:05 am

    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.

  9. kchorny September 12, 2014 at 10:49 am

    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?

    • Mark Stanger September 12, 2014 at 11:01 am

      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’);

      • kchorny September 17, 2014 at 2:45 pm

        That was too easy, thank you!!

  10. John October 15, 2014 at 2:07 pm

    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’);

    • Paul January 27, 2015 at 3:07 pm

      John, I’m having a similar issue. Did you find a solution?

  11. Paul January 28, 2015 at 2:49 pm

    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?

    • Mark Stanger January 28, 2015 at 2:56 pm

      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.

  12. Paul January 28, 2015 at 4:40 pm

    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.

    • Deb March 19, 2015 at 8:58 pm

      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

  13. Deb March 20, 2015 at 8:16 pm

    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

    • Mark Stanger March 21, 2015 at 5:33 am

      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.

  14. Deb March 21, 2015 at 8:48 pm

    Thanks a lot Mark! I wasn’t adding the correct related list to the task form. It works as expected!!!

  15. Deb March 21, 2015 at 11:29 pm

    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

  16. Shay Faingold May 20, 2015 at 4:14 am

    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

  17. Brad R. August 13, 2015 at 11:46 am

    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!

    • Mark Stanger August 13, 2015 at 2:15 pm

      Thank you, I’m glad it helped!

  18. Heather February 24, 2017 at 1:39 pm

    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

    • Mark Stanger February 24, 2017 at 9:14 pm

      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.

      • Heather March 8, 2017 at 10:39 am

        That worked great. Have you seen anything similar to this for adding new attachments from the approval to the request item?

        • Mark Stanger March 8, 2017 at 2:04 pm

          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.

  19. JR Guieb March 10, 2017 at 6:59 pm

    This works just perfectly for us. Now is there a way to have this in the Service Portal?

  20. BK June 5, 2017 at 11:35 pm

    Add on… The approval refers to the change request approvals (example approval from change coordinator, CAB) for Normal, Emergency changes

    Thanks.

  21. Stacy Menigoz August 16, 2017 at 4:07 pm

    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

    • Mila Morales September 14, 2017 at 10:16 am

      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”

Comments are closed.

Categories

Tags

Loading

Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.