How to copy an attachment to an Image field within the same form?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2016 08:18 PM
Hello guys, I already have a field in a form that is Image type, now I would like that the attachments (it would only be images attached to this records) be copied to my image field(It would only be one attachment per record so it should be one attachment and one image) is there any easy way to do it? I thought about a business rule that checks when I have attachements I get it right! but I got stuck thinking how to move this attachment to my image field! any ideas???
Thank you very much!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2016 08:56 PM
Add a after insert business rule on sys_attachment table with the condition as below
current.table_name == 'u_custom_table' //replace tablename
Use the below script
var att = new GlideRecord('sys_attachment');
att.initialize();
att.file_name = 'u_image'; //replace the image field name
att.content_type = current.content_type;
att.compressed = current.compressed;
att.table_name = 'ZZ_YY'+current.table_name;
att.size_bytes = current.size_bytes;
att.size_compressed = current.size_compressed;
att.table_sys_id = current.table_sys_id;
var attRec = att.insert();
var attDoc = new GlideRecord('sys_attachment_doc');
attDoc.addQuery('sys_attachment', current.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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2016 11:08 AM
Thank you Kalairasan!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2016 08:46 PM
If the query is resolved, please close the question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2017 03:05 AM
This works well, but has the limitation that the image field isn't immediately refreshed when the image is attached. Just wondering, Anyone have any ideas how to trigger an immediate refresh of the image field?
Regards
Paul