You need to sign in to do that
Don't have an account?
Chad 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()); } }
All Answers