How to copy an attachment to an Image field within the same form?

joseeduardo
Giga Expert

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!

5 REPLIES 5

Kalaiarasan Pus
Giga Sage

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();


  }


Thank you Kalairasan!


If the query is resolved, please close the question


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?


***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul