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
mitsvikmitsvik 

multiple email get sent for case inserted trigger in apex code-urgent prod issue

public class sendEmailIntrestedPartyCaseCreateUpdate {
    
    
    public static void sendEmailtoIntrestedPartyOnCase(List<Case> listOfCases){
        
        Set<Id> AccountIds = new Set<Id>(); 
        Map<Id,Set<String>> mapOfInterestedP1toAccounttoUserEmail = new Map<id,Set<String>>();
        Map<Id,Set<String>> mapOfInterestedAlltoAccounttoUserEmail = new Map<id,Set<String>>();
        for(Case caseRecord: listOfCases){
            if(caseRecord.AccountId!=null){
                AccountIds.add(caseRecord.AccountId);
                
            }
        }
        for(Interested_Party__c ipRecords: [SELECT Id, Name, Account__c, P1_Cases__c, All_Cases__c, UserEmail__c
                                            FROM Interested_Party__c where Account__c IN: AccountIds ]){
                                                
                                                
                                                //p1 map population
                                                if(ipRecords.P1_Cases__c){
                                                    if(!mapOfInterestedP1toAccounttoUserEmail.containsKey(ipRecords.Account__c)){
                                                        
                                                        Set<String> lstUserEmails= new Set<String>();
                                                        lstUserEmails.add(ipRecords.UserEmail__c);
                                                        
                                                        mapOfInterestedP1toAccounttoUserEmail.put(ipRecords.Account__c,lstUserEmails);
                                                        
                                                        
                                                    } else if
                                                        (mapOfInterestedP1toAccounttoUserEmail.containsKey(ipRecords.Account__c)){
                                                            Set<String> lstUserEmails= new Set<String>();
                                                            lstUserEmails.add(ipRecords.UserEmail__c);
                                                            
                                                            mapOfInterestedP1toAccounttoUserEmail.get(ipRecords.Account__c).add(ipRecords.UserEmail__c);
                                                        }
                                                }
                                                //all cases map population
                                                if(ipRecords.All_Cases__c){
                                                    if(!mapOfInterestedAlltoAccounttoUserEmail.containsKey(ipRecords.Account__c)){
                                                        
                                                        Set<String> lstUserEmails= new Set<String>();
                                                        lstUserEmails.add(ipRecords.UserEmail__c);
                                                        
                                                        mapOfInterestedAlltoAccounttoUserEmail.put(ipRecords.Account__c,lstUserEmails);
                                                        
                                                    } else if
                                                        (mapOfInterestedAlltoAccounttoUserEmail.containsKey(ipRecords.Account__c)){
                                                            Set<String> lstUserEmails= new Set<String>();
                                                            lstUserEmails.add(ipRecords.UserEmail__c);
                                                            
                                                            mapOfInterestedAlltoAccounttoUserEmail.get(ipRecords.Account__c).add(ipRecords.UserEmail__c);
                                                            
                                                        }
                                                }
                                                /*Email messgging fom here*/
                                                
                                                List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
                                                
                                                for(Case varCase: listOfCases){
                                                    Set<String> useremailsSet= new Set<String>();
                                                    
                                                    if(mapOfInterestedAlltoAccounttoUserEmail.containsKey(varCase.AccountId)){
                                                        useremailsSet.addall(mapOfInterestedAlltoAccounttoUserEmail.get(varCase.AccountId));
                                                        system.debug('&&&&&&&((()()'+useremailsSet);
                                                    }
                                                    
                                                    else if (varCase.Priority=='1-Critical'&& mapOfInterestedP1toAccounttoUserEmail.containskey(varCase.AccountId)){
                                                        
                                                        useremailsSet=mapOfInterestedP1toAccounttoUserEmail.get(varCase.AccountId);
                                                        
                                                    }
                                                    List<String> userEmailList = new List<String>();
                                                    userEmailList.addaLL(useremailsSet);
                                                    
                                                    IF(userEmailList.SIZE()>0){
                                                        Messaging.SingleEmailMessage sendEmailmsg = new Messaging.SingleEmailMessage();
                                                        sendEmailmsg.toaddresses= userEmailList;
                                                        string body='';
                                                        body += '<html></br> </br>' + 'A case has been created,for more details, '+  'please review the Case HERE: '
                                                            + '<a href="'+URL.getSalesforceBaseUrl().toExternalForm()
                                                            +'/'+ varcase.id +'">' + varcase.CaseNumber + '</a>';
                                                        body +=  '</body></html>';
                                                        
                                                        sendEmailmsg.setHtmlBody(body);
                                                        
                                                        
                                                        
                                                        String Subject = 'A new Case has been created for the account '+' '+ varcase.Account_Name_Display__c;
                                                        sendEmailmsg.setSubject(Subject);
                                                        lstMsgs.add(sendEmailmsg);
                                                    }
                                                }
                                                
                                                Messaging.sendEmail(lstMsgs);
                                                
                                            }
    }
}
I have an custom object intrested party to Account where i can add users who should be notified when a case for that account in inserted.However if i am the case owner and i create a couple intrested parties....each time a case is created...the case owner based on the above code gets that many number of emails...I am not sure what i neeed to fix...but any help i s appreciated.