You need to sign in to do that
Don't have an account?
sakthidharan 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 ;
}
}
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 ;
}
}
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.
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
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.
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