You need to sign in to do that
Don't have an account?
Mhlangano Khumalo
HELP with bulkification: I want to remove an SQL query inside a for loop.
I'm receiving a JSON string from another system in the format below. These records already exist in Salesforce. I want to only query these records in salesforce, set a boolean field to false in all of them and update.
The below code works fine, the issue is it's not bulkified.
The problem is the FOR loop. I tried using the IN clause in SOQL but the problem is im not getting the ID in the JSON string.
PLEASE HELP.
{ "bacctList" : [{ "RC_Account_No__c" : "RC-2", "A_c_Number__c" : "111", "Bank_code__c" : "250655" }, { "RC_Account_No__c" : "RC-1", "A_c_Number__c" : "222", "Bank_code__c" : "198765" }] }
The below code works fine, the issue is it's not bulkified.
@RestResource(urlMapping='/dosystem/bankaccounts/*') global with sharing class BankAccountWebservice{ @HttpPatch global static BankAccountWrapper doPatch(List<Bank_Account__c> bacctList) { RestRequest req = RestContext.request; RestResponse res = RestContext.response; BankAccountWrapper response = new BankAccountWrapper(); List<Bank_Account__c> bankAccountsToUpdate= new List<Bank_Account__c>(); for(integer i=0; i<bacctList.size(); i++){ List<Bank_Account__c> ac = [SELECT Debit_Order_A_c__c,A_c_Number__c,Bank_code__c,RC_Account_No__c FROM Bank_Account__c WHERE (RC_Account_No__c =: bacctList[i].RC_Account_No__c AND A_c_Number__c =: bacctList[i].A_c_Number__c AND Bank_code__c =: bacctList[i].Bank_code__c)]; for(integer j=0; j< ac.size(); j++){ ac[j].Debit_Order_A_c__c = false; bankAccountsToUpdate.add(ac[j]); } } try { update bankAccountsToUpdate; response.bacctList = bankAccountsToUpdate; response.status = 'Success'; response.message = bacctList.size()+' Records Updated successfully'; } catch(Exception exc) { res.StatusCode = 500; response.bacctList = null; response.status = 'Error'; response.message = 'Your request failed with the following error: ' + exc.getMessage(); } return response; } global class BankAccountWrapper { public List<Bank_Account__c> bacctList; public String status; public String message; public BankAccountWrapper(){ bacctList = new List<Bank_Account__c>(); } } }
The problem is the FOR loop. I tried using the IN clause in SOQL but the problem is im not getting the ID in the JSON string.
List<Bank_Account__c> bankAccountsToUpdate= new List<Bank_Account__c>(); integer updatedRecordCount=0; List<Bank_Account__c> ac= [select Name,Debit_Order_A_c__c,A_c_Number__c,Bank_code__c From Bank_Account__c where Id IN : bacctList]; for(integer i=0; i<ac.size(); i++){ ac[i].Debit_Order_A_c__c = false; bankAccountsToUpdate.add(ac[i]); updatedRecordCount++; }
PLEASE HELP.
and you had the following data stored on your records
and your code ran then C345 would be updated and it shouldn't.
I now see your initial sample code and below is some updated code. Also there was a typo in my initial code with the map declaration.
NOTE: This code has not been tested and may contain typographical or logical errors
All Answers
NOTE: This code has not been tested and may contain typographical or logical errors
I'm getting this error, what should l change?
and you had the following data stored on your records
and your code ran then C345 would be updated and it shouldn't.
I now see your initial sample code and below is some updated code. Also there was a typo in my initial code with the map declaration.
NOTE: This code has not been tested and may contain typographical or logical errors