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