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
Chad MoutesChad Moutes 

Apex Trigger count only if check box is checked.

I have an Apex Trigger listed below, what it does is it shows a count of how many Projects are linked to an Account, it works perfectly but I added a checkbox to the Project object and I would like to now only count Projects that have that checkbox checked. Can anyone help me with this?
 
trigger RollupActiveProjectsCountToAccountTrigger on Project__c (after insert, after delete, after undelete) {

   List<Id>AccountIds = new List<Id>();    
   
   If(Trigger.isInsert || Trigger.isUndelete){
       for(Project__c p: Trigger.new){
           AccountIds.add(p.Company_Name__c);
       }
   }
   
   If(Trigger.isDelete){
       for(Project__c p: Trigger.old){
               AccountIds.add(p.Company_Name__c);
       }
   }
   
   AggregateResult[] groupedResults = [SELECT count(id)projectCount, Company_Name__c FROM Project__c WHERE Company_Name__c in :AccountIds GROUP BY Company_Name__C];
   
   Map<Id,Account>accountMap = new Map<Id,Account>([SELECT Id, Total_Active_Projects__c FROM Account WHERE Id in :AccountIds]);
   
   for(AggregateResult ar: groupedresults) {
       accountMap.get((Id)ar.get('Company_Name__c')).Total_Active_Projects__c = (decimal)ar.get('Projectcount');
   }
   
   try {
       update accountMap.values();
   } catch(DmlException e) {
       System.debug(e.getMessage());
   }

}

 
Best Answer chosen by Chad Moutes
KaranrajKaranraj
You need to update the query in the trigger code. Just include your checkbox field in the where condition of soql query, then it will fetch product to which the checkbox is enabled.
AggregateResult[] groupedResults = [SELECT count(id)projectCount, Company_Name__c FROM Project__c WHERE Company_Name__c in :AccountIds and checkboxField__c == True GROUP BY Company_Name__C];
Please replace checkboxField__c into the API name of the field in the query

All Answers

KaranrajKaranraj
You need to update the query in the trigger code. Just include your checkbox field in the where condition of soql query, then it will fetch product to which the checkbox is enabled.
AggregateResult[] groupedResults = [SELECT count(id)projectCount, Company_Name__c FROM Project__c WHERE Company_Name__c in :AccountIds and checkboxField__c == True GROUP BY Company_Name__C];
Please replace checkboxField__c into the API name of the field in the query
This was selected as the best answer
Chad MoutesChad Moutes
I added the field into the query, but now I'm getting this error unexpected token: '==' at line 17 column 160
 
AggregateResult[] groupedResults = [SELECT count(id)projectCount, Company_Name__c FROM Project__c WHERE Company_Name__c in :AccountIds and Active_Project__c == TRUE GROUP BY Company_Name__C];

 
Chad MoutesChad Moutes
Nevermind, lol. I figured it out. Thankss for the help.
KaranrajKaranraj
It is single equal to, just replace it 
AggregateResult[] groupedResults = [SELECT count(id)projectCount, Company_Name__c FROM Project__c WHERE Company_Name__c in :AccountIds and Active_Project__c = TRUE GROUP BY Company_Name__C];