Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

I want to copy an attachment, but how do I do that?

Tsunemasa Matsu
Tera Contributor

Hi,

I want to copy an attachment, but how do I do that?

Is it possible to do this using GlideSysAttachment?

If not, another method is fine.

 

Copy source:Files attached to the file attachment type column of the abccabc table in the abc application scope

Copy destination:Record of the efgefg table in the abc application scope

 

Thanks.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Tsunemasa Matsu 

Your traditional GlideSysAttachment.copy() won't work here i.e. record to record

you need to tweak the logic since you want to copy from Attachment type Field to target record

Example: you can use this server side code in business rule on abc table on record insert and it will add that file to other table record

Note: you need to enhance it further

I did it in background script and it should work in scoped app as well

var targetRecord = new GlideRecord("change_request");
targetRecord.addQuery("sys_id", "6ae8a3272b0366d04a2bfbfdf291bf12");
targetRecord.query();
if (targetRecord.next()) {

    var sourceRecord = new GlideRecord('incident');
    if (sourceRecord.get('00c01445fb5532500db5f8454eefdcc9')) {
        new global.VariableUtil().copyAttachment(sourceRecord.u_my_file, 'change_request', targetRecord.sys_id);
    }
}

Output:

copy file from attachment field to target record.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Tsunemasa Matsu 

 

https://www.servicenow.com/community/developer-forum/copy-attachment-from-table-a-to-table-b-in-scop...

 

*************************************************************************************************************
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]

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

@Dr Atul G- LNG 

Thank you for your reply.
When I tried to copy using the logic below, when I looked at the sys_attachment table, the destination table name had ZZ_YY attached to it.
I would like to copy the data without the ZZ_YY attached to the destination table.

-------------------------------------------------------------------------------------------------------------

var attachment = new GlideSysAttachment();
var targetSysID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

var copiedAttachments = attachment.copy('ZZ_YYx_xxxxxxx_abc_abcabc', current.sys_id, 'x_xxxxxx_abc_efgefg', targetSysID);

Ankur Bawiskar
Tera Patron
Tera Patron

@Tsunemasa Matsu 

Your traditional GlideSysAttachment.copy() won't work here i.e. record to record

you need to tweak the logic since you want to copy from Attachment type Field to target record

Example: you can use this server side code in business rule on abc table on record insert and it will add that file to other table record

Note: you need to enhance it further

I did it in background script and it should work in scoped app as well

var targetRecord = new GlideRecord("change_request");
targetRecord.addQuery("sys_id", "6ae8a3272b0366d04a2bfbfdf291bf12");
targetRecord.query();
if (targetRecord.next()) {

    var sourceRecord = new GlideRecord('incident');
    if (sourceRecord.get('00c01445fb5532500db5f8454eefdcc9')) {
        new global.VariableUtil().copyAttachment(sourceRecord.u_my_file, 'change_request', targetRecord.sys_id);
    }
}

Output:

copy file from attachment field to target record.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

Thank you for your reply.

I was able to solve the problem using the method you provided.