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

Hello,

Please help me with the script. 

var queryString = "table_nameINsc_task^table_sys_idIN" + current.sys_id;


    var taskGR = new GlideRecord("sc_task");
    taskGR.addQuery("request_item", current.sys_id);
    taskGR.query();
    while (taskGR.next()) {
        queryString += "," + taskGR.sys_id.toString();
    }
    current.addEncodedQuery(queryString);

This is not working.

suvro
Mega Sage
Mega Sage

You can use the below line of code

GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');

@Ranjit Singh  @Ranjit Singh Rathore  

Did you try this ???

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I would suggest to avoid copying the files as it would increase the size of sys_attachment table

Instead you can refer this article and show the attachments in the related list.

"Related Attachments" Related List

Regards
Ankur

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

@Ranjit Singh Rathore 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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