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

Anusha Reddy1
Giga Expert

To copy the attachments from RITM to task, please create an after insert BR as below. It will copy attachments from RITM to tasks.

find_real_file.png

Sriniwas
Mega Contributor

Hi,

Have implemented the below code for this.

This BR is after insert and update on u_custom_tasks (this my custom task table)

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

var gr = new GlideRecord("sys_attachment");
gr.addQuery("296c4dbcdb0d109078a1c14078a1c14",current.u_custom_request);
gr.query();
while (gr.next()) {
GlideSysAttachment.copy('u_custom_requests', current.u_custom_request, 'u_custom_tasks', current.sys_id);
}

})(current, previous);

 

u_custom_requests - is my custom request table.

u_custom_request - is my custom field on task to refer the request number.

 

Scenario 1) user added 3 attachments to the request and submitted the request.

Now the task got generated and it had all the 3 attachments automatically since we have BR. - No issues.

Scenario 2) Now user added one more attachment(attachment 4) to the Request.

Now if we see the task, the latest attachment (attachment 4)s not available automatically.

Once he do some updates to the task, then only it is populating. Since the BR is insert or update on Task table.

Now my requirement is, with out updating the task in Scenario 2 how to get the attachment 4 to the task level.

Any help on this please.

Thanks,

Sriniwas

 

 

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

Hi Anusha,

Thanks for the reply.

have created 2 BRs now.

1) One BR on task table and after insert.

It will help to copy the attachments from request to task while task creation.

2)Another BR on sys_attachment table and table name is Request table here and it is after insert and update.

It will help to populate attachments newly added on the request to task with out doing any  update on the task.

 

Thanks,

Sriniwas

Glad it worked.