Copy Newest Attachment Only From Task to RITM

Chris Dea1
Tera Contributor

Hi all,

 

I followed this post and have a business rule that is copying attachments from Task to RITM.  It copies all attachments from Task to RITM each time a new attachment is added to Task.  For my scenario, I would only like the newest attachment to be copied over.  So currently for example if I add "Attach1.pdf" file to Task, it copies over to the RITM, but if I add a newer "Attach2.pdf" to the Task, both the previous "Attach1.pdf" and new "Attach2.pdf" are copied over to the RITM, creating duplicate of "Attach1.pdf".  Is there any way to have only the newest attachment get copied over from Task to RITM?

Below is the current script I am using in the BR:

 

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);

}

}

 

Anyone have any script to help me out? Thanks in advance for any help.

 

-Chris

1 ACCEPTED SOLUTION

Muhammad Khan
Mega Sage
Mega Sage

Hi Chris,

 

You can try the following script.

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));
        }

    }
}

 

Hopefully, this will resolve your query.

View solution in original post

13 REPLIES 13

Chris Dea1
Tera Contributor

Good question!  I added related list on Task to show ITIL users attachments from customers that were added to the RITM.  The issue I am trying to fix is when ITIL users add new attachments to give to customers, they are adding the files on Task which the customers do not see on the Service Portal when viewing their tickets.  So by copying new attachments from Task to the RITM I am hoping to resolve this ongoing issue.

In that case, I would recommend that instead of copying the attachment, transfer the attachment from the task record to the RITM record. Then it will be available for both the customer, and the itil-user.

This could be done with a simple flow. No coding required.

Simple example below:

find_real_file.png

I attempted to follow this flow without success since copying is not recommended. Can you please expand on the flow that can be used in this situation?

Please clarify, what step do you need to be expanded/explained ?

Muhammad Khan
Mega Sage
Mega Sage

Hi Chris,

 

You can try the following script.

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));
        }

    }
}

 

Hopefully, this will resolve your query.