0

I am developing a functionality where I need to copy emails(emails+attachments) from a child case to parent case on merging.

Right now what ever I have developed I achieved copying emails from child to master case. But when an email is sent with attachments from child case then only email is getting copied to master case not the attachments.

Below is my trigger code

trigger CaseTrigger on Case (before insert, after insert, before update,after update, before delete,after delete) {

    CaseTriggerHandler csHandler = new CaseTriggerHandler(trigger.new,trigger.old,trigger.newMap,trigger.oldMap,trigger.isUpdate,trigger.isInsert);

    if (trigger.isAfter && trigger.isInsert) {

       csHandler.HandleAfterInsert();

    }
    if (trigger.isAfter && trigger.isUpdate) {

       csHandler.HandleAfterUpdate();

    }

}

And Below is my trigger handler class(I have kept only useful method related to my question,rest I have omitted for better view and usefulness to my question)

    public class CaseTriggerHandler { 

    public static boolean run = true;
    public static boolean runOnce(){
        if(run){
            run=false;
            return true;
        }else{
            return run;
        }
    }

    List<Case> listOfCase;
    List<Case> newCase;
    List<Case> oldCase;
    Map<Id,Case> newCaseMap;
    Map<Id,Case> oldCaseMap;
    boolean isUpdate;
    boolean isDelete;    

    public CaseTriggerHandler (List<Case> newCase, List<Case> oldCase, 
                               Map<Id,Case> newCaseMap, Map<Id,Case> oldCaseMap, boolean isUpdate, boolean isDelete){
                                   this.newCase = newCase;
                                   this.oldCase = oldCase;
                                   this.newCaseMap = newCaseMap;
                                   this.oldCaseMap = oldCaseMap;
                                   this.isUpdate = isUpdate;
                                   this.isDelete = isDelete;
                               }

    public void HandleAfterUpdate(){       
        if(run){     

            handleemailattachment();

        }
    }

    public void HandleAfterInsert(){

        handleemailattachment();

    } 

    public void handleemailattachment(){
     Map<Id, Id> parentCaseIdMap = new Map<Id, Id>();
     for(Case c : newCase){
         if(c.ParentId!=null)parentCaseIdMap.put(c.Id, c.ParentId);
     }
     // Fetch all the attachments related to the child case
     List<EmailMessage> EmailMessageList= new List<EmailMessage>();
     List<EmailMessage> attachmentToBeCloned = new List<EmailMessage>();
     EmailMessageList = [Select Id,Subject,ParentId,textBody From EmailMessage Where ParentId in:parentCaseIdMap.keySet()];
     for(EmailMessage att : EmailMessageList){
         EmailMessage a = att.clone();
         if(parentCaseIdMap.containsKey(att.ParentId)){
            a.ParentId = parentCaseIdMap.get(att.ParentId);
            attachmentToBeCloned.add(a); 
         } 
    }
    if(attachmentToBeCloned!=null && attachmentToBeCloned.size()>0){
        insert attachmentToBeCloned;
    } 
}
}

Illustration of my requirements:

Suppose I created one Email- to-Case which triggers a creation of cases in SF with 2 emails. Now I linked this case to other Case. Upon merging both of the cases emails are getting copied to master/parent case but if I attach any attachment while creating Email to Case then upon merging only emails are getting copied not attachments .

Hi @Pranay, As per your comments I have written below trigger and trigger handler but still it seems not working

trigger AttachmentTrigger on Attachment (after insert) {
if (trigger.isInsert && trigger.isAfter) {

    AttachmentTriggerHandler.handleattachements(trigger.new);
}

}

Below is my trigger handler

public class AttachmentTriggerHandler {

public static void handleattachements(List<Attachment> attList){
    Set<Id> Parents = new Set<Id>();
List<Attachment> newFiles = new List<Attachment>();

for (attachment a : attList) {
    Parents.add(a.parentId);
}

for (EmailMessage e : [SELECT Id, ParentId FROM EmailMessage WHERE Id in :Parents]) { //loop through unique parents
    for (Attachment a : attList) {
        if (e.Id == a.ParentId) {
            Attachment newFile = a.clone();
            newFile.ParentId = e.ParentId;
            newFiles.add(newFile);
        }
    }
}

Insert newFiles;


}

}

Kindly have a look where I am missing anything or I am doing something wrong.

Many thanks in advance

1 Answer 1

0

You have to write trigger on Attachments, to change the parentID from child case to that of Master Case.

On Attachment there is a parentID field which will be set to email record's id.

So you have to find out if that email is attached to master case or child case. If its child case then in before Insert trigger of attachment change the parentID to that of master case.

1
  • I tried as per your comments and pasted my attachemnet trigger codes and trigger handler in my post above. It still seems not working. Kindly have a look and let me know If I am doing anything wrong or missing anything on same Commented Apr 10, 2018 at 7:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .