How to copy attachments from Case to Case task

The Matrix
Tera Contributor

Hi All,

How to copy attachments from Case to Case Task, it is in custom app and both table are custom created. 

Whenever Case Task is being created from Case copy attachments from Case to Case Task. Case Task is a related list on Case form and its like Parent(Case) to Child(Case Task) relationship.

I have tried below BR Code and I need help in to add a query to get a matching record from Tasks related List

 

I have tried below before Insert Update BR on Case Task, but its not working.

    var grCase = new GlideRecord('case');
    grCase.addQuery('parent', current.sys_id); // here need help on how to add query to get a matching record from Related list
    grCase.query();
    if (grCase.next()) {
        new GlideSysAttachment.copy('case_task', current.sys_id, 'case', grCase.sys_id);
}

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

You'll probably want this to trigger only before insert so it's not copied every time the case task is updated.  You'll need to add some logs to see if the GR is returning any records, but the GlideSysAttachment.copy method arguments are 'from' and 'to' in that order, and it doesn't sound like the case_task is the parent of the case, which is what you are querying for, so if you have a field named 'parent' on the case_task table that gets populated with the case, then it would be more like:

 

 

var grCase = new GlideRecord('case');
grCase.addQuery('sys_id',  current.parent); 
grCase.query();
if (grCase.next()) {
    new GlideSysAttachment.copy('case', grCase.sys_id, 'case_task', current.sys_id);
}

 

 

Are 'case' and 'case_task' the complete table names?

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

You'll probably want this to trigger only before insert so it's not copied every time the case task is updated.  You'll need to add some logs to see if the GR is returning any records, but the GlideSysAttachment.copy method arguments are 'from' and 'to' in that order, and it doesn't sound like the case_task is the parent of the case, which is what you are querying for, so if you have a field named 'parent' on the case_task table that gets populated with the case, then it would be more like:

 

 

var grCase = new GlideRecord('case');
grCase.addQuery('sys_id',  current.parent); 
grCase.query();
if (grCase.next()) {
    new GlideSysAttachment.copy('case', grCase.sys_id, 'case_task', current.sys_id);
}

 

 

Are 'case' and 'case_task' the complete table names?

Hi @Brad Bowman  Need one suggestion,

How can I add check in BR, so that when attachments is added only that time BR will trigger and it will not copy same file again n again? 

By changing the When to Run to only before Insert, since the rule is on the case_task table it will only run when a task is created.  If there's the possibility that the case didn't have an attachment when the task was created, and you want to copy it to each task when it is added to the case, then you would need another Business Rule after insert on the attachment table.  That script would look like this:

var grCaseTask = new GlideRecord('case_task');
grCaseTask.addQuery('parent',  current.table_sys_id); 
grCaseTask.query();
while (grCaseTask.next()) {
    new GlideSysAttachment.copy('case', current.table_sys_id, 'case_task', grCaseTask.sys_id);
}

Akash4
Kilo Sage
Kilo Sage

Hello,

Here is a modified script which might work:

Try this BR as Before Insert/Update on Case Task. If this Case is of HR Case then the table is sn_hr_core_case and its child is sn_hr_core_task and if its from Customer Case it has different name. Use the right one for your scenario.


if (current.parent) {
var grCase = new GlideRecord('case'); //case can be sn_hr_core_case
if (grCase.get(current.parent)) {
new GlideSysAttachment().copy('case', grCase.sys_id, 'case_task', current.sys_id); //here as well replace 'case' and 'case_task'
}
}

 

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.