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
Hermann OuréHermann Ouré 

Code optimisation on EmailMessage

Hello,
I have created a class and a trigger to be able to stop clients from sending email message on Closed Cases.
The code works as intended. We tend to communicate with Clients using the quick action Email under the Case Feed tab. But sometimes, clients try to communicate to us about new issues using an old email reply of a closed case.
Now, with the code I wrote, they receive an email informing them that the case is closed & that we are not communicating on the case anymore.
The code works as intended... But since I am still learning on the platform, I have the feeling that my code is cumbersome.
How can I optimise and make my code more efficient?
Thanks

Apex class:
 
public class EmailManager {
@future
public static void sendEmailToCaseDeactivated(Set<Id> caseIds){

    List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
    List<Case> lstCase = [SELECT Id, Status,ContactEmail FROM Case WHERE 
                          Status = 'Closed' AND
                          ContactEmail != Null AND
                          Id IN: caseIds];
    EmailTemplate templateId = [SELECT Id FROM EmailTemplate WHERE DeveloperName =:'Smart_Community_Email_to_Case_Deactivated'];
   
    for(Case cas :  lstCase) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(UserInfo.getUserId());
        mail.setTemplateId(templateId.Id);
        String[] sendingTo = new String[]{cas.ContactEmail}; 
        mail.setToAddresses(sendingTo); 
        mail.setSaveAsActivity(false);
        allmsg.add(mail);
    }               
    Messaging.sendEmail(allmsg, false);
}

}

Apex Trigger: 
 
trigger IncomingEmailClosedCase on EmailMessage (before insert,after insert) {

if(trigger.isBefore) { 
    Set<Id> caseIds = new Set<Id>();
    for(EmailMessage em: Trigger.New) {
        if (em.Incoming)
             caseIds.add(em.parentId);   
    }
    if(!caseIds.isEmpty())
        EmailManager.sendEmailToCaseDeactivated(caseIds);
}
if(trigger.isAfter){
    Map<Id,Id> mapOfCaseVsEM = new Map<Id,Id>();
    for(EmailMessage em: Trigger.New) {
        if (em.Incoming)
            mapOfCaseVsEM.put(em.parentId,em.Id);   
    }
    set<Id> todeleteEM = new Set<Id>();
    if(mapOfCaseVsEM.isEmpty()) return;
    for(Case lstCase : [SELECT Id, Status,ContactEmail FROM Case WHERE 
                      Status = 'Closed' AND
                      ContactEmail != Null AND
                      Id IN: mapOfCaseVsEM.keyset()]){
        if(mapOfCaseVsEM.containsKey(lstCase.Id)){
            Id emId = mapOfCaseVsEM.get(lstCase.Id);
            todeleteEM.add(emId);
        } 
    } 
    if(!todeleteEM.isEmpty())   delete [ select Id from EmailMessage where id in :todeleteEM];
}
}

 
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/325168/code-optimisation-on-emailmessage

Your query is already here.


Let me know if that helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.