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
Bill ThachBill 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.



User-added imageUser-added imageUser-added image

 
Alain CabonAlain Cabon
Hi,

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];
 
Rajesh3699Rajesh3699
Hi,

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.
Bill ThachBill Thach

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"?



 

Alain CabonAlain Cabon
Hi Bill,

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.
 
Bill ThachBill Thach
So to give a litte more context into the records that would need updating. There would be no need to update the contacts that are already in the system. I have created a new record type and a custom Account Path for the organization. Would I still need code to do "dummy updates?"
Alain CabonAlain Cabon
No. Currently, when you create or update a contact by changing the checkbox, the "Number of Champions" field is updated correctly but there is a "one shot" treatment for all the contacts never updated since the creation of the trigger that is needed.
Bill ThachBill Thach
Is there a knowledge article or any resource that you would be able to link so that I can read to write this additional code needed?
 
Alain CabonAlain Cabon
You can also just update the "Number of Champions" field directly in account with the dataloader but you will have more preparation to do.

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.
Bill ThachBill Thach
I opened the Developer Console and then an Execute Anonymous window. Type the following: update [select id from contact limit 10000];

And I got this...

User-added imageUser-added image
Alain CabonAlain Cabon
Hi,

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).
Bill Thach 4Bill Thach 4
Here is my updated code: For some reason, when I try to add another contact and click the checkbox field "Champion" I am getting this error(see image)User-added image

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];
}