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
GFT TimGFT Tim 

Help with getting Apex code to update a Case

Below is my code. The case record through debug log says it updates however it does not populate the field on the record.  When I run the code through Developer Console and run it anonyomusly the code runs properly and the field populates on the record.  
This code is part of a TriggerHandler class for Case on BeforeUpdate.  Basically anytime a case is update where:
Case Reason = Withdrawal
Case Sub_Type__c = Multiple
and I get the Banking_Site value from the first record in the related list Failed_Transactions
and populate the case field ExtAPIBankSite__c with the value.



        set<ID> myCase = new set<ID>();
        list<Case> cList = new list<case>();
        Map<Id,String> cMap = new map<Id,String>();
       
        for(Case c : lstCases){
            if(c.Reason == 'Withdrawal' && c.Sub_Type__c == 'Multiple'){
                if(c.ExtAPIBankSite__c == null){
                    myCase.add(c.id);  
                }                                   
            }
        }
       
       cList = [SELECT Id,ExtAPIBankSite__c,(SELECT Id,Banking_Sites__c FROM Failed_Transactions__r ORDER BY Process_Order__c ASC Limit 1)FROM      Case WHERE Id in:myCase];
       
        for(Case cLoop : cList){                     
            cMap.put(cLoop.Id,cLoop.Failed_Transactions__r[0].Banking_Sites__c);
            if(cMap.get(cLoop.Id)!= null){                               
                 cLoop.ExtAPIBankSite__c = cMap.get(cLoop.Id);
            }
        }
     }
Best Answer chosen by GFT Tim
EnreecoEnreeco
Oh I get it.
The problem is that you are updating not the Cases in the Trigger (from the lstCases) but you are changing the values of the queried cList cases that are not part of the trigger (so you set the new value but no update is done actually).
You need to iterate through the lstCases list also in the second for loop.
Hope this helps

All Answers

EnreecoEnreeco
Hi,
can be a formula field (if any) is not already set in the trigger? I'm talking about Case.Sub_Type__c field.
Have you tried to put some System.debug over the for loops to get if the values are actually set?
GFT TimGFT Tim
Case.Sub_Type field is a picklist field.  The Banking_Sites__c field is a formula field.  I had debug statements all over this code to see what could be the problem and I can see in the debug logs that the cLoop.ExtAPIBankSite__c field is being set and the record not updating.
EnreecoEnreeco
Oh I get it.
The problem is that you are updating not the Cases in the Trigger (from the lstCases) but you are changing the values of the queried cList cases that are not part of the trigger (so you set the new value but no update is done actually).
You need to iterate through the lstCases list also in the second for loop.
Hope this helps

This was selected as the best answer
GFT TimGFT Tim
UGH I'm kicking myself in the head.....Thanks ForceLogic!!
EnreecoEnreeco
Ahahaha these are the benefits of pair programming :)