function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Harjeet Singh 13Harjeet Singh 13 

How to copy attachments sent with an email from child case to master 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) {

       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 .

Kindly help.

Many thanks in advance

Thanks & Regards,
Harjeet
NagendraNagendra (Salesforce Developers) 
Hi Harjeet,

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.

Thanks,
Nagendra
Harjeet Singh 13Harjeet Singh 13
Thanks Nagendra for your response.

I wrote below code as per your suggestions but seems its still not working. Can you have a look into the same and let me know what I am doing wrong on same
 
trigger AttachmentTrigger on Attachment (after insert) {
if (trigger.isInsert && trigger.isAfter) {

    AttachmentTriggerHandler.handleattachements(trigger.new);
}
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;


}

Needless to say I am waiting again for your kind response.

Many thanks in advance

Thanks & Regards,
Harjeet