You need to sign in to do that
Don't have an account?
Merry S
Cannot get trigger to update account field
I am trying to update a field on the account that comes from a case. This trigger should look at all cases related to the account and determine if there is and R/Y/G string. If there is R it should set the account field to R, if there is no R and no G it should set it to Y and if the default would be G. I have worked on this for hours - I am new to this and I just don't know enough to get it to work. I have posted this on both here and the stack exchange. Thanks in advance for any help. Let me know if you need more detail.
trigger CaseStatusUpdate on Case (after update, after insert) { List<ID> AccIDs = new List<ID>(); List<Account> updateAccList = new List<Account>(); For( Case c1 : Trigger.New){ AccIDs.add(c1.AccountId); } Map<ID,Account> AccUpdateMap = new Map<ID,Account>(); for(Account acc : [Select ID , AccCaseStatusCalcGRY__c from Account where ID in:AccIDs]) { AccUpdateMap.put(acc.ID ,acc ); } For(Case c1: Trigger.New){ Account updateacc =AccUpdateMap.get(c1.AccountID); if (c1.CaseStatusCalcGRY__c == 'R') { updateacc.AccCaseStatusCalcGRY__c = 'R'; } else if (c1.CaseStatusCalcGRY__c != 'R' && c1.CaseStatusCalcGRY__c != 'G') { updateacc.AccCaseStatusCalcGRY__c = 'Y'; } else { updateacc.AccCaseStatusCalcGRY__c = 'G'; updateAccList.add(updateacc); } if(updateAccList!=null && updateAccList.size()>0) update updateAccList; } }
I missed declaring the AccIds variable.
Try this one.
All Answers
It's simple
One closing bracket was missing in earlier one
Thanks! That worked, to a point. It is now updating the field on the acccount, but it does not appear to be looking at all cases. It will set to R/Y/G based on the newest case added to the account. I am not sure how to get it to look at all cases and make the decision.
This should work for you
There is still a bit of work to be done to handle when case records are deleted and undeleted, but hopefully you should be able to figure those out.
Thank You. I am getting this error now.
Error: Compile Error: Variable does not exist: AccIds at line 10 column 134
I missed declaring the AccIds variable.
Try this one.