You need to sign in to do that
Don't have an account?
utz1
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
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
Instead of send mail, you can create EmailMessage Record of Child case.
Thanks,
If it helps you, please mark is as best answer, so it will be helpful for other developers.
All Answers
You can achieve using Apex Trigger(1st suggestion) and also can try with process builder(2nd suggestion).
Thanks,
You need to use Trigger only.
Thanks...
If it helps you, please mark is as best answer, so it will be helpful for other developers.
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);
}
}
}
}
Instead of send mail, you can create EmailMessage Record of Child case.
Thanks,
If it helps you, please mark is as best answer, so it will be helpful for other developers.
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);
}
}
}
}
}