You need to sign in to do that
Don't have an account?
Modification on current trigger?
Hi, want to modifying this trigger to include only specific records. So basically I have number of contacts for the account shown on account record. This is calculated via the below trigger: There is a custom number field on account Number_of_contacts and the number is pushed via this trigger on account. Now, I have a need where I only need to push number of contacts that are checked (Motivator on Board - a custom checkbox field) on contact records not all contacts. I can make customer number field on account such as "Number of Total MOBs" but I want a trigger to push it.
Thank you!!
trigger ContactTickerTrigger on Contact (After insert, After delete, After undelete, After Update) { Set<Id> parentIdsSet = new Set<Id>(); List<Account> accountListToUpdate = new List<Account>(); IF(Trigger.IsAfter){ IF(Trigger.IsInsert || Trigger.IsUndelete || Trigger.isUpdate){ FOR(Contact c : Trigger.new){ if(c.AccountId!=null){ parentIdsSet.add(c.AccountId); If(Trigger.isUpdate){ if(Trigger.oldMap.get(c.Id).AccountId != c.AccountId){ parentIdsSet.add(Trigger.oldMap.get(c.Id).AccountId); } } } } } IF(Trigger.IsDelete){ FOR(Contact c : Trigger.Old){ if(c.AccountId!=null){ parentIdsSet.add(c.AccountId); } } } } System.debug('#### parentIdsSet = '+parentIdsSet); List<Account> accountList = new List<Account>([Select id ,Name, Number_of_Contacts__c, (Select id, Name From Contacts) from Account Where id in:parentIdsSet]); FOR(Account acc : accountList){ List<Contact> contactList = acc.Contacts; acc.Number_of_Contacts__c = contactList.size(); accountListToUpdate.add(acc); } try{ update accountListToUpdate; }catch(System.Exception e){ } }
Thank you!!
List<Account> accountList = new List<Account>([Select id ,Name, Number_of_Contacts__c, (Select id, Name From Contacts) from Account Where id in:parentIdsSet]);
Even though we applied the filter in the beginning, when this SOQL query executes, it contained all the contacts that were associated to an account and did not filter the result based on if Motivator_On_Board__c checkbox was checked within those Contacts. Thus, I applied the filter here to ensure that it only picks up those specific contacts. Below is the revised code:
Apex Class: I hope this resolves your issues!
All Answers
Replace everywhere you state if(c.AccountId!=null) with if(c.AccountId!=null && c.Motivator_on_Board__c = true)
Note: I assume that the API for the checkbox was Motivator_on_Board__c but if it isn't please replace it with the correct API from your org.
As a Salesforce Best Practice, I strongly suggest moving the code you have in this trigger to a separate Apex Class. It makes maintanenance of the code much easier. Imagine if in the future, your company requests to add more functionality to the trigger to do some other action. Incorporating that functionality into this trigger would be no easy task and at some point, it'll become very hard to follow as to what is doing what.
Thanks for the code edit. However, when I am using this code edit it showing the error - Error: Compile Error: Expression cannot be assigned at line 7 column 20
Hi Mit, my apologies. This is a minor typo on my end. Replace the single equal sign with 2 equals. Thus, it should look like this:
if(c.AccountId!=null && c.Motivator_on_Board__c == true)
That should resolve the issue you are having. My apologies again.
Contacts) from Account Where id in:parentIdsSet]); FOR(Account acc : accountList){ List<Contact> contactList = acc.Contacts; acc.Number_of_Contacts__c = contactList.size(); accountListToUpdate.add(acc); } try{ update accountListToUpdate; }catch(System.Exception e){ } }
List<Account> accountList = new List<Account>([Select id ,Name, Number_of_Contacts__c, (Select id, Name From Contacts) from Account Where id in:parentIdsSet]);
Even though we applied the filter in the beginning, when this SOQL query executes, it contained all the contacts that were associated to an account and did not filter the result based on if Motivator_On_Board__c checkbox was checked within those Contacts. Thus, I applied the filter here to ensure that it only picks up those specific contacts. Below is the revised code:
Apex Class: I hope this resolves your issues!