Copying Attachment from One field to another table.

Ubada Barmawar
Giga Guru

Hi Experts, 

 

I had a use case where I had to copy attachment from one table to another table so I used the script below.

 

var attachment = new GlideSysAttachment();

var attached = attachment.copy('source_table','source_table_sys_id', 'sys_data_source','datasource_sys_id' );

 

now that I have to copy an attachment from field on a custom table(file attachment type) to data source, the above script doesnt work as expected. the table name in sys_attachment appears as ZZ_YY appended by custom table name. Im not even able to copy field attachment to the same form.

 

any responses are highly appreciated

 

regards,

Ubada Barmawar

1 ACCEPTED SOLUTION

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Ubada Barmawar ,

 

What you have attempted is right yes need to created a business rule on change of attachment field you need to take the table name and sys_id of the attachment and copy it on target table using attachment.copy. 

 

From the field which has attachement get the name of the file and do a glide record on sys_attachment table once the record attachment is found in query initialize the attachment on the target table were you needed it..

 

here is the sample code :

var gr = new GlideRecord('xxxx');
gr.get('f847adac1b40d110be52419fe54bcbca');
var fSys = gr.u_current_file.toString();
gs.info("File sys id: " + fSys);

var gAtach = new GlideRecord('sys_attachment');
gAtach.get(fSys);
gs.info("File Name:" + gAtach.file_name);

var targetRef = new GlideRecord('your_table');
targetRef.get('target_sys_id');

//Example of script

var glideSysAttachmentRef = new GlideSysAttachment();
var guid = "";
var newFileName = gAtach.getValue("file_name");

guid = glideSysAttachmentRef.writeContentStream(targetRef, newFileName, gAtach.getValue("content_type"), glideSysAttachmentRef.getContentStream(gAtach.getUniqueValue()));

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

View solution in original post

8 REPLIES 8

Mark Manders
Mega Patron

Try the flow action 'copy attachment'. You can just give the source and the target and it will copy it for you.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

copy attachment takes attachment from record and not from field.

UbadaBarmawar_0-1712314995390.png

 

Amit Pandey
Kilo Sage

.

This code is AI generated and it doesn't work!