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