Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jim Coyne
Kilo Patron
Part of the Tips 'N Tricks" series.

 

I've seen a lot of requests in the Community to see related attachments on multiple forms.   For instance, people want to see Requested Item attachments on the related Catalog Task records as well.   Most "solutions" that are suggested involve copying the attachments from one record to the other, which you really do not want to do (synching problems, duplicate records for no reason, etc...).   My solution is to create a Defined Related List (Create defined related lists) which can display attachments from multiple records.

 

We start by adding a new Relationship record (System Definition \ Relationships):

Name:                   Related Attachments
Applies to table:       Global [global]
Queries from table:     Attachment [sys_attachment]
Query with:

 

(function refineQuery(current, parent) {
  var tableName = parent.getTableName();
  var queryString = "table_name=" + tableName + " ^table_sys_id=" + parent.getValue("sys_id");   //default query

  switch (tableName){
    //add your table-specific blocks from below
  }

  current.addEncodedQuery(queryString);

  function u_getRelatedRecords(table, field, sysId){
    var result = "";
    var gr = new GlideRecord(table);
    gr.addQuery(field, sysId);
    gr.query();
    while (gr.next()){
      result += "," + gr.getValue("sys_id");
    }
    return result;
  }

})(current, parent);

 

The script checks the table name for the record being displayed and then builds the appropriate query.   As a safety measure, the queryString variable is given a default query to display the attachments for just that one record, otherwise all attachments would appear in the list if the Related List was added to a form that did not have any specific "case" block.   I created the private "u_getRelatedRecords" function to simplify the whole script as we use the same GlideRecord query to retrieve the appropriate sys_ids regardless of the table.

 

The above script is just the starting block - I'll add table specific examples next.   Each of the next blocks of code should be inserted within the "switch" block at line 6:

 

Request, Requested Item and Catalog Task Tables

 

      //===== Requests =====

      case "sc_request":

      queryString = "table_nameINsc_request,sc_req_item,sc_task^table_sys_idIN" + parent.getValue("sys_id");

      //find the related Requested Items
      queryString += u_getRelatedRecords("sc_req_item", "request", parent.getValue("sys_id"));

      //and then the Catalog Tasks
      queryString += u_getRelatedRecords("sc_task", "request_item.request", parent.getValue("sys_id"));

      break;


      //===== Requested Items =====
      case "sc_req_item":
      queryString = "table_nameINsc_request,sc_req_item,sc_task^table_sys_idIN" + parent.getValue("request") + "," + parent.getValue("sys_id");

      //find the related Catalog Tasks
      queryString += u_getRelatedRecords("sc_task", "request_item", parent.getValue("sys_id"));

      break;

 
      //===== Catalog Tasks =====
      case "sc_task":
      queryString = "table_nameINsc_request,sc_req_item,sc_task^table_sys_idIN" + parent.request_item.request.toString() + "," + parent.getValue("request_item");

      //find the related Catalog Tasks
      queryString += u_getRelatedRecords("sc_task", "request_item", parent.getValue("request_item"));

      break;

 

 

Incident and Service Desk Call Tables

 

      //===== Incidents =====
      case "incident":
      queryString = "table_nameINincident,new_call^table_sys_idIN" + parent.getValue("sys_id");

      //find the related New Call
      queryString += u_getRelatedRecords("new_call", "transferred_to", parent.getValue("sys_id"));

      break;


      //===== Service Desk Calls =====
      case "new_call":
      queryString = "table_nameINincident,new_call^table_sys_idIN" + parent.getValue("sys_id") + "," + parent.getValue("transferred_to");

      break;

 

 

Idea and Demand Tables

 

      //===== Idea =====
      case "idea":
      queryString = "table_nameINidea,dmn_demand^table_sys_idIN" + parent.getValue("sys_id") + "," + parent.getValue("demand");

      break;


      //===== Demand =====
      case "dmn_demand":
      queryString = "table_nameINidea,dmn_demand^table_sys_idIN" + parent.getValue("sys_id") + "," + parent.getValue("idea");

      break;

 

 

Project and Project Task Tables

 

  //===== Project =====
  case "pm_project":
  queryString = "table_nameINpm_project,pm_project_task,idea,dmn_demand^table_sys_idIN" + parent.getValue("sys_id");
 
  //find the related Project Tasks
  queryString += u_getRelatedRecords("pm_project_task", "top_task", parent.getValue("top_task"));

  //find the related Idea and Demand
  queryString += u_getRelatedRecords("dmn_demand", "project", parent.getValue("sys_id"));
  queryString += u_getRelatedRecords("idea", "demand.project", parent.getValue("sys_id"));

  break;


  //===== Project Task =====
  case "pm_project_task":
  queryString = "table_nameINpm_project,pm_project_task,idea,dmn_demand^table_sys_idIN" + parent.getValue("top_task");
 
  //find the related Project Tasks
  queryString += u_getRelatedRecords("pm_project_task", "top_task", parent.getValue("top_task"));

  //find the related Idea and Demand
  queryString += u_getRelatedRecords("dmn_demand", "project", parent.getValue("top_task"));
  queryString += u_getRelatedRecords("idea", "demand.project", parent.getValue("top_task"));

  break;

 

 

HR Case and HR Task Tables

 

      //===== HR Case =====
      case "hr_case":
      queryString = "table_nameINhr_case,hr_task^table_sys_idIN" + parent.getValue("sys_id"); 

      //find the related HR Tasks
      queryString += u_getRelatedRecords("hr_task", "parent", parent.getValue("sys_id"));

      break;


      //===== HR Tasks =====
      case "hr_task":
      queryString = "table_nameINhr_case,hr_task^table_sys_idIN" + parent.getValue("sys_id") + "," + parent.getValue("parent");

      break;

 

 

Now you can see all the attachments from related records if you add the "Related Attachments" Related List to a form:

 

find_real_file.png

 

The above blocks of code are just examples of what you can do and there are quite a few more that can be added.   I'll add some more as I come across some more ideas or people ask for more.

 

If you want a better looking and more useful list view, you will want to read this post - TNT: Improving the Attachments List View:

 

find_real_file.png

 

You will be able to see the record the attachment is actually on (instead of a sys_id), and even click on the link to go to that particular record.

 

NOTE: My earlier blog post, A Better Requested Item Attachments Related List, got a little messy so I split it into 2 different posts so it would be easier to read and update if required.   This post is the first of those 2 new posts.

71 Comments