You need to sign in to do that
Don't have an account?
Bill Thach
Apex trigger rollup summary account contact checkbox
I noticed that I cannot create a rollup summary from the Account and Contact objects. I have created a custom checkbox on the contact field labeled "Champion" as well as a custom number field on the Account object labeled "Number of Champions." I created a trigger on the contact object but it does not seem update the "Number of Champions" field. This is my first trigger, so guidance would be greatly appreciated.
Your trigger works but there is no retroactive effects by default.
The trick is to update all the Ids of the existing contacts (just the ids, dummy updates but that will trigger your code).
update [select id from contact limit 10000];
Try out process builder....no need of coding for this requirement.
Sicne we can update Lookup fields in process builder [ but not in workflow]
Thank You,
Rajesh Adiga P.
Thank you Alaln,
I am a complete newbie with triggers. I'm looking at what you wrote. My question is do I replace the code: "(Select Id, Champion__c from Contacts)" with "contact limit 10000"?
update [select id from contact limit 10000];
... is a command for the anonymous windows in the developer console (execution of apex directly).
You have perhaps more than 10,000 contacts and that will be not sufficient so the dataloader will be needed.
Your code works for new updates or creations (and deletions) but you need "dummy" updates for all the existing contacts in order to trigger your code for all of them.
Does a trigger update the existing records?
https://developer.salesforce.com/forums/?id=906F000000090j6IAA
https://success.salesforce.com/answers?id=90630000000hQQkAAM
Salesforce also wrote about the very common trick : update [select id from contact limit 10000];
sfdcfox is always very concise and interesting:
https://salesforce.stackexchange.com/questions/133823/how-to-touch-all-records-in-an-object-to-run-a-trigger
... because you could need a batch treatment just for the "one shot" initialization if you have a lot of contacts and you cannot calculate all the number of champions easily.
And I got this...
You must write exactly the command with "update" front of "[" (that is the most important).
It is a syntax very specific and surprising at the beginning.
update [select id from contact limit 10000];
... that means "update" the list that contains just Ids of contacts (up to 10,000 read contacts).
You will update Ids with the same Ids (dummy) but the goal is to trigger your code (after update).
trigger NumberOfChampionsCount on Contact (after insert, after delete, after undelete,after update) {
set<Id> accIds = new set<Id>();
if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
for(Contact con: Trigger.new){
accIds.add(con.AccountId);
}
}
if(trigger.isUpdate || trigger.isDelete) {
for(Contact con: Trigger.old){
accIds.add(con.AccountId);
}
}
List<Account> accList = [select id, Number_of_Champions__c, (Select Id, Champion__c from Contacts) from Account Where ID IN: accIds];
for(Account acc : accList){
system.debug('Contacts--->'+acc.contacts.size());
acc.Number_of_Champions__c = 0;
for(Contact con : acc.Contacts) {
if(con.Champion__c)
acc.Number_of_Champions__c++;
}
}
update [select id from contact limit 10000];
}