+ Start a Discussion
SambitNayakSambitNayak 

trigger to update parent record field from child

Hi,
I have a requirement where i need to update a field on Account object everytime a related case is closed.
The Field "Closed Cases" is a number field on Account object. Everytime, I close a case, this field should be incremented by 1.
I wrote a trigger but it's showing NULL Pointer Exception. 
Please let me know if you see any issues here:

 

public class caseTriggerHandler {

    public static void autoFillNoOfContacts(List<Case> caseList, map<id, Case> oldCase){
        Set<ID> AccIDs= New Set<ID>();
        List<Account> accsToUpdate = New List<Account>();
        
        for (Case cs:caseList){
            if(cs.status=='closed' && oldCase.get(cs.Id).status<>'closed'){
            AccIDs.add(cs.AccountId);
        }
        }
                       
        Decimal noOfCases= 0;
        for(Account acc: [SELECT ID, Closed_Cases__c, (SELECT Id,Status FROM Cases) FROM Account WHERE Id IN:AccIDs]){
            noOfCases+=noOfCases;
            acc.Closed_Cases__c+=noOfCases;
            accsToUpdate.add(acc);
        }
        update accsToUpdate;
    }
}

Best Answer chosen by SambitNayak
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sambit,

Can you try the below logic.
 
public class caseTriggerHandler {

    public  void autoFillNoOfContacts(List<Case> caseList, map<id, Case> oldCase){
        Set<ID> AccIDs= New Set<ID>();
        List<Account> accsToUpdate = New List<Account>();
        
        for (Case cs:caseList){
            if(cs.status=='closed' && oldCase.get(cs.Id).status!='closed'){
            AccIDs.add(cs.AccountId);
        }
        }
                       
       if(AccIDs != null && AccIDs.size() > 0){
        List<Account> lstAccount = [select id, Closed_Cases__c , (select id from Cases where status = 'Closed') from Account where id in: AccIDs];
        if(lstAccount.size() > 0){
            for(Account acc: lstAccount){
                acc.Closed_Cases__c  = acc.Cases.size();
            }
            
            update lstAccount;
        }
    }
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sambit,

Can you try the below logic.
 
public class caseTriggerHandler {

    public  void autoFillNoOfContacts(List<Case> caseList, map<id, Case> oldCase){
        Set<ID> AccIDs= New Set<ID>();
        List<Account> accsToUpdate = New List<Account>();
        
        for (Case cs:caseList){
            if(cs.status=='closed' && oldCase.get(cs.Id).status!='closed'){
            AccIDs.add(cs.AccountId);
        }
        }
                       
       if(AccIDs != null && AccIDs.size() > 0){
        List<Account> lstAccount = [select id, Closed_Cases__c , (select id from Cases where status = 'Closed') from Account where id in: AccIDs];
        if(lstAccount.size() > 0){
            for(Account acc: lstAccount){
                acc.Closed_Cases__c  = acc.Cases.size();
            }
            
            update lstAccount;
        }
    }
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​
This was selected as the best answer
SambitNayakSambitNayak
Hi Sai..
Working perfect!!
Thanks so much!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Thanks for confirming. Answered the same in stack exchange with details why your code failed. 

Thanks,