My apex code is throwing a SOQL 101 error on line 5. What can I do to resolve this?

 public class YodleeEscrowRefresh {
    Public void escrowrefresh(){
         List<Bank_Facilities__c> bflist =  [Select Id, Financial__c from Bank_Facilities__c Where Financial__c != Null];
        for(Bank_Facilities__c bf : bflist) {
            List<Financial__c> Financials = new List<Financial__c>([select id, Account__c from Financial__c where id =: bf.Financial__c AND Ready_for_Refresh__c=true limit 1]);

            if (Financials.size()>0)
            {
            List<Account> Acc = new List<Account>([select id, cin__c from Account where id =: Financials[0].Account__c limit 1]);
            if (Acc.size()>0)
            {
            YodleeBankStmtRefresh ybs = new YodleeBankStmtRefresh();
            system.debug('Account Details:' + acc[0].Id + ':' + acc[0].cin__c);
            ybs.bankstmtrefresh(acc[0].Id);
            system.debug('Account Details:' + acc[0].Id + ':' + acc[0].cin__c);
            }
         }
        }
    }
}
  • Please give help with code change – Aakash Shivach Oct 18 '16 at 8:51
  • 1
    Query is inside a for look, use a map instead to avoid it. I can write a code for you, but would be a nice exercise. if you try and do it yourself. – Raul Oct 18 '16 at 8:53
  • Read up on bulkification – Eric Oct 18 '16 at 8:56
  • You have an SOQL query inside a loop. You have to learn more about APEX because it's one of the fundamental principle of APEX Coding. – Akram G Oct 18 '16 at 9:04
  • Hi Rahul, Thanks a lot for having a look into it. I had tried using Map and also pulling the query out of For loop but it didnt worked can you help me in code please – Aakash Shivach Oct 18 '16 at 9:13
up vote 2 down vote accepted

First of all never put any SOQL query inside a for loop as it will reach the governor limit in Salesforce. This is the reason you are getting this error. As maximum SOQL query limit is 100.

Here is the Code that might help you as you are just passing the account parameters.

public class YodleeEscrowRefresh {
public void escrowrefresh(){
    List<Bank_Facilities__c> bflist = [SELECT Id, Financial__c FROM Bank_Facilities__c WHERE Financial__c != NULL];
    Set<Id> financialIdSet = new Set<Id>();
    for(Bank_Facilities__c bf : bflist) {
        financialIdSet.add(bf.Financial__c);
    }
    List<Financial__c> FinancialsList = [SELECT Id, Account__c FROM Financial__c WHERE Id IN: financialIdSet AND Ready_for_Refresh__c = true AND Account__c != NULL];
    Set<Id> accountIdSet = new Set<Id>();
    for(Financial__c fc : FinancialsList) {
        accountIdSet.add(fc.Account__c);
    }
    List<Account> accList = [SELECT Id, cin__c FROM Account WHERE Id IN: accountIdSet];
    if(accList.size() > 0) {
        for(Account acc : accList) {
            YodleeBankStmtRefresh ybs = new YodleeBankStmtRefresh();
            System.debug('Account Details:' + acc.Id + ':' + acc.cin__c);
            ybs.bankstmtrefresh(acc.Id);
            System.debug('Account Details:' + acc.Id + ':' + acc.cin__c);
        }
    }
}
}

But make sure to make YodleeBankStmtRefresh method "bankstmtrefresh" bulkified. So that it can take a List of Account Ids and can process them at once because it will an error if the bankstmtrefresh method is using any DML method. Hope this will help.

  • Happy to know that it solved your problem. – AKSHAY Dhiman Oct 18 '16 at 10:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.