Copy Attachment with out duplicates

nayeem2
Giga Expert

Hi All,

 

I am trying to copy attachment from task table to request with out duplicates

 

can any one help me

 

thanks in advance

35 REPLIES 35

Alan,



        Wow, this is great!   I was looking for a simpler design, but what you have is a better option.   I'll test it out tomorrow!   Thank you.



Shane


It's been very helpful for us since it also walks up from an approval record to what is being approved and gets all of its attachments as well.   We have a homegrown audit workflow where business users get an approval for an RITM and what they are approving is actually whatever's attached on one of the child sc_tasks and this works to bring that attachment that lives on the sc_task up to the RITM and then over to the approval without duplicates!   You just gotta make sure approvers have read permissions (ACL) on all those task levels for what they're approving.


***MODIFIED INFORMATION WITHIN THIS POST TO INCLUDE THE CORRECT DESIGN**



Alan,



      Your design is amazing!   I changed the code as needed for the HR tables and it is now showing the related list "HR Task Attachments" on the HR Case form.



FULL DESIGN:



Related List


System Definition > Relationships


Name:   HR Task Attachments


Applies to table:   HR Case [hr_case]


Queries from table: Attachment [sys_attachment]


(function() {


  var csfam = commaSeparatedTaskFamily('hr_task',parent.sys_id); //in relationships the parent is whatever is displaying this list


  var famarray = csfam.split(',');


  var hgr = current.addQuery('table_sys_id',famarray[0]); //first record's attachments


  for (var i=1; i<famarray.length; i++){


  hgr.addOrCondition('table_sys_id',famarray[i]); //then gets the rest


  }


})();




Script Include:


Name:   commaSeparatedTaskFamily


Accessible from:   All application scopes


function commaSeparatedTaskFamily(fromtable, recid){


  var family = '';


  //first has to find sys_id of target parent


  /*if (fromtable == 'hr_task'){


  var gr = new GlideRecord('hr_task');


  gr.get(recid);


  family = gr.parent; //gets parent's sys_id since its a reference


  }*/


  if (fromtable == 'hr_case'){


  family = recid; //itself is the parent


  }


  /* if (fromtable == 'sysapproval_approver'){


  var gr = new GlideRecord('sysapproval_approver');


  gr.get(recid);


  family = gr.sysapproval; //gets the approval_for's sys_id since its a reference


  }*/


  var ngr = new GlideRecord('hr_task');


  ngr.addQuery('parent',family);


  ngr.orderByDesc('number');


  ngr.query();


  while (ngr.next()){


  family += ',';


  family += ngr.sys_id;


  }


  return family;


}



Result of design:


  1. Open an HR Case that has at least 1 HR Task with an attachment.
  2. Scroll to bottom where related lists are
  3. See HR Task Attachments tab
    1. Attachment(s) show!

HRTask Attachments.PNG


Yeah, your code looks good and by clicking the 1.PNG or whatever for file name should start the download of the item unless for some reason an ACL is not allowing it.   If you're a full admin you should be able to though.   The list should work the same way sys_attachment.list does, so if you can go there and click on file names, the related list should also work.


I can go to sys_attachment.list, click the attachment, and it opens the file.  




When I right-click the file name in the related list, choose Copy URL to Clipboard, and then paste the URL in another browser tab, it opens the file.  




Unfortunately, when I click the file name in the related list, it gives a page that says "Secure Connection Failed."  




I notice that my column Table sys ID has a different sysID for each HR Task the attachment comes from.   Does yours work the same?




In your script include on line 7, you have request_item.   Where did you get that name from because it's not the table name?




Thank you for your help!