Copy Attachment from SC Task to RITM

raj99918
Tera Contributor

Hi ,

 

How do Copy Attachments from SC tasks to RITM (may be 1 or 2 or 3 tasks exists for all the tasks).?

 

 

2 REPLIES 2

Adrian Ubeda
Mega Sage
Mega Sage

Hello @raj99918 , 

You can use a Business rule onBefore for this applied on sc_task table and then use the function GlideSysAttachment.copy (https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc...). Your BR should be something similar as follows:

/*Table - sc_task
When to Run - Before Update
Condition - Send Attachments to True
Scripts - */
(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here{
        GlideSysAttachment.copy('sc_task', current.getUniqueValue(), 'sc_req_item', current.parent.getUniqueValue());
})(current, previous);

There's also an action into flow designer called 'Cpy attachment'. You can use flow designer for this instead BR, as you prefer.

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

swathisarang98
Giga Sage
Giga Sage

Hi @raj99918 ,

 

You can write below BR ,

Table: sys_attachment

condition : after Insert

 

 

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

    var taskRec = new GlideRecord('sc_task');

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

    if (taskRec.next()) {

        var attachment = new GlideRecord('sys_attachment');

        attachment.addQuery('table_sys_id', current.table_sys_id);
        attachment.orderByDesc('sys_created_on');
        attachment.query();

        if (attachment.next()) {

            var ritmRec = new GlideRecord('sc_req_item');

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

            if (ritmRec.next()) {

                var gsa = new GlideSysAttachment();
                gsa.writeContentStream(
                    ritmRec,
                    attachment.file_name,
                    attachment.content_type,
                    gsa.getContentStream(attachment.sys_id));
            }

        }
    }

})(current, previous);

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang