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

Naveen63
Tera Contributor

Hello,

Please try below the business rule to achieve your requirement.

find_real_file.png

 

find_real_file.png

 

Hope this helps!

If I have answered your question, please mark my response as correct and helpful.

thank you

Joshwa Antony S
Mega Guru

Hi,

You could use GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id'); in TASK activity if you are creating task via workflow

Copy attachments from record to record

GlideSysAttachment.copy('sc_req_item',current.sys_id,'sc_task',task.sys_id);

OR use flow designer instead of Business Rule

Copy Attachment action

find_real_file.png

 

find_real_file.png

Regards,

JAS

Sriniwas
Mega Contributor

Hi, the issue here is whenever the attachment is added to the request then automatically the attachments should appear in the tasks.

in my case, it is working only if some update happened on the task then only the new attachments populating on the task.

Thanks,

Sriniwas