How to copy attachment from sctask to ritm

Praveen94
Kilo Contributor

How to copy attachment from sctask to ritm

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Praveen,

I assume you want attachment to be copied over from SC Task to RITM when attachment is added

sample BR on sys_attachment; It would be copying all attachments to RITM from SC TASK

BR: after insert on sys_attachment

Condition: current.table_name == 'sc_task'

Script:

var taskRec = new GlideRecord('sc_task');

taskRec.addQuery('sys_id', current.table_sys_id);

taskRec.query();

if(taskRec.next()){

var ritmRec = new GlideRecord('sc_req_item');

ritmRec.addQuery('sys_id',taskRec.request_item);

ritmRec.query();

if(ritmRec.next()){

GlideSysAttachment.copy('sc_task', current.table_sys_id, 'sc_req_item', ritmRec.sys_id);

}

}

Regards
Ankur

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

View solution in original post

18 REPLIES 18

Hi Ankur,

 

Every time BR runs duplicate attachment is attaching to RITM as per provided script.

Step 1: attaching to sctask update to Ritm

Step2: Again attaching different attachment to same sctask again it's copying all attchments to RITM.

 

Thanks

Praveen

 

 

Here is the code I use for the same purpose, use it in the script section and set the condition as current.request_item.cat_item == 'sys ID of your item'

 

moveRitmAttachments();

function moveRitmAttachments() {
    var attachmentGr = new GlideRecord('sys_attachment');
    attachmentGr.addQuery('table_name','sc_task');
    attachmentGr.addQuery('table_sys_id',current.sys_id);
    attachmentGr.query();
    while (attachmentGr.next()) {
	attachmentGr.table_name = 'request_item';
	attachmentGr.table_sys_id = current.sys_id;
	attachmentGr.update();
    }    
}

 

If my reply has helped in any way, kindly mark it correct/helpful. Thanks!

Hi Praveen,

please delete older attachments and then fresh copy all

updated script for both the questions


var taskRec = new GlideRecord('sc_task');

taskRec.addQuery('sys_id', current.table_sys_id);

taskRec.query();

if(taskRec.next()){

var ritmRec = new GlideRecord('sc_req_item');

ritmRec.addQuery('cat_item.name', 'YOUR CATALOG ITEM NAME HERE');

ritmRec.addQuery('sys_id',taskRec.request_item);

ritmRec.query();

if(ritmRec.next()){

// delete all RITM Attachments before copy

var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', ritmRec.sys_id);
att.query();
att.deleteMultiple();

GlideSysAttachment.copy('sc_task', current.table_sys_id, 'sc_req_item', ritmRec.sys_id);

}

}

Regards
Ankur

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

Hi Ankur,

Is there a way to retain the attachments in the RITM? Currently the code is replacing all the attachments in the RITM from the sctask. Thank you

Regards,

Raph

Hi @Ankur Bawiskar ,

I have used similar code to copy attachments from SC Task to RITM table but the problem is the attachments in the sc task are from "Attachment" variable.

So, instead of 

GlideSysAttachment.copy('sc_task', current.table_sys_id, 'sc_req_item', ritmRec.sys_id);

I have used

GlideSysAttachment.copy('ZZ_YYsc_task', current.table_sys_id, 'sc_req_item', ritmRec.sys_id);

 

Now, attachment is getting copied from sc task to activity stream of RITM but not to the paperclip icon on the header. I want that to be attached to paperclip on the RITM table. Do you have any suggestions on this?

Also, I don't see the attachment variable on the sc task once the task is closed complete, it would be good to have it on the task even it's closed. 

 

FYI - we are currently on Paris version and the catalog item is in a scoped application.

 

Thanks,

Kat