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

Trigger to count won cases,lost and all cases
Hi
I am trying to write a trigger to update account custom fields with total,won and lost cases.unable to get won and lost cases number.(I had tried to write soql for won and lost cases.but not sure how can i update them?do i need to update individual list?Is there any other way)
Also when i delete a case,how can i update the count.
please help!!
I am trying to write a trigger to update account custom fields with total,won and lost cases.unable to get won and lost cases number.(I had tried to write soql for won and lost cases.but not sure how can i update them?do i need to update individual list?Is there any other way)
Also when i delete a case,how can i update the count.
please help!!
trigger counttrigger on Case (after insert, after update,after delete) { List<Case> lstCase = [select id, AccountId from Case where id in: trigger.newmap.keyset()]; set<Id> sAccId = new set<Id>(); for(Case cs: lstCase) { if(cs.AccountId != null) { sAccId.add(cs.AccountId); } } if(sAccId != null && sAccId.size() > 0){ List<Account> lstAccount = [select id,Totalcases_c,Woncases__c,Iostcases__c, (select id,Status from Cases ) from Account where id in: sAccId]; for(Account acc:lstAccount ){ acc.Totalcases__c = lstAccount.Cases.size(); } } } }
Please use below code. It calculates Totalcases_c,Woncases__c,Iostcases__c values from cases to Accounts. Also, I`m assumiung you have 'Lost' and 'Won' status values on your Case Object.
Please mark this as Best Answer if it helps!