You need to sign in to do that
Don't have an account?
jaishri
i have a code in trigger when case is closed,create and delete send a email to case contact when case is delete how to send a email to case contact can anyone help me to provide solution
trigger CaseEmail on Case (after insert, after update,before delete) {
If((Trigger.isUpdate || Trigger.isinsert) && Trigger.isafter){
Set<Id> conIds = new Set<Id>();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for (Case c: trigger.new) {
conIds.add(c.ContactId);
}
Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
for (Case c : trigger.new) {
if (c.status == 'Closed') {
Contact relatedCaseContact = conMap.get(c.ContactId);
Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
CaseNotificationmail.setToAddresses(new List<String> { relatedCaseContact.Email });
CaseNotificationmail.setReplyTo('jayati.shukla.sbg@gmail.com');
CaseNotificationmail.setSenderDisplayName('Salesforce');
CaseNotificationmail.setSubject(' Case Status updation ' + 'Changed to ' + c.status + ' Case Number:' + c.CaseNumber);
CaseNotificationmail.setPlainTextBody(' Your case Status for Case Number: ' + c.CaseNumber + ' Related Case Contact:' +c.ContactId +' has been closed ');
mails.add(CaseNotificationmail);
}
if(Trigger.isinsert && Trigger.isafter){
Contact relatedCaseContact = conMap.get(c.ContactId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new List<String> { relatedCaseContact.Email });
mail.setSubject('New Case Create: '+ c.CaseNumber);
String body = 'Case is created. Thank you for contacting us';
mail.setHtmlBody(body);
mails.add(mail);
}
}
Messaging.sendEmail(mails);
}
if(Trigger.isDelete && Trigger.isBefore){
Set<Id> conIds = new Set<Id>();
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Map<Id, Contact> conMp = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
for (Case cs : Trigger.old) {
Contact relatedCaseContact = conMp.get(cs.ContactId);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String> { relatedCaseContact.Email });
email.setSubject('Case Deleted');
email.setPlainTextBody('This message is to alert you that the Case number' + cs.CaseNumber + ' has been deleted. Thank you for contacting us.');
emails.add(email);
}
Messaging.sendEmail(emails);
}
}
If((Trigger.isUpdate || Trigger.isinsert) && Trigger.isafter){
Set<Id> conIds = new Set<Id>();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for (Case c: trigger.new) {
conIds.add(c.ContactId);
}
Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
for (Case c : trigger.new) {
if (c.status == 'Closed') {
Contact relatedCaseContact = conMap.get(c.ContactId);
Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
CaseNotificationmail.setToAddresses(new List<String> { relatedCaseContact.Email });
CaseNotificationmail.setReplyTo('jayati.shukla.sbg@gmail.com');
CaseNotificationmail.setSenderDisplayName('Salesforce');
CaseNotificationmail.setSubject(' Case Status updation ' + 'Changed to ' + c.status + ' Case Number:' + c.CaseNumber);
CaseNotificationmail.setPlainTextBody(' Your case Status for Case Number: ' + c.CaseNumber + ' Related Case Contact:' +c.ContactId +' has been closed ');
mails.add(CaseNotificationmail);
}
if(Trigger.isinsert && Trigger.isafter){
Contact relatedCaseContact = conMap.get(c.ContactId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new List<String> { relatedCaseContact.Email });
mail.setSubject('New Case Create: '+ c.CaseNumber);
String body = 'Case is created. Thank you for contacting us';
mail.setHtmlBody(body);
mails.add(mail);
}
}
Messaging.sendEmail(mails);
}
if(Trigger.isDelete && Trigger.isBefore){
Set<Id> conIds = new Set<Id>();
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Map<Id, Contact> conMp = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
for (Case cs : Trigger.old) {
Contact relatedCaseContact = conMp.get(cs.ContactId);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String> { relatedCaseContact.Email });
email.setSubject('Case Deleted');
email.setPlainTextBody('This message is to alert you that the Case number' + cs.CaseNumber + ' has been deleted. Thank you for contacting us.');
emails.add(email);
}
Messaging.sendEmail(emails);
}
}
Can yoy check the below article . It explains why this issue usually occurs.
https://help.salesforce.com/s/articleView?id=000353740&type=1 (https://help.salesforce.com/s/articleView?id=000353740&type=1)
Thanks,
All Answers
Can you confirm what is the issue with the above code. Is it not working or what excatly are you having issue with above.
Thanks,
I have made small change in the code. Added the highlited part because of which you are facing error.
If this solution helps, Please mark it as best answer.
Thanks,
could you explain this code line by line it is more helpful for me please
I have commented the instructions for almost all the required lines.
Please mark this solution as best answer, It it helped you.
Thanks,
Create a trigger and call the apex class methods in it ................i don't know how to do
Sure I can provide it. Is it possible to add as other question as this requires some efforts to change into handler.
Thanks,
this is code like if when a case is created ,closed and delete we are using insert ,update delete i want to put the logic in handler and call method in trigger i also provide a link https://bestirtech.com/blog/2020/09/salesforce-apex-trigger-handler/ here is sample of account trigger handler class and account trigger . Help me...
trigger CaseEmail on Case (after insert, after update) {
If((Trigger.isUpdate || Trigger.isinsert) && Trigger.isafter){
Set<Id> conIds = new Set<Id>();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for (Case c: trigger.new) {
conIds.add(c.ContactId);
}
Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
for (Case c : trigger.new) {
if (c.status == 'Closed') {
Contact relatedCaseContact = conMap.get(c.ContactId);
Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
CaseNotificationmail.setToAddresses(new List<String> { relatedCaseContact.Email });
CaseNotificationmail.setReplyTo('jayati.shukla.sbg@gmail.com');
CaseNotificationmail.setSenderDisplayName('Salesforce');
CaseNotificationmail.setSubject(' Case Status updation ' + 'Changed to ' + c.status + ' Case Number:' + c.CaseNumber);
CaseNotificationmail.setPlainTextBody(' Your case Status for Case Number: ' + c.CaseNumber + ' Related Case Contact:' +c.ContactId +' has been closed ');
mails.add(CaseNotificationmail);
}
if(Trigger.isinsert && Trigger.isafter){
Contact relatedCaseContact = conMap.get(c.ContactId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new List<String> { relatedCaseContact.Email });
mail.setSubject('New Case Create: '+ c.CaseNumber);
mail.setPlainTextBody('Case is created. Thank you for contacting us');
mails.add(mail);
}
}
Messaging.sendEmail(mails);
}
if(Trigger.isDelete && Trigger.isBefore){
Set<Id> conIds = new Set<Id>();
for (Case c: trigger.old) {
conIds.add(c.ContactId);
}
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Map<Id, Contact> conMp = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
for (Case cs : Trigger.old) {
Contact relatedCaseContact = conMp.get(cs.ContactId);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String> { relatedCaseContact.Email });
email.setSubject('Case Deleted');
email.setPlainTextBody('This message is to alert you that the Case number' + cs.CaseNumber + ' has been deleted. Thank you for contacting us.');
emails.add(email);
}
Messaging.sendEmail(emails);
}
}
Please try the below trigger and handler.
Trigger:
Handler:
Thanks,
Can we remove this line from handler class if(Trigger.isinsert && Trigger.isafter)
No you cannot remove that line because we are using the same method for update and insert. For the system to know which alert should it send we used in the handler.
Thanks,
Trigger
trigger CaseEmail on Case (after insert, after update,before delete) {
If((Trigger.isUpdate || Trigger.isinsert) && Trigger.isafter){
CaseHelper.AfterInsert(Trigger.new);
}
if(Trigger.isDelete && Trigger.isBefore){
CaseHelper.BeforeDelete(Trigger.old);
}
}
Helper class
public class CaseHelper {
public static void AfterInsert(List<Case> caselist){
Set<Id> conIds = new Set<Id>();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for (Case c: caselist) {
conIds.add(c.ContactId);
}
Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
for (Case c : caselist) {
Contact CaseContact = conMap.get(c.ContactId);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new List<String> { CaseContact.Email });
mail.setSubject('New Case Create '+ c.CaseNumber);
mail.setPlainTextBody('Case is created Thank you for contacting us');
mails.add(mail);
}
Messaging.sendEmail(mails);
}
public static void BeforeDelete(List<Case> caselist){
Set<Id> conIds = new Set<Id>();
for (Case c: caselist) {
conIds.add(c.ContactId);
}
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Map<Id, Contact> conMp = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);
for (Case cs : caselist) {
Contact CaseContact = conMp.get(cs.ContactId);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String> { CaseContact.Email });
email.setSubject('Case Deleted');
email.setPlainTextBody('This message is to alert you that the Case number' + cs.CaseNumber + ' has been deleted. Thank you for contacting us.');
emails.add(email);
}
Messaging.sendEmail(emails);
}
}
Can yoy check the below article . It explains why this issue usually occurs.
https://help.salesforce.com/s/articleView?id=000353740&type=1 (https://help.salesforce.com/s/articleView?id=000353740&type=1)
Thanks,