-1

I have a requirement where checkbox on Account object needs to be updated when checkbox on all of its contact is checked. Suppose that account has a checkbox 'Default__c'. This account is having 3 contacts. Contact object has 'Check__c' checkbox. Now consider the scenarios- 1. If all 3 contact's are having 'Check__c' checkbox as true then 'Default__c' checkbox on its account should be true. 2. If 2 contacts are having 'Check__c' checkbox true and 1 contact is having 'Check__c' checkbox as false then 'Default__c' checkbox on its account should be false. 3. If all 3 contact's are having 'Check__c' checkbox as false then 'Default__c' checkbox on its account should be false.

I want to implement the above functionality using aggregate and group by clause of soql in Trigger. Can anyone help me with the piece of code for implementing the above functionality? Or anyone is having any better solution approach to implement this, do share your thoughts.

please have a look at the code that currently I am using, it works fine. Please suggest logic wise if its fine or not.

trigger ContactTrigger on Contact(after insert,after update){
   if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate)){

   Map<Id, Account> accounts = new Map<Id, Account>();

   for(Contact con : Trigger.new){
      accounts.put(con.AccountId, new Account(Id=con.AccountId,  
      Number_False__c=0, Number_True__c=0));
     }

   for(AggregateResult result : [SELECT AccountId acId, Count(Id) 
       ContactCount,Check__c def FROM Contact WHERE AccountId IN: 
       accounts.keyset() GROUP BY AccountId,Check__c ]){

   if((Boolean)result.get('def')==false){
      accounts.put((Id)result.get('acId'), new Account(Id=          
       (Id)result.get('acId'), Number_False__c= 
       (Integer)result.get('ContactCount')));          
    }
  if((Boolean)result.get('def')==true){            
  if(accounts.containsKey((Id)result.get('acId'))){
      Decimal falseVal = accounts.get((Id)result.get('acId')).Number_False__c;
      Account ac = new Account();
      ac.Id = (Id)result.get('acId');
      ac.Number_False__c=falseVal;                  
      ac.Number_True__c=(Integer)result.get('ContactCount');
      accounts.put((Id)result.get('acId'), ac);
      }
    }
  }

Map<Id, Account> accToUpdate = new Map<Id, Account>();
for(Account acc: accounts.values()){
if((acc.Number_False__c==0 || acc.Number_False__c==null) && (acc.Number_True__c!=0 || acc.Number_True__c!=null)){
    accToUpdate.put(acc.Id, new Account(Id=acc.Id, Default__c=true));
}else{
    accToUpdate.put(acc.Id, new Account(Id=acc.Id, Default__c=false));
  }
}       
update accToUpdate.values();
 } }

Can anyone have a look on this and provide some comment. Thanks in advance.

3
  • What have try so far?
    – Reshma
    Dec 4, 2018 at 5:24
  • Hi @Reshma I have implemented this by quering on account and fetching all its contacts. But my approach is having 2 for loops. Just wanted to optimise my code using aggregate and group by functions of soql. If you have done something like that, do let me know Dec 4, 2018 at 6:08
  • Please share your code in question
    – Reshma
    Dec 4, 2018 at 6:15

1 Answer 1

2

You can do this pretty simply with DLRS. Of course, it's possible to write this yourself, but using an existing solution will help you from making mistakes; writing your own rollup summary code is harder than you might imagine.

Here's the DLRS solution. First, install it. Second, create two new fields on Account: Checked Contacts (Number) and Total Contacts (Number). Third, create two DLRS settings to sum total contacts and checked contacts. Fourth, create a process builder that sets the Account checkbox field to true when the numbers are equal and not zero, false otherwise. Fifth, run a DLRS scheduled job to force a recalculation. Once all this happens, you're done.

If you still want to write a trigger yourself, you're looking at approximately 40 lines of code, including all proper error handling. You will want to read our help topics about asking a question that is on-topic. We do not generally write code, but we will help you fix a reasonable attempt at writing your own trigger. Also, feel free to peruse our previous questions and answers that may help you.

3
  • Thanks for the quick reply @sfdcfox. Here I am specifically looking for trigger only, and if possible using aggregate and group by function on soql. If you have implemented that way, do let me know. Dec 4, 2018 at 5:59
  • 1
    @sagarmaurya Yes, I have written a trigger like this, specifically using aggregate/group by. You would have found this if you had taken my suggestion to check out our previous questions and answers. Again, we do not write code here; please review the previous answers, do some research, make a solid attempt at trying this, and if you should get stuck, ask a specific question with specific code, and we'll help you.
    – sfdcfox
    Dec 4, 2018 at 6:22
  • please have a look at the code that currently I am using, it works fine. Please suggest logic wise if its fine or not. Dec 5, 2018 at 11:23

Not the answer you're looking for? Browse other questions tagged .