You need to sign in to do that
Don't have an account?

Trigger that send email notification when the case is updated
Hi,
I am trying to write the Trigger code when the status of the case is changed and any other changes in fields with conditional loops for changing the Mail subject.
The issue was on trigger that send the same mail subject when i changed the status or changed the iother fields in the case.
The following is my code,
trigger caseUpdationMailNotification on Case ( after update) { contact relatedCaseContact; CaseComment Cscmnt; for(case Cases :trigger.new){ relatedCaseContact = [SELECT Email FROM Contact WHERE Id = :Cases.ContactId]; Messaging.reserveSingleEmailCapacity(1); //Fetch the related case contact. Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage(); CaseNotificationmail.setToAddresses(new List<String> { relatedCaseContact.Email }); CaseNotificationmail.setReplyTo('ambigaraman@gmail.com'); CaseNotificationmail.setSenderDisplayName('Salesforce Support'); If(Cases.Status =='Working'){ CaseNotificationmail.setSubject(' Case Status updation : ' +'Changed to working. '+'Case Number:' + Cases.CaseNumber); CaseNotificationmail.setPlainTextBody('Your case Status: ' + Cases.CaseNumber +'To view your case <a href=https://na1.salesforce.com/'+Cases.Id);} If(Cases.Status =='Escalated'){ CaseNotificationmail.setSubject(' Case Status updation : ' +'Changed to Escalated. '+ 'Case Number:' +Cases.CaseNumber); CaseNotificationmail.setPlainTextBody('Your case Status: ' + Cases.CaseNumber +' has been updated.'+'To view your case <a href=https://na1.salesforce.com/'+Cases.Id); } If(Cases.Status =='closed'){ CaseNotificationmail.setSubject(' Case Status updation : ' +'Changed to closed. '+ 'Case Number:' +Cases.CaseNumber); CaseNotificationmail.setPlainTextBody('Your case Status:' + Cases.CaseNumber +' has been updated.'+'To view your case <a href=https://na1.salesforce.com/'+Cases.Id); } else{ CaseNotificationmail.setSubject(' Case updation : ' + 'Case Number:'+ Cases.CaseNumber); CaseNotificationmail.setPlainTextBody('Your case : ' + ' has been updated.'+'To view your case <a href=https://na1.salesforce.com/'+Cases.Id); } Messaging.sendEmail(new Messaging.SingleEmailMessage[] { CaseNotificationmail }); } }
If i use 'elseif', It again send the email with subject for status changed,
Can anyone help me to write the trigger that send the mail that send when we change the status only. or there is only changes in other fields(not in status) of the case?
Thanks & Regards.,
Ambiga
You can try above code, see if it helps!!
All Answers
You can try above code, see if it helps!!
Hi,
It works, Thanks a lot Sonali Bhardwaj!
Regards.,
Ambiga
i am getting error like Above plese do that one
trigger UpdateCasetosendEmail on Case (after update) {
for(Case c:trigger.new){
sendEmail();
}
public void sendEmail(){
Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
string [] toAdd= new string[]{'xxx@gmail.com', 'xxx@gmail.com' };
mail.setToAddresses(toAdd);
mail.setSubject('update case');
mail.setPlainTextbody('any field updated of the case ');
Messaging.Sendemail(new Messaging.SingleEmailMessage[] {mail});
}
}