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.

Getting Record Attachments via API

AleksaStojanov
Tera Contributor

Hi all,
Is there a way to get all attachments sys_ids for a specific record via API?

 

Thanks in advance!

3 REPLIES 3

Astik Thombare
Tera Sage
Tera Sage

Hi @AleksaStojanov  ,

 

Attachments are stored in sys_attachment table . so if you want to get all the attachment sys_id for particaular record please use below script -

 

 

var attachment = new GlideRecord('sys_attachment');


attachment.addQuery('table_sys_id',current.sys_id); // current record sys_id


attachment.query();


while(attachment.next()){


gs.info('sys_id of the attachment is : ' +attachment.sys_id);


}

 

 

if my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

 

Thanks,

Astik

lmahoney
Mega Contributor

Is it possible to do this with the REST API instead of through a ServiceNOW script?

 

I'm writing a python script that interacts with the ServiceNOW API and the goal of the script is to pull the attachments associated to a list of ServiceNow records. I'm stuck on trying to find the relationship between a given record and it's attachments.

 

I see our 'sys_attachment' table has fields 'table_name' and 'u_record' where:
- 'table_name': is the name of the table of the record the attachment is from

- 'u_record' is a 'Document ID' field that is a reference to a given record

 

I've been trying to supply the sys_id of the record I want to get the attachments of as the 'u_record' parameter but I haven't had luck. These were being supplied to the 'Get Table' endpoint as well as the 'Get Attachment Metadata' endpoint.

Hi @lmahoney,

It is posible, I used this approach
<instance_url>/api/now/table/sys_attachment?sysparm_query=table_sys_id=<record_id>