+ Start a Discussion
AmbigaRamAmbigaRam 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
Sonali BhardwajSonali Bhardwaj
trigger caseUpdationMailNotification on Case (after update) {
    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) {
        Contact relatedCaseContact = conMap.get(c.ContactId);
        Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();  
        CaseNotificationmail.setToAddresses(new List<String> { relatedCaseContact.Email });
        CaseNotificationmail.setReplyTo('ambigaraman@gmail.com');
        CaseNotificationmail.setSenderDisplayName('Salesforce Support');            
        
        String oldStatus = trigger.oldMap.get(c.id).status;
        if (c.status != oldStatus) {
          CaseNotificationmail.setSubject(' Case Status updation : ' + 'Changed to ' + c.status + '. Case Number:' + c.CaseNumber);
          CaseNotificationmail.setPlainTextBody('Your case Status: ' + c.CaseNumber + 'To view your case <a href=https://na1.salesforce.com/' + c.Id); 
        }
        else {  
           CaseNotificationmail.setSubject(' Case  updation : ' + 'Case Number:' + c.CaseNumber);
           CaseNotificationmail.setPlainTextBody('Your case : ' + ' has been updated.' + 'To view your case <a href=https://na1.salesforce.com/' + c.Id);
       }
       mails.add(CaseNotificationmail); 
    }
    Messaging.sendEmail(mails);
}

 You can try above code, see if it helps!!

All Answers

Sonali BhardwajSonali Bhardwaj
trigger caseUpdationMailNotification on Case (after update) {
    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) {
        Contact relatedCaseContact = conMap.get(c.ContactId);
        Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();  
        CaseNotificationmail.setToAddresses(new List<String> { relatedCaseContact.Email });
        CaseNotificationmail.setReplyTo('ambigaraman@gmail.com');
        CaseNotificationmail.setSenderDisplayName('Salesforce Support');            
        
        String oldStatus = trigger.oldMap.get(c.id).status;
        if (c.status != oldStatus) {
          CaseNotificationmail.setSubject(' Case Status updation : ' + 'Changed to ' + c.status + '. Case Number:' + c.CaseNumber);
          CaseNotificationmail.setPlainTextBody('Your case Status: ' + c.CaseNumber + 'To view your case <a href=https://na1.salesforce.com/' + c.Id); 
        }
        else {  
           CaseNotificationmail.setSubject(' Case  updation : ' + 'Case Number:' + c.CaseNumber);
           CaseNotificationmail.setPlainTextBody('Your case : ' + ' has been updated.' + 'To view your case <a href=https://na1.salesforce.com/' + c.Id);
       }
       mails.add(CaseNotificationmail); 
    }
    Messaging.sendEmail(mails);
}

 You can try above code, see if it helps!!

This was selected as the best answer
AmbigaRamAmbigaRam

Hi,

 

It works, Thanks a lot Sonali Bhardwaj!

 

Regards.,

Ambiga

suresh143suresh143
Error Error: Compile Error: line 19:165 no viable alternative at character '"' at line 19 column 165

i am getting error like Above plese do that one

John naJohn na
Suresh, try re-typing your double quotes.  See this post:  http://stackoverflow.com/questions/11001916/no-viable-alternative-at-character
Nagma KhanNagma Khan
used this trigger and when are your case update then send a mail define email .
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});
    }
        

}