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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Ubada Barmawar 

 

What do mean by field to field copy? OOTB provide table to table. What is use case.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

I have a custom table in a scoped application, on that custom table I have a field (type: attachment). what I want to do is ....I want to copy this attachment to datasource when the form is submitted, to automate the import.

but the problem is copying of attachment from a table to another table works different than copying attachment from a field to a table. do you think I will be able to do that through any script?? bcus "copy" method doesnt seem to work.

 

usecase is of automating the import process from an attachment to various tables, approach adopted >>> scheduled data import(inactive, triggered by script)>datasource>transform map. achieved through flow designer.

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

will try it 👍