You need to sign in to do that
Don't have an account?
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:
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); } } } } }
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
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
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