You need to sign in to do that
Don't have an account?
Priyesh Misquith 12
trigger on case to send a email when the case status is closed.
I need a help to write a trigger
can any one help me how can i auto send a email to the Case Owner whenever the case status is updated to closed through the trigger insted of process builder.
Thanks in advance.
can any one help me how can i auto send a email to the Case Owner whenever the case status is updated to closed through the trigger insted of process builder.
Thanks in advance.
Use below code:
Regards,
Khan Anas
All Answers
I trust you are doing very well.
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement .
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks and Regards,
Khan Anas
Set<Id> ownerIDs = new Set<Id>();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for (Case c: trigger.new) {
ownerIDs.add(c.ownerid);
}
Map<Id, User> conMap = new Map<Id, User>([SELECT Id, Email FROM User WHERE Id In :ownerIDs]);
for (Case c : trigger.new) {
if (c.status == 'Closed' && Trigger.oldMap(c.Id).status <> 'Closed') {
User relatedOwner = ownerIDs.get(c.ownerid);
Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
CaseNotificationmail.setToAddresses(new List<String> { relatedOwner.Email });
CaseNotificationmail.setReplyTo('khananasrehmani@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 + ' has been closed');
mails.add(CaseNotificationmail);
}
}
Messaging.sendEmail(mails);
}
I have modified Anas trigger as per your need. Have a look and mark Anas answer as best if it works for you.
Thanks,
Hasan Shamsi
in the following code i am not able to fetch 'Owner Name = '+c.owner.name+'<p/>'+
'Owner Email= '+c.Owner.email+'<p/>'+
'Owner mobile= '+c.Owner.Phone+'<p/>'+
'AccountName= '+c.Account.name+'<p/>'+
The values are showing as null.
i want to populate case owner email, mobile, and the account name of the case in the mail.
Thanks,
priyesh
trigger SendCaseEmail on Case (before update) {
Set<Id> Usid = new Set<Id>();
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for (Case c: trigger.new) {
Usid.add(c.OwnerId);
}
Map<Id, user> conMap = new Map<Id, user>([SELECT Id,name,phone,mobilephone ,Email FROM user WHERE Id In :Usid]);
system.debug('owner' + Trigger.new);
for (Case c: Trigger.new){
if(c.Status=='Closed'){
system.debug('Mydebug'+c.Owner.name+c.Owner.email+c.Owner.email+c.owner.Phone);
User relatedCaseUser = conMap.get(c.OwnerId);
Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
CaseNotificationmail.setToAddresses(new List<String> { relatedCaseUser.Email });
CaseNotificationmail.setSubject('trigger email ');
CaseNotificationmail.setHtmlBody(
'Your case Status for Case Number: ' + c.CaseNumber +' has been closed'+
'owner Name = '+relatedCaseUser.name+'<p/>'+
'owner Email= '+relatedCaseUser.email+'<p/>'+
'owner mobile= '+relatedCaseUser.mobilephone +'<p/>'+
'ticket id= '+c.id);
mails.add(CaseNotificationmail);
}
}
Messaging.sendEmail(mails);
}
Use below code:
Regards,
Khan Anas