1
trigger emailAttachmentVisibleOnParent on Attachment (before insert) {
    for( Attachment a : trigger.new ) {  
        // Check the parent ID - if it's 02s, this is for an email message   
       if( a.parentid == null )     
            continue;       
        String s = string.valueof( a.parentid );     
        if( s.substring( 0, 3 ) == '02s' ) {  
            a.parentid = [select parentID from EmailMessage where id = :a.parentid].parentID;   
        }
    }
}

This Trigger is used for moving the EmailMessage attachments to Case Attachments. Here i want to store the attachments in both like EMailMessage Attachments and Case Attachments.

Help me on the above requirement. Thanks in Advance.

2
  • What part of it isn't working?
    – Nick C
    Commented Aug 31, 2015 at 7:03
  • Hello Nick, This trigger is working fine for moving the EmailMessage Attachments to Case Attachments, but i want to display the attachments in both the objects on EmailMessage and Case Attachments Commented Aug 31, 2015 at 7:07

2 Answers 2

1

You can't attach an attachment on multiple records. If you want to attach same attachment on both EmailMessage and Case records then you need to duplicate the attachment.

UPDATE: As crop1645 explained this process needs an after insert. I've moved trigger from Attachment to EmailMessage because we are inserting attachments in the trigger. Though I'm not quite sure code is 100% correct, I did not test this.

trigger copyEmailAttachmentToCase on EmailMessage(after insert) {

  Set<Id> emailMessageIds = new Set<Id>();
  List<EmailMessage> emailMessages = Trigger.new;
  Map<Id, EmailMessage> emailMessagesMap = new Map<Id, EmailMessage>();
  for(Integer i = 0; i < emailMessages.size(); i++){
    emailMessageIds.add(emailMessages[i].Id);
    emailMessagesMap.put(emailMessages[i].Id, emailMessages[i]);
  }
  List<Attachment> attachments = [Select Id, Name, ParentId, Body, IsPrivate, OwnerId FROM Attachment WHERE ParentId IN :emailMessageIds];

  if(attachments.size() > 0){
    List<Attachment> newAttachments = new List<Attachment>();
    for(Integer i = 0; i < attachments.size(); i++){
        if(emailMessageMap.get(attachments[i].ParentId) != null){
            Attachment a = new Attachment();
            a.ParentId = emailMessageMap.get(attachments[i].ParentId).ParentId;
            //clone rest of the attachment
            newAttachments.add(a);
        }
    }

    insert newAttachments;
  }
}
1
  • 1
    i.e. OP will need to use an after insert trigger, properly bulkified - because of heap limits, this most likely will need to be done with async processes if trigger set size > 1.
    – cropredy
    Commented Aug 31, 2015 at 19:38
0

Hi @Tugce and @cropredy,

I am also working on close to same requirements where I have to clone emails from child case to parent case.Right now I am able to copy email from child to parent case when case is created by Email to Case. But when Email is sent with attachements using Email to Case then also only emails are getting copied not the attachements. I guess you have already achieved copying email attachements from child to parent. My requirement is not copying email attachement as an attachements in parent case but it should be as email attachement only.

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

} }

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

Any help will be greatly appreciated.

Many thanks

You must log in to answer this question.

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