You need to sign in to do that
Don't have an account?

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;
}
}
Can you try the below logic.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
Can you try the below logic.
If this solution helps, Please mark it as best answer.
Thanks,
Working perfect!!
Thanks so much!!
Thanks for confirming. Answered the same in stack exchange with details why your code failed.
Thanks,