How to add attachments from request to task automatically

Sriniwas
Mega Contributor

Hi,

We have a requirement that the attachments added on to the request should get automatically added on to the tasks related to that request.

1)While creating the request users will add attachments to the request  and after creating the request also users will add and the tasks will generate later based on the workflow trigger conditions. whenever, the tasks generated for that request all the attachments present on the request should get added to the task.

2)In the flow there are so many tasks will generate one after one, and the attachments should added to all the tasks even the closed tasks in the flow.

Can any one please help me on this.

 

1 ACCEPTED SOLUTION

Please try below code, tested it works as expected.

find_real_file.png

script:

 

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

// Add your code here
var copyFrom = new GlideRecord(current.table_name);
copyFrom.get(current.table_sys_id);
var id = current.table_sys_id;

var copyTo = new GlideRecord('sc_task');
copyTo.addQuery('request_item',id);
copyTo.query();
if(copyTo.next()){
var task = copyTo.getUniqueValue();
}
copyAttachments(copyTo.getTableName(), task);

deleteDuplicateAttachments(task);


/************************** functions *****************************/

function copyAttachments(table, sys_id){

var attachmentUtil = new GlideSysAttachment();

attachmentUtil.copy(current.table_name, current.table_sys_id, table, sys_id);

}

function deleteDuplicateAttachments(sys_id){

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', sys_id);
gr.orderBy('hash');
gr.orderBy('file_name');
gr.orderByDesc('sys_created_on');
gr.query();

var lastHash = 'not_a_match';

var lastFileName = 'not_a_match';

while(gr.next()){

var isDuplicate = false;

if ((lastHash == gr.hash) && (lastFileName == gr.file_name)){
isDuplicate = true;
}

if(isDuplicate)
gr.deleteRecord();

lastHash = gr.hash.getValue();
lastFileName = gr.file_name.getValue();
}
}

})(current, previous);

View solution in original post

10 REPLIES 10

Thanks for sharing this. My requirement is lso similar, this one works when we need to add the attachment to the task, What if, we have an approval in the same flow and need that attachment in the approval email also?