How to duplicate a task attachment in RITM

Ariomar de Deus
Tera Contributor

I created a BR to duplicate the Task attachments for RITM, but the code doesn't work. Could you check what's happening with my script?

The rule is pointed to sc_task and is set to after, and execution is marked as insert and update.

(function executeRule(current, previous /*null when async*/ ) {
 
    GlideSysAttachment.copy('sc_task', current.sys_id, 'sc_req_item', current.request_item);
    gs.info('@@@anexo' + current.number);
 
})(current, previous);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Ariomar de Deus 

ideally you should have after insert BR on sys_attachment table so that whenever file is added on sc_task it gets copied to sc_req_item.

BR: After insert "sys_attachment"

Conditions: current.table_name == 'sc_task'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord("sc_task");
    gr.addQuery("sys_id", current.table_sys_id);
    gr.query();
    if (gr.next()) {
        new GlideSysAttachment().copy("sc_task", gr.getUniqueValue(), 'sc_req_item', gr.request_item.toString());
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

4 REPLIES 4

jcmings
Mega Sage

You may need to include this line at the top of your code:

var attachment = new GlideSysAttachment();

 

And then change the existing .copy() line to this:

  attachment.copy('sc_task', current.sys_id, 'sc_req_item', current.request_item);

 

Source: https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...

 

Even making this adjustment didn't work.

Ankur Bawiskar
Tera Patron
Tera Patron

@Ariomar de Deus 

ideally you should have after insert BR on sys_attachment table so that whenever file is added on sc_task it gets copied to sc_req_item.

BR: After insert "sys_attachment"

Conditions: current.table_name == 'sc_task'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord("sc_task");
    gr.addQuery("sys_id", current.table_sys_id);
    gr.query();
    if (gr.next()) {
        new GlideSysAttachment().copy("sc_task", gr.getUniqueValue(), 'sc_req_item', gr.request_item.toString());
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Ariomar de Deus 

Another approach I will suggest is to show attachment related list on RITM.

This will avoid adding same file at 2 places and will save Attachment table size

check this link

TNT: "Related Attachments" Related List 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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