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.

Unable to copy attachment from RITM variable to RITM itself

Haritej
Tera Contributor

Hi Community,

I have created a attachment variable for catalog item. When I submit the request by attaching a file to variable it is getting stored in sys_attachment table with table name as ZZ_YYsc_cart_item and table sys_id as catalog item sys_id. 

I want to copy this same attachment from variable to RITM record itself and as well as to a email notification. But i am unable to Query my RITM record from sys_attachment table and copy the attachment.

Any suggestions would really help me!

5 REPLIES 5

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Haritej 

I can understand this pain as once I was a developer I faced the same issue.
You can use the sys_id of the catalog item to query the RITM record. Since the attachment is stored in the sys_attachment table with the table name as ZZ_YYsc_cart_item and the table sys_id as the catalog item sys_id, you can use the following query to get the RITM record:
......................
Let make it super simple assume you have to write a SQL how would you write.. Something like below :-
SELECT *
FROM ritm
WHERE sys_id = <catalog_item_sys_id>

Lets convert it into glide scripting

var ritm = new GlideRecord('ritm');
ritm.get('<catalog_item_sys_id>'); // Replace with the actual sys_id

var attachment = new GlideRecord('sys_attachment');
attachment.get('table_name', 'ZZ_YYsc_cart_item');
attachment.get('table_sys_id', ritm.sys_id);

// Copy the attachment to the RITM record
ritm.attachment = attachment.file;
ritm.update();

follow the video :- https://www.youtube.com/watch?v=4P-AUaNQ8EE

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/