- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2019 05:38 AM
Hi Team,
I am trying to attach an image which is stored in servicenow database from workflow while inserting catalog item.
I am not sure how to attach an image using script. Please can anyone help me on this.
Thanks in Advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2020 06:43 AM
Can you close this thread as answered by marking appropriate response as correct so that this question moves to answered list and helps future readers as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2019 08:17 AM
What have you tried?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2019 08:59 AM
Here is the code that populates the picture field on new catalog item with attachment on the submited item
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
gr.query();
while(gr.next()){
gr.file_name = 'picture';
gr.table_name = 'ZZ_YYsc_cat_item';
gr.table_sys_id = sysid; //sysid is the sys_id of the newly created catalog item
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2019 09:38 AM
Hi Praveen,
If you want to do this using workflow then you can do this once RITM is created. what following script does is searches for the attachment record for that image in sys_attachment table with that table name and sys id and then creates fresh record in sys_attachment table and copies same data. along with this it also creates entry in sys_attachment_doc table which holds meta data for the image file. the following script is tested and is working fine. just replace the proper values for image record sys id and RITM sys id highlighted in bold
script:
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'ZZ_YYdb_image');
gr.addQuery('table_sys_id','<sysIdOfImageRecord>'); // search for the image in db_image table and add the record sys id here
gr.query();
if(gr.next()){
var gr1 = new GlideRecord('sys_attachment');
gr1.initialize();
gr1.file_name = gr.file_name; //replace the image field name
gr1.content_type = gr.content_type;
gr1.compressed = gr.compressed;
gr1.table_name = 'sc_req_item';
gr1.size_bytes = gr.size_bytes;
gr1.size_compressed = gr.size_compressed;
gr1.table_sys_id = current.sys_id; // ritm sys id
var attRec = gr1.insert();
}
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', gr.sys_id);
attDoc.query();
while(attDoc.next()){
var attDocCopy = new GlideRecord('sys_attachment_doc');
attDocCopy.initialize();
attDocCopy.sys_attachment = attRec;
attDocCopy.position = attDoc.position;
attDocCopy.length = attDoc.length;
attDocCopy.data = attDoc.data;
attDocCopy.insert();
}
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 11:24 PM
Hi Praveen,
Any update on this?
Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader