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.

How to copy attachment from one record to other

Ranjit Singh Ra
Tera Expert

Hello,

I have a basic requirement of adding an attachment from sc_task to its RITM. How do I achieve it?

 

I have written a After Insert/Update BR on sc_task table with this script:

var gr = new GlideRecord("sc_req_item");
    gr.addQuery("sys_id", current.request_item);
    gr.query();
    if (gr.next()) {
        if (gr.cat_item.category.getDisplayValue() == 'Departmental Services') {
            var attachment = new GlideSysAttachment();
            var copiedAttachments = attachment.copy('sc_task', current.sys_id, 'sc_req_item', gr.sys_id);
        }
    }

This works only when the user add the attachment to sc_task and Saves the record. However I do not wish to save the record because that is unnecessary having the user to click.

So I wrote another After Insert/Update BR on sys_attachment table with thisscript:

var taskGR = new GlideRecord("sc_task");
    taskGR.addQuery("sys_id", current.table_sys_id);
    taskGR.query();
    if (taskGR.next()) {
        var ritmGR = new GlideRecord("sc_req_item");
        ritmGR.addQuery("sys_id", taskGR.request_item);
        ritmGR.query();
        if (ritmGR.next()) {
            if (ritmGR.cat_item.category.getDisplayValue() == 'Departmental Services') {
                var attachment = new GlideSysAttachment();
                var copiedAttachments = attachment.copy(current.table_name, current.table_sys_id, 'sc_req_item', ritmGR.sys_id);
            }
        }

 

And this is running in Infinite loop (I saw 500+ attachments got added to the RITM)

 

What is the best possible solution to my requirement?

 

Thank you.

9 REPLIES 9

Jaspal Singh
Mega Patron
Mega Patron

Hi,

BR on Attachment table needs to be only on insert. No need of update.

Also, instead of copying attachment & duplicating why not use a relationship. Refer article for a check.

You can alter it as per your need.

hello jaspal,

can you help me 

https://community.servicenow.com/community?id=community_question&sys_id=41613ced1b2cdd90a59033f2cd4bcbcb

Something is seriously wrong.

I did the exact same way what you suggested:

find_real_file.png

find_real_file.png

 

With this, I am still getting to Infinite Loop. 

i tried  https://<INSTANCE>.service-now.com/cancel_my_transaction.do

But it not getting cancelled as well. 

 

Where I must be getting wrong? Please help!

@jaspal 

I referred to the article you shared but not sure why the attachment is still not visible:

find_real_file.png

 

Please help me with the script. Thank you in advance!