function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
YASH VARDHAN SINGHYASH VARDHAN SINGH 

apex class send a email when the case status is closed and in trigger will be separated

please help me out.
Best Answer chosen by YASH VARDHAN SINGH
sachinarorasfsachinarorasf
Hi Yash,
I can use the below code for sending the email. I also used it in my org.
trigger SendEmailOnCase on Case (after insert, 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) {
        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('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 hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
 

All Answers

sachinarorasfsachinarorasf
Hi Yash,
I can use the below code for sending the email. I also used it in my org.
trigger SendEmailOnCase on Case (after insert, 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) {
        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('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 hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
 
This was selected as the best answer
YASH VARDHAN SINGHYASH VARDHAN SINGH
thanks