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

Rohit Sarkar1
Mega Guru

You can sort the list by adding this on a GlideRecord on sys_attachment table,

with addQuery('table_sys_id',ritmRec.sys_id.toString());

then do a deleteMultiple() to remove all and then copy

Yousaf
Giga Sage

Hi Chris,

This post has a upgraded script did you try ?


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

}

}


***Mark Correct or Helpful if it helps.***

Thank you Yousaf!  I tried the script, it almost meets my needs..  With this script, if new attachments are added to Task they are added to RITM, after the existing attachments on the RITM are deleted.  However, I want to retain any of the original attachments the customer added when the RITM was created.  With this script if the customer had added an attachment (Attach1_from_Customer.pdf) when initially creating the RITM, the attachment will get deleted when the ITIL user adds a new attachment (Attach2_from_ITIL.pdf) on the Task.

OlaN
Giga Sage
Giga Sage

Hi,

What's the purpose of copying the attachments?

You could easily add a related list which shows what attachments are available on the RITM from the task and vice versa.