+ Start a Discussion
sonam gupthasonam guptha 

trigger to make value as zero if no values present no child records present on it


Below is my trigger iam counting child records based and prinitng on parent object,but one error iam seeing if there are no child objects with that picklist value still its showing count as 1,i wanted to show the value as zero,if no records present on it,please help me out
trigger requirementsTrigger on Requirements__c (after insert, after update) {
    List <Id> progIds = new List<Id> ();
    List <Programs__c> programs = new List<Programs__c>();
    List <AggregateResult> requirements = new List<AggregateResult>();
    for(Requirements__c req:trigger.new){
    programs = [Select Id, Count__c From Programs__c Where Id In :progIds];
    requirements = [Select Programs__c, Count(Id) From Requirements__c Where Programs__c IN: progIds
            AND Waived__c = 'Yes' AND Status__c IN ('Active')
            Group By Programs__c];
    for(AggregateResult ar: requirements){
        for(Programs__c p:programs){
            if(ar.get('Programs__c') == p.Id){
                p.Count__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0')));

v varaprasadv varaprasad
Hi Sonam,

please add below debug and check value once.

Please add program to list  and update the list.
update programs;

sonam gupthasonam guptha
if i add a list iam its not able to count the values?
sonam gupthasonam guptha

on the above trigger am i following all the best practices,can you please have a look?
And any solution for to make the value zero