UI Action to copy attachment from SCTASK to RITM

Samiksha2
Mega Sage

Hi All,

 

I have a requirement to copy attachment form sctask to ritm but not all attachments. So for that we need toadd a button in the sctask. When a fulfiller add the attachment and click on the button the uploaded(recent) attachment should be copied to RITM.

 

Please help.

 

Thanks,

Samiksha

2 ACCEPTED SOLUTIONS

Hemanth M1
Giga Sage
Giga Sage

Hi @Samiksha2 ,

 

Refer here: https://www.servicenow.com/community/developer-forum/copy-newest-attachment-only-from-task-to-ritm/m... 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

Hi @Samiksha2 ,

 

Copy past the exact code in your UI action script section, which you will be creating on sc_task table:  

var taskRec = new GlideRecord('sc_task');

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

if (taskRec.next()) {

    var attachment = new GlideRecord('sys_attachment');

    attachment.addQuery('table_sys_id', current.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));
        }

    }
}

 

Please mark @Hemanth M1 answers correct if it helps u resolve your query,

 

Thanks,

Danish

View solution in original post

7 REPLIES 7

Hemanth M1
Giga Sage
Giga Sage

Hi @Samiksha2 ,

 

Refer here: https://www.servicenow.com/community/developer-forum/copy-newest-attachment-only-from-task-to-ritm/m... 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Hi @Hemanth M1 ,

How to add the above code in UI action?

 

Thanks,

Samiksha

Hi @Samiksha2 ,

 

Copy past the exact code in your UI action script section, which you will be creating on sc_task table:  

var taskRec = new GlideRecord('sc_task');

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

if (taskRec.next()) {

    var attachment = new GlideRecord('sys_attachment');

    attachment.addQuery('table_sys_id', current.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));
        }

    }
}

 

Please mark @Hemanth M1 answers correct if it helps u resolve your query,

 

Thanks,

Danish

Hi @Danish Bhairag2 ,

I added the code in UI action dont know why that was not running. I copied your code its working.

Thanks.😊