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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Mass Emailing trigger sending email out 5 times - Please help - Urgent

Hi there,

I have a trigger which I have wrriten to send emails to all the contacts that are the children of an account which a custom object record (service__c) is also the child of. The code is designed to trigger upon Last_emails_Sent (picklist) change. The picklist is changed from a time based work flow rule with rule criteria of '(Service: Service NameEQUALSIntensive Property Coaching) AND (Service: Start of ServiceNOT EQUAL TOnull)'. It updates the picklist at (40 days before end of service support, 1 hour before end of service support, 120 days after sstart of service and 240 days after start of service). For some reason, upon reviewing my mass email, 5 accounts came to expiry today and at 11pm last night, 5 emaiils were sent out to each associated contact. 

I am not sure what has caused this problem, any help would be very much appreciated. Particular to do with whether this is due to the time based workflow or the trigger. Thank you in advance for any hel that you may give me. Below is my code:
 
rigger SendEmailtocontact on Service__c (after Update)
{
    List<String> lcontactEmails = new List<String>();
    Set<Id> sIds = new Set<Id>();
//Added
    Set<ID> accIds = new Set<ID>();
    for(Service__c qItr : Trigger.new)
{
        if(Trigger.isUpdate)
  {
            if(Trigger.oldmap.get(qItr.id).Last_Email_Sent__c!= qItr.Last_Email_Sent__c )
   {
                sIds.add(qItr.id);
    //Added
    accIds.add(qItr.Account__c);
            }
        }
  //Will Never Execute as trigger will fire only if a record is updated
        else if(Trigger.isInsert)
  {
           
                sIds.add(qItr.id);
            
        }
    }
// Modified
    //for(Account accItr : [SELECT id,(SELECT id FROM Contacts) FROM Account WHERE id IN (SELECT Account__c FROM Service__c WHERE Id IN: sIds)])
for(Account accItr : [SELECT id,(SELECT id FROM Contacts) FROM Account WHERE id IN : accIds])
{
        for(Contact con : accItr.contacts)
  {
            if(!String.isBlank(con.id))
   {
                lcontactEmails.add(con.id);
            }
        }
       // No need to put this condition here.
  /*if(!lcontactEmails.isEmpty())
  {
   EmailHandler.sendEmail(lcontactEmails);
  }*/   
    }
//Put this here
if(!lcontactEmails.isEmpty())
{
for(Service__c serv : Trigger.new)
{       
 if(Serv.Accept_Mass_Emails__c  == True){

        //Email handler Updated
        if(Serv.Last_Email_Sent__c == 'IPC Support Expiry')
        {
        EmailHandler.sendSupportExpiryIPCEmail(lcontactEmails);
        }
        
        //Email handler Updated
        if(Serv.Last_Email_Sent__c == '120 days after IPC Support Start')
        {
        EmailHandler.send120AfterStartIPCEmail(lcontactEmails);
        }
        
        //Email handler Updated
        if(Serv.Last_Email_Sent__c == '240 days after IPC Support Start')
        {
        EmailHandler.send240AfterStartIPCEmail(lcontactEmails);
        }
        
        //Email handler Updated
        if(Serv.Last_Email_Sent__c == '40 days before IPC Support Expiry')
        {
        EmailHandler.send40BeforeExpiryIPCEmail(lcontactEmails);
        }
        
        //Email handler Updated
        if(Serv.Last_Email_Sent__c == 'Momentum Support Expiry')
        {
        EmailHandler.sendSupportExpiryMOMEmail(lcontactEmails);
        }
        
        
        //Email handler Updated
        if(Serv.Last_Email_Sent__c == '160 days after Momentum Support Start')
        {
        EmailHandler.send150AfterStartMOMEmail(lcontactEmails);
        }
        
        
        //Email handler Updated
        if(Serv.Last_Email_Sent__c == '320 days after Momentum Support Start')
        {
        EmailHandler.send300AfterStartMOMEmail(lcontactEmails);
        }
    
}    

}   
}
}

 
Best Answer chosen by Developer.mikie.Apex.Student
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,

1.Create a field in object(Service__c ) of type check.
2. In the same workflow, create a workflow rule , make that varible as true
3. in the trigger  put one condition like createdvarible==false then only call the remain logic.

thank you

All Answers

ra811.3921220580267847E12ra811.3921220580267847E12
Hi,
As per my understanding workflow is firing  number of times, so that mails are going. try to avoid workflow firing muiltiple times by creating boolean varaible.

thank you
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you for your reply. Would you be able to provide example code on what example you mean by boolean variable? Do you mean like a checkbox? Or something within the actual class?
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,

1.Create a field in object(Service__c ) of type check.
2. In the same workflow, create a workflow rule , make that varible as true
3. in the trigger  put one condition like createdvarible==false then only call the remain logic.

thank you
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
So i create a field such as Accept_mass_email__c (Checkbox). Then I create a workflow rule which will set this field to true and have the trigger update it back to false? Is that what you mean? Thank you so much for your time
ra811.3921220580267847E12ra811.3921220580267847E12
Yes you update the field  to true in the same work flow rule(with rule criteria of '(Service: Service NameEQUALSIntensive Property Coaching) AND (Service: Start of ServiceNOT EQUAL TOnull)' only.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you for your help my friend. I will get to work on trying to implement this tonight, then i will come back to provide kudos and best answer. Cheers :)