Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Servicenow fetch attachment

venkatk2
Giga Contributor

Hi,

 

Please help me with following query -

 

How do I fetch an attachment to use it somewhere else that is being attached to a new created record on ServiceNow?

4 REPLIES 4

p_kanczugowski
Tera Guru

Hi @venkatk2, once the file is uploaded, you can copy it using this code:

var SOURCE_RECORD_ID = 'ABC';
var TARGET_RECORD_ID = 'DEF';
var SOURCE_TABLE = 'incident';
var TARGET_TABLE = 'sc_task';

new GlideSysAttachment().copy(SOURCE_TABLE, SOURCE_RECORD_ID, TARGET_TABLE, TARGET_RECORD_ID);

 

Please mark this response as useful and the question as solved if the issue has been solved. Thank you!

venkatk2
Giga Contributor

Hi @p_kanczugowski  ,
   we are looking at using the attachment externally as part of api and are looking for a way to fetch the attachment Sys_ID from the record Sys_ID.  thanks for the above response , we will test this for other use cases.  

Hi @venkatk2 

 

Do you want to fetch the sys_id of the attachment on a record from a REST API call ?

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Shailesh Kumar1
Tera Contributor

Hi @venkatk2 

Copy Attachment: Use this script

 var attRec = new GlideRecord('sys_attachment');

  attRec.addQuery('table_sys_id','sys_id');//sys_id of the record in which the attachment is attached

  attRec.query();

  while(attRec.next()){

    //provide the table name and the reord sys_id to which you want to attach the copied attachment

    new global.VariableUtil().copyAttachment(attRec.sys_id, 'table name', 'record sys_id');

  }