+ Start a Discussion
Harjeet Singh 13Harjeet Singh 13 

Copy Email to Case attachmnets from child case to Parent Case

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


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




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(){
            return true;
            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(){       



    public void HandleAfterInsert(){



    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();
            a.ParentId = parentCaseIdMap.get(att.ParentId);
    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 .

Kindly help.

Many thanks in advance

Thanks & Regards,