function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sakthidharan Ambayiramsakthidharan Ambayiram 

not able to update a field in Account object with after Insert trigger

Scenario: To count the the total cases in the account whenever creating a new case and up[date the case count field in account object through trigger and class
Issue: Case Count field is not updated in account record when the new case getting created 

trigger newCaseInsert on Case (after insert) {
    for(Case cs: trigger.new)
    getCaseCount.getupdatedCount(cs.Account.id);
    }

public class getCaseCount {
     public static Integer CountValue;
     public static Integer getupdatedCount(String AccountId){
         CountValue =[Select COUNT() from Case where Account.id=:AccountId];
         List<Account> record= [Select id,Name,Case_Count__c from Account where Account.id=:AccountId];
         for(Account acts: record){
           acts.Case_Count__c=CountValue;
         }
         update record;
         return CountValue ; 
    }  
}
Best Answer chosen by sakthidharan Ambayiram
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sakthidharan,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CountCaseOnAcc on Case (after insert, after update, after delete) {
    
    Set <Id> accountIds = new Set <Id>();
    List <Account> lstAccountsToUpdate = new List <Account>();
    
    if(Trigger.isInsert){
        for(Case con:trigger.new){
            accountIds.add(con.accountID);
        }
    }
    
    if(Trigger.isUpdate|| Trigger.isDelete){
        for(Case con:trigger.old){
            accountIds.add(con.accountID);
        }
    }
    
    for(Account acc:[SELECT Id,Name,Number_Of_Cases__c,(Select Id from Cases) from Account where Id IN: accountIds]){
        Account accObj = new Account ();
        accObj.Id = acc.Id;
        accObj.Number_Of_Cases__c = acc.Cases.size();
        lstAccountsToUpdate.add(accObj);
    }
    
    if(lstAccountsToUpdate.size()>0){
        UPDATE lstAccountsToUpdate;
    }
}
This code will work when you insert, update or delete the cases.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sakthidharan,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CountCaseOnAcc on Case (after insert, after update, after delete) {
    
    Set <Id> accountIds = new Set <Id>();
    List <Account> lstAccountsToUpdate = new List <Account>();
    
    if(Trigger.isInsert){
        for(Case con:trigger.new){
            accountIds.add(con.accountID);
        }
    }
    
    if(Trigger.isUpdate|| Trigger.isDelete){
        for(Case con:trigger.old){
            accountIds.add(con.accountID);
        }
    }
    
    for(Account acc:[SELECT Id,Name,Number_Of_Cases__c,(Select Id from Cases) from Account where Id IN: accountIds]){
        Account accObj = new Account ();
        accObj.Id = acc.Id;
        accObj.Number_Of_Cases__c = acc.Cases.size();
        lstAccountsToUpdate.add(accObj);
    }
    
    if(lstAccountsToUpdate.size()>0){
        UPDATE lstAccountsToUpdate;
    }
}
This code will work when you insert, update or delete the cases.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
sakthidharan Ambayiramsakthidharan Ambayiram
Thanks Khan