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
krishna chaitanya 35krishna chaitanya 35 

Multiple emails are recived from singleemailmessage(Recurring Emails)

Hello All,
I am fetching contacts and few email from custom lables to send an email from singlemailmessge but i am receiving several times .can any one help me to fix this issue.


public class IncidentTriggers {
    public static void SendEmailtoSiteCOntact(List < Incident__c > lnewind) {
    
    Map < ID, ID > mCaseIDtoSiteContID = new Map < ID, ID > ();
    Map < ID, Incident__c > mSiteIDtoIncident = new Map < ID, Incident__c > ();
    
 try{
 //Find all Incident__cs with Sites
    for (Incident__c c: lnewind) {
    if (c.Site__c != null) {
    system.debug('incident:' + c.ID);
    system.debug('Incident__c.Site__c:' + c.Site__c);
    mCaseIDtoSiteContID.put(c.ID, c.Site__c);
    system.debug('values in the map'+mCaseIDtoSiteContID);
    system.debug('incidnetid:' + mCaseIDtoSiteContID.get(c.id));
    //mSiteIDtoIncident.put(c.Site__c, c);
    }

 //-----------------------------------

 //Find All Site_Contacts referenced in the above incidents
 List < Site_Contact__c > lSContacts = [SELECT ID, Site__c, Role__c, Contact__c, contact__r.Email, Transfer_To_Customer_Complaint__c, Transfer_To_Supplier_Complaint__c, Transfer_To_Product_Specification__c FROM Site_Contact__c WHERE Site__c in : mCaseIDtoSiteContID.values()];

 //Incident__c c = mSiteIDtoIncident.get(sc.Site__c);


    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    string address1=label.Incident_Manager1;
    string address2=label.Incident_Manager2;
    string address3=label.Incident_Manager3;
    string addresses=address1+':'+address2+':'+address3;
    for (Site_Contact__c sc: lSContacts) {
    if (sc.contact__r.Email != null || sc.contact__r.Email != 'null') {         
    addresses = addresses + ':' + sc.contact__r.Email;
    system.debug('intial Email Address Information ' + addresses);
    List < String > emailAddresses = new List < String > ();
    emailaddresses = addresses.split(':', 0);
    system.debug('final list of Email Address Information ' + emailaddresses);
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    EmailTemplate et = [SELECT Id ,name FROM EmailTemplate WHERE Name = 'Test Template for incident'];
    email.ToAddresses = (emailaddresses);
    email.setTemplateId(et.Id);
    email.setSaveAsActivity(false);
    email.setTargetObjectId(sc.contact__c);
    email.setOrgWideEmailAddressId('0D2200000004EQx');
    email.setwhatid(c.id);
    mails.add(email);
    }
    }
    
    }
    Messaging.sendEmail(mails); 
}
    
catch(Exception ec)
       {    
          system.debug('error is' + ec);
        }

    
   }
}

Regards,
Krishna
Wilfredo Morillo 20Wilfredo Morillo 20
You can have the same contact in multiple incidents and that's why you are getting multiples emails. Also I strongly advice you to check the best practices. 

https://developer.salesforce.com/page/Apex_Code_Best_Practices

(Marked as the best if it helped you.)

Thanks, 
krishna chaitanya 35krishna chaitanya 35
Hello wilfredo,
Contacts in custom lables and contacts are from query both are unique contacts.I need the email to be fired only once.
Regards,
Krishna