copy attachment from sys_attachment record to sc_task record

Kumbham Pravee1
Tera Contributor

what's wrong in this code?

I am trying to attach file from sys_attachment to sc_task

below is the code:(Business rule)

table:sys_attachment

after insert

(function executeRule(current, previous /*null when async*/) {

var reqnumber;

var gr1;

var gr2;

var reqitemnumber;

var sysid;

var uv = current.getUniqueValue();

gs.addInfoMessage(uv);

var s1 = current.getValue('table_sys_id');

var gr = new GlideRecord('sc_request');

gr.addQuery('sys_id',s1);

gr.query();

if(gr.next()){

reqnumber = gr.getUniqueValue();

gs.addInfoMessage(reqnumber);

gr1 = new GlideRecord('sc_req_item');

gr1.addQuery('request',reqnumber);

gr1.query();

if(gr1.next()){

reqitemnumber = gr1.getUniqueValue();

gs.addInfoMessage(reqitemnumber);

gr2 = new GlideRecord('sc_task');

gr2.addQuery('request_item',reqitemnumber);

gr2.query();

while(gr2.next()){

sysid = gr2.getUniqueValue();

gs.addInfoMessage(sysid);

GlideSysAttachment.copy('sys_attachment', uv, 'sc_task', sysid);

}

}

}

})(current, previous);

6 REPLIES 6

sachin_namjoshi
Kilo Patron
Kilo Patron

This seems to be issue with attachment API.


Please see below



Using GlideSysAttachment.copy only copies one attachment when there are multiple on the source recor...



Regards,


Sachin


kristenankeny
Tera Guru

I believe you want to set your unique value (uv) to the sc_request, not the attachment itself. https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=c_GlideSysAttachmentScopedAPI



However, best practice would to not copy the attachment, but to make it visible on the task in a related list, as outlined in jim.coyne's post here: "Related Attachments" Related List


But If I do soo, if i click on the file it is getting downloaded but can we show it in a tab or (like manage attachments [view] link) instead of downloading the file.


You could create a UI Action (it would need to be a list context menu, to ensure it's running for a single attachment) that calls a UI page that mimics the UI macro they have out of box.



<!-- in some cases we want a "view" link to be displayed after the file name -->


    <j:if test="${jvar_show_link}">


      <j:choose>


  <j:when test="${gs.getProperty('glide.ui.attachment_popup')=='false'}">


                  <a class="attachment" href="sys_attachment.do?sys_id=${sys_attachment.sys_id}&amp;view=true"> ${gs.getMessage('[view]')}</a>


      </j:when>


      <j:otherwise>


                  <a class="attachment" click-on-enter="true" href="#" onclick="tearOffAttachment('${sys_attachment.sys_id}');">${gs.getMessage('[view]')}</a>


      </j:otherwise>


      </j:choose>


    </j:if>