alan_lowrance
Mega Guru

We often are working catalog tasks and see that someone mentioned that something's attached and were sick of climbing up to the parent (sc_req_item) and possibly up again to the request in order to see that attachment (and any other tiered tickets like change_request/change_task, etc.).   So I made a related list that you can put in its own content tab on any tables you wish to see more associated attachments on.

The only thing I would say to look out for is make sure that your Requested Items have a parent field pointing to the Request and not just the Request reference field.   To do this, I added a script activity in the REQ workflow to search for its own children and set their 'parent' as itself.   Seems that all other task-type tickets in the system use the parent field already, but if your REQ is created by bundling all the RITMs, the linkage won't be there unless its WF fills it.

First we need the back-end logic [non-client callable SCRIPT INCLUDE] that all the other components will use to make the list of related attachments:

function commaSeparatedTaskFamily(fromtable, recid){

  var family = ''; //building comma-separated list of related sysids

  var gr = new GlideRecord('task');

  if (fromtable == 'sysapproval_approver'){ //sysapprovals are different since they don't have parents

  var aprv = new GlideRecord('sysapproval_approver');

  aprv.get(recid);

  gr.get(aprv.sysapproval);

  family += (gr.sys_id + ', ');

  }

  else

  gr.get(recid); //gr will be the source record

  //STEP 1 up & across

  if (gr.parent){

  family += (gr.parent + ', ');

  var pfind = new GlideRecord('task');

  pfind.get(gr.parent);

  while (pfind.parent){ //not sure if .get() inside the loop will change this or not

  family += (pfind.parent + ', ');

  pfind.get(pfind.parent);

  }

  if (fromtable != 'sysapproval_approver'){ //we don't want approvals seeing siblings

  var sfind = new GlideRecord('task');

  sfind.query('parent',gr.parent);

  while (sfind.next()){

  if (sfind.sys_id != gr.sys_id) //don't want it finding itself

  family += (sfind.sys_id + ', ');

  }

  }

  }

  //STEP 2 down & down

  var cfind = new GlideRecord('task');

  cfind.query('parent',gr.sys_id);

  while (cfind.next()){

  family += (cfind.sys_id + ', ');

  var gcfind = new GlideRecord('task');

  gcfind.query('parent',cfind.sys_id);

  while (gcfind.next())

  family += (gcfind.sys_id + ', ');

  }

  return family;

}

Then, for every table you want to display this on, you'll need to create a [System Definitions > Relationships] entry that will query from the sys_attachment table.   Thanks Steve Bell (Cloud Sherpas) for simplifying this for me!

(function() {

  var csfam = commaSeparatedTaskFamily('WHATEVER TABLE THIS IS APPLIED TO',parent.sys_id);//parent is just what is displaying this list

  current.addQuery('table_sys_id', 'IN', csfam);

})()

Then, similarly if you want to list all these attachments in an email notification you just add this to mail scripts and put into your template with ${mail_script:attach_links}

attachLinks();

function attachLinks() {

  var gr = new GlideRecord('sys_attachment');

  if (!current.sys_class_name) //APPROVALS DONT HAVE CLASS NAMES

    var rectype = 'sysapproval_approver';

  else

    var rectype = current.sys_class_name;

  var csfam = commaSeparatedTaskFamily(rectype,current.sys_id);

  gr.addQuery('table_sys_id', current.sys_id).addOrCondition('table_sys_id', 'IN', csfam);

  gr.query();

  if(gr.hasNext()){

    template.print("Attachments: ");

    while (gr.next()) {

      var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr.getTableName(),gr.sys_id) +   '">' + gr.file_name + '</a>';

      template.print(attachLink);

      if(gr.hasNext())

        template.print(",   ");

      else

        template.print("\n");

    }

  }

}

What it looks like on the record (for this case, the top list are its children, and the bottom show its sibling attachment (4th tier), both children's attachments (5th tier), parent's (3rd tier), parent's parent (2nd tier), and parent's parent's parent (top tier).   But it won't show it's sibling's children's attachments and won't show it's parent's sibling's or their children's.   In other words, no aunts/uncles, nieces/nephews, or cousins! (but its possible, I just didn't want to include those because they shouldn't be relevant to what task is currently being worked).

Capture.JPG

And here's what it looks like on the email (not same ticket so that's why number of attachments differ) and this is from an approval email, so it doesn't show any siblings for those because they shouldn't be relevant, you'd only be approving something directly on or under the sysapproval.

Capture2.JPG

9/14/15 EDIT: Noticed that approvals weren't showing the attachments of what they were approving so add line 8 to the Script Include.