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
utz1utz1 

Mail send to child cases when mail sent to parent cases

Hello,

I want to develop functionality where mail should be send to related cases when mail sent to parent case.
How can we achive this?

Thanks & Regards,
Utkarsha
Best Answer chosen by utz1
Gokula KrishnanGokula Krishnan
Hi Utz,

Instead of send mail, you can create EmailMessage Record of Child case.
 
trigger sendemailtochildcases on Case (after update) {
   
Set<Id> contactIds = new Set<Id>();
 
Case c = trigger.new[0];
    
Set<Id> ChildCaseId = new Set<Id>();

For(Case c : [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail
                FROM Case where ParentId=: c.Id]){
	ChildCaseId.add(c.id);
}				
				
List<EmailMessage> EmailInsert = new List<EmailMessage>();

For(EmailMessage e : [SELECT ActivityId, BccAddress, CcAddress, CreatedById, CreatedDate, FromAddress, FromName, HasAttachment, Headers, HtmlBody, Id, Incoming, IsClientManaged, IsDeleted, IsExternallyVisible, LastModifiedById, LastModifiedDate, MessageDate, MessageIdentifier, ParentId, RelatedToId, ReplyToEmailMessageId, Status, Subject, SystemModstamp, TextBody, ThreadIdentifier, ToAddress, ValidatedFromAddress FROM EmailMessage where RelatedToId =: c.Id order by createddate desc limit 1]}){
	For(Id Cldid : ChildCaseId){
		EmailMessage i = new EmailMessage();
		i.RelatedToId = Cldid;
		i.MessageDate = e.MessageDate;
		i.Status = e.Status;
		i.ToAddress = e.ToAddress;
		i.FromAddress = e.FromAddress;
		i.FromName = e.FromName;
		i.CcAddress = e.CcAddress;
		i.BccAddress = e.BccAddress;
		i.Subject = e.Subject;
		i.TextBody =e.TextBody;
		EmailInsert.add(i);	
	}	
}


if(EmailInsert.size()>0)
	insert EmailInsert;

}

Thanks,

If it helps you, please mark is as best answer, so it will be helpful for other developers.
 

All Answers

Gokula KrishnanGokula Krishnan
Hi Urkarsha,

You can achieve using Apex Trigger(1st suggestion) and also can try with process builder(2nd suggestion).

Thanks,
utz1utz1
Is this possible through process builder to send same mail ?
Gokula KrishnanGokula Krishnan
No.
You need to use Trigger only.
Thanks...
utz1utz1
ohh. Thank you
Gokula KrishnanGokula Krishnan
Hi,

If it helps you, please mark is as best answer, so it will be helpful for other developers.
 
utz1utz1
Hello Gokula,

I have written this trigger. I am getting child cases and i am able to send mail. But i want to send mail to the child which will be sent on parent feed email messages.
I am not getting any value over here 
(select id, ccaddress, BccAddress, FromAddress, Subject, ActivityId, ToAddress, HasAttachment, HtmlBody, TextBody from EmailMessages) 


trigger sendemailtochildcases on Case (after update) {
   
 Set<Id> contactIds = new Set<Id>();
 //Set<Id> caseId2 = new Set<Id>();
    
 //   RelatedCase rc = trigger.new[0];
Case c = trigger.new[0];
system.debug('List of Case :='+c);
    
Case[] caseId = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail
                FROM Case where ParentId=: c.Id];
    //c.ParentId
system.debug('List Related Child Case '+caseId);


 Case caseId1 = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail,
                 (select id, ccaddress, BccAddress, FromAddress, Subject, ActivityId, ToAddress, HasAttachment, HtmlBody, TextBody from EmailMessages) 
           FROM Case where Id=: c.Id LIMIT 1];
system.debug('Case Ids to get email message '+caseId1);
    
        for (Case c1 : caseId)         
            //for (Case c1 : Trigger.new) 

  {  

     contactIds.add(c1.ContactId);
      system.debug('c1.ContactId:----->'+c1.ContactId);
     Map<Id,String> ContactEmail = new Map<Id, String>();
     Map<Id, String> ContactFirstname = new Map<Id, String>();
     
     for(Contact c2: [select id, email from Contact where id IN :contactIds])
    {
        system.debug('c2:'+c2);
        
      ContactEmail.put(c2.id,c2.email);
        system.debug('c2.id:---->'+c2.id);
        system.debug('c2.email:---->'+c2.email);
        
      if (c1.CaseNumber != Null) 
  {
      system.debug('c1.CaseNumber:---->'+c1.CaseNumber);

      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      String[] toAddresses = new String[] {ContactEmail.get(c2.id)}; 
      mail.setToAddresses(toAddresses);
      mail.setSubject(caseId1.Subject);
      mail.setUseSignature(false);
     // mail.getPlainTextBody(caseId1.TextBody);
    string msg = 'The follwing meeting has been scheduled to you<ol>';
      msg = msg + '<li>'+c1.subject + c1.description;
      msg = msg + '</ol>';
      mail.setHtmlBody(msg);
    
       // Send the email
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
      system.debug('Email is sent--------------------->'+mail);
        }
      }
 }
     
}

 
Gokula KrishnanGokula Krishnan
Hi Utz,

Instead of send mail, you can create EmailMessage Record of Child case.
 
trigger sendemailtochildcases on Case (after update) {
   
Set<Id> contactIds = new Set<Id>();
 
Case c = trigger.new[0];
    
Set<Id> ChildCaseId = new Set<Id>();

For(Case c : [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail
                FROM Case where ParentId=: c.Id]){
	ChildCaseId.add(c.id);
}				
				
List<EmailMessage> EmailInsert = new List<EmailMessage>();

For(EmailMessage e : [SELECT ActivityId, BccAddress, CcAddress, CreatedById, CreatedDate, FromAddress, FromName, HasAttachment, Headers, HtmlBody, Id, Incoming, IsClientManaged, IsDeleted, IsExternallyVisible, LastModifiedById, LastModifiedDate, MessageDate, MessageIdentifier, ParentId, RelatedToId, ReplyToEmailMessageId, Status, Subject, SystemModstamp, TextBody, ThreadIdentifier, ToAddress, ValidatedFromAddress FROM EmailMessage where RelatedToId =: c.Id order by createddate desc limit 1]}){
	For(Id Cldid : ChildCaseId){
		EmailMessage i = new EmailMessage();
		i.RelatedToId = Cldid;
		i.MessageDate = e.MessageDate;
		i.Status = e.Status;
		i.ToAddress = e.ToAddress;
		i.FromAddress = e.FromAddress;
		i.FromName = e.FromName;
		i.CcAddress = e.CcAddress;
		i.BccAddress = e.BccAddress;
		i.Subject = e.Subject;
		i.TextBody =e.TextBody;
		EmailInsert.add(i);	
	}	
}


if(EmailInsert.size()>0)
	insert EmailInsert;

}

Thanks,

If it helps you, please mark is as best answer, so it will be helpful for other developers.
 
This was selected as the best answer
utz1utz1
Thanks Gokula,

This code is also working. Posting  to help others.
trigger Sendemailmessage on EmailMessage (after insert) {
Set<Id> contactIds = new Set<Id>();
 //Set<Id> caseId2 = new Set<Id>();
 EmailMessage ems = trigger.new[0];
 if(ems.ParentId != null)  
 {
 //   RelatedCase rc = trigger.new[0];
Case c = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail
                FROM Case where Id=: ems.ParentId];
     
system.debug('List of Case :='+c);
    
Case[] caseId = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail
                FROM Case where ParentId=: c.Id];
    //c.ParentId
system.debug('List Related Child Case '+caseId);


 //Case caseId1 = [SELECT CaseNumber, AccountId, Id, ContactId, Subject, Description, ContactEmail, contact.name, ParentId, SuppliedEmail,
                
     EmailMessage em =    [select id, ccaddress, BccAddress, FromAddress, Subject, ActivityId, ToAddress, HasAttachment, HtmlBody, TextBody from EmailMessage
                  where ParentId=: c.Id  order by CreatedDate desc limit 1];
system.debug('Case Ids to get email message '+em);
    
        for (Case c1 : caseId)         
            //for (Case c1 : Trigger.new) 

  {  

     contactIds.add(c1.ContactId);
      system.debug('c1.ContactId:----->'+c1.ContactId);
     Map<Id,String> ContactEmail = new Map<Id, String>();
     Map<Id, String> ContactFirstname = new Map<Id, String>();
     
     for(Contact c2: [select id, email from Contact where id IN :contactIds])
    {
        system.debug('c2:'+c2);
        
      ContactEmail.put(c2.id,c2.email);
        system.debug('c2.id:---->'+c2.id);
        system.debug('c2.email:---->'+c2.email);
        
      if (em.id != Null) 
  {
      system.debug('c1.CaseNumber:---->'+c1.CaseNumber);

      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      String[] toAddresses = new String[] {ContactEmail.get(c2.id)}; 
      mail.setToAddresses(toAddresses);
      mail.setSubject(em.Subject);
      mail.setUseSignature(false);
      mail.saveAsActivity = True;

     // mail.fileattachments
     // mail.getPlainTextBody(caseId1.TextBody);
      string msg = '';
      msg = msg +em.TextBody;
      
      mail.setHtmlBody(msg);
    
      
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = : c.Id])
        {
        // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
      
      
      
       // Send the email
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
      system.debug('Email is sent--------------------->'+mail);
        }
      }
 }
 }   
}