Display parent attachments in sc_task

Stephen W_
Giga Guru

I want to make sure that attachments from a request item are visible on tasks.

I know I can easily copy them to the tasks, but unnecessary duplication and synchronizing issues make this less desirable.

I could display an info message with a link to any attachments. (this might be the next best thing)

What I would really like to do is modify the attachment macro to include the RITM attachments along with any sc_task attachments so that whoever is working a task will have visibility to any attachments the requester may have placed on it.

Anyone done this before? (Or have a better idea)

Thanks,

-Stephen

1 ACCEPTED SOLUTION

Brian Dailey1
Kilo Sage

Today is your lucky day...   "Consolidated Attachments" is actually something I've been working on lately, using a defined Relationship as Anthony suggests.



Here is a defined relationship you can create for the [task] table that will show you attachments for ALL references (including parent, request, requested_item, parent_incident, etc.), as well as from any emails on the current record AND all the references just mentioned.   One nice thing is that the related list will also give you a nice label telling you which object the attachment comes from.   To do so:



  1. Go to System Definition>Relationships and create New
  2. Set Applies to table:   Task [task]
  3. Set Queries from table: Attachment [sys_attachment]
  4. For the "Query with" script, enter the following:


try{


      var itemList = getItemList();


      //Get attachments for all of the above


      current.addQuery('table_sys_idIN' + itemList);


}


catch(e){


      gs.log("Error(" + e.lineNumber + "): " + e, "CONSOLIDATED ATTACHMENTS");


}




function getItemList(){


      var arrItems=[],references=[],relTasks=[],relEmails=[];


      relTasks=[parent.sys_id];


  //Get References


      references = getReferences(parent);


  //Get Emails


      var instanceList = relTasks.concat(references);      


      relEmails = getRelatedEmails(instanceList.join());


      arrItems = instanceList.concat(relEmails);


      return arrItems.join(); //Return as a CSV list


}



function getReferences(parent){


  //Get referenced record_ids


      var refFields = parent.getFields();


      var itemListRefs = [];


      for (var i=0; i<refFields.size(); i++) {


              var field = refFields.get(i);


              var ed = field.getED();


              if(field.getED().getInternalType() == 'reference' && field.hasValue()){


                              itemListRefs.push(field.toString());


              }


      }


      return itemListRefs;


}



function getRelatedEmails(instanceList){


      var strItems = '';


      var grRelEmail = new GlideRecord('sys_email');


      grRelEmail.addQuery('instanceIN' + instanceList);


      grRelEmail.query();


      while(grRelEmail.next()){


              strItems += grRelEmail.sys_id + ',';


      }      


      return strItems.split(',');


}



After you've created this defined relationship, just go to Configure->Related Lists to add it to your form (to avoid redundancies, I would remove the standard "Attachments" related list for recundancy's sake).  



This is actually a trimmed-down version of what I've been working on... the full version includes attachments from all records that reference the current record as well (i.e., those that appear on its related lists).   But I'm still working on the efficiencies of that method, so haven't included it here.   The thing that makes it work nicely is that attachments rely on a document ID field... so you only need to use the item list of sys_ids to find all related attachments, and forget about what table they belong to.




Enjoy,


-Brian


View solution in original post

16 REPLIES 16

Hello Brian



This works for request, request item and request task but I can't get it to work for Project>Project Task>Change.



How do I get the Change to reference back to the Project?



Thanks


Hi Josh,



That depends on which way you're talking about going.... are you looking to see everything listed on the parent (top-level) Project?   If so, today is NOT your lucky day...



To get to Change from Project via Project Task is going to take either recursively querying a parent for children (i.e., get Project Tasks for this Project, then get Changes for all those Project Tasks), or checking the hierarchy on each Change to see if it belongs to either the Project or a Project Task that is attached to it.



If you look at the function getItemList(), you'll see an array that starts off with just the parent:         relTasks=[parent.sys_id]



I have a version tested that expands this to include all tasks related to the parent by all the various possible references, but it is quite the resource hog and done more for proof-of-concept...   I wouldn't recommend using it in production.   Additionally, it is only querying based on the immediate parent.   To use this recursively to check up the hierarchy would be unthinkable.



I haven't abandoned the idea of adding related tasks, but I've been diverted to some other purposes lately.   I'll give it more thought and post back here if I come up with something that might be of use to you.




Thanks,


-Brian


Hi Brian



Thanks for your response.



Well, I was hoping I could take a slightly different approach using this


link


https://community.servicenow.com/community/develop/blog/2015/08/11/a-better-requested-item-attachments-related-list



I just need to figure out what the scripting is, so that the relationship


is created between Change and 'top' Project, using a custom field on the


Change form with the Project Task's Project number in it.



There has a to be a way to do this...



Thanks again


Josh




On Thu, Jul 7, 2016 at 5:13 PM, wwar1ace <community-no-reply@servicenow.com>


Hi Brian!



I had a very similar issue, and your script worked like a charm, thank you!


One thing though - I got a message under the related list that some rows have been removed by security constraints, it turned out those were picture and icon of the Catalog Item itself - is there a way to modify this script/apply a filter to only include the attachments from the RITM/emails from user but not sth not directly related to the Catalog Task?



Thanks for your help!


Hi Joanna,



You can adjust the if() statement in the getReferences() function to check for the field name you wish to exclude and have it not get pushed to the item list.




Thanks,


-Brian