You need to sign in to do that
Don't have an account?
HARSHIL U PARIKH
Counting Unique Email of Contacts On Account via Trigger. Trigger works for individual record but when Mass Updated it behaves unexpected..
Hello Developes!
I have a trigger which counts number of unique email address contacts on Account.
Trigger Code:
Account - 1 gets updated with values of Unique_Email_Contacts__c as 1
Account - 2 gets updated with values of Unique_Email_Contacts__c as 2
Account - 3 gets updated with values of Unique_Email_Contacts__c as 3 etc...
Thank you for the help!
I have a trigger which counts number of unique email address contacts on Account.
Trigger Code:
Trigger UniqueEmailCons on Contact(After Insert, After Update, After Delete, After Undelete){ List<ID> accountIds = New List<ID>(); If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUndelete){ For(Contact con : Trigger.New){ accountIds.add(con.AccountID); } } If(Trigger.IsDelete){ For(Contact con : Trigger.old){ accountIds.add(con.AccountID); } } Set<String> UniqueEmails = New Set<String>(); List<Account> AccountListToUpdate = New List<Account>(); For(Account act : [Select ID, Unique_Email_Contacts__c, (Select Id, Email FROM Contacts) FROM Account WHERE Id = :accountIds]) { act.Unique_Email_Contacts__c = 0; For(Contact c : act.contacts) { If(C.Email != null) { UniqueEmails.add(C.Email); } } act.Unique_Email_Contacts__c = UniqueEmails.size(); AccountListToUpdate.add(act); } try{ update AccountListToUpdate; } Catch(Exception e){ System.debug('Exception thrown is::: ' + e.getMessage()); } }Trigger works fine for individual getting inserted, updated, deleted record etc.. but when I update all the contacts togather then all the accounts in org (let's say 18 ) of them gets updated as follow:
Account - 1 gets updated with values of Unique_Email_Contacts__c as 1
Account - 2 gets updated with values of Unique_Email_Contacts__c as 2
Account - 3 gets updated with values of Unique_Email_Contacts__c as 3 etc...
Thank you for the help!
Hi Govind,
Try this
Thanks
Vivian
All Answers
Hi Govind,
Try this
Thanks
Vivian