+ Start a Discussion
learn_sfdclearn_sfdc 

Though i have entered limitation - Too Many Query rows 50001

Hello,

I have used limitation in query still i am facing the same problem, can anyone help me here please.

global class POCreationBatch implements Database.Batchable<sObject>
{

    global Database.QueryLocator start(Database.BatchableContext DBC)
    {
         return Database.getQueryLocator([Select Id,Name,Purchase_Order_Type__c,ActiveContractAsset_Count__c from Account where Purchase_Order_Type__c='Required for Invoice' limit 5000]);

             
    }

    global void execute(Database.batchableContext bc,List<sObject> scope)
    {

        List<Account> accountList = (List<Account>)scope;
        Set<Id> accountIdSet = new Set<Id>();

        for(Account accountRecord : accountList)
        {
            accountIdSet.add(accountRecord.id);
        }

        ProductOrderUtils POU = new ProductOrderUtils();
        POU.createPurchaseOrders(accountIdSet);
    }


    global void finish(Database.batchableContext BC){}

}
Best Answer chosen by learn_sfdc
Dev_AryaDev_Arya
hi learn_sfdc,

As I mentioned before, the SELECT statements result per transaction are cummulative (result count of each select statement is summed). Now lets say, each batch has 200 records to process and for each record in that batch you are calling  'createPurchaseOrders'. In this 'createPurchaseOrders' function you have three select statements, each with LIMIT of 15000.  Your code perhaps is hitting the query limit processing the second record of the batch. Please put proper debug or try catch  statements to get the right  error spot. 

Also please use 'Add a code sample' tool to add code for proper visualization and understanding of the code,

If this helps your question, please mark the solution as green.
Cheers,Dev
 

All Answers

Dev_AryaDev_Arya
hi learn_sfdc,

could it be, that the other functions 'createPurchaseOrders' that you have used have also SELECT statements and cummulatively they are exceeding the limit? 

Cheers,Dev
v varaprasadv varaprasad
HI ,

Please check once if below method also having soql or not.
Please use LIMIT in there also otherwise use below limits class.
Check debug logs also which query retrieving more rows.

Use the following condition where ever you have soql Limits.getQueries() == Limits.getLimitQueries()​
 
List<Account> accnts = [select id from account limit :Limits.getLimitQueries()];

//Then you could check to see if the max was reached:
if(Limits.getQueries() == Limits.getLimitQueries()){
 system.debug('The max number of records were returned and the List may be incomplete.');
}

I hope it helps you.

Please let me know in case of any other assistance.

Thanks
Varaprasad
 
learn_sfdclearn_sfdc
I have used in below method but no luck

public void createPurchaseOrders(Set<Id> accountIdSet)
    {
        for(Opportunity oppRecord : [Select Id,Name,AccountId from Opportunity where Name like:'%' +oppName+ '%' and AccountId IN :accountIdSet limit 15000])
        
        {
            mapAccountOpportunity.put(oppRecord.AccountId,oppRecord);
            
        }


        List<Acct_By_Sub__c> accountBySUBRecordList = new List<Acct_By_Sub__c>([Select Id,(Select Id,NS_Currency__c,BG_Client_Contracting_Name__c,
                BG_Misys_Billing_Entity__c,Account_by_Sub__c,Account_by_Sub__r.Account__c,BG_Billing_Group_Active__c from Billing_Groups__r where BG_Billing_Group_Active__c = true)
        from Acct_By_Sub__c where Account__c IN:accountIdSet limit 15000]);
        

        if(accountBySUBRecordList!=null && accountBySUBRecordList.size()>0)
        {
            purchaseOrdersToUpdate = getPurchaseOrders(accountBySUBRecordList);
        }


        for(Billing_Group__c BillingGroupRecd : [Select Id,Account_by_Sub__c,Account_by_Sub__r.Account__c,(Select Id,Name,System__c,Annual_Maintenance__c,Contract_Annual_Maintenance__c,
                CA_Billing_Status__c,NS_Subscription_Item_Start_Trigger__c,
                NS_Subscription__c,X3PP_Recurring_Costs__c,X3PP_Legacy_Coral_Sales_Ref__c,
                X3PP_Asset_Location__c,Account__c,CA_Billing_Group__c,
                Purchase_Order_Request__c from Contract_Assets__r where CA_Billing_Status__c='Active billing') from Billing_Group__c where Id IN :BillingGroupSetId limit 15000])
 
        {
            mp_ListContractAsset.put(String.valueOf(BillingGroupRecd.Id),BillingGroupRecd.Contract_Assets__r);
            


        }
 
Ginny MahantGinny Mahant
Could you share code of  getPurchaseOrders
learn_sfdclearn_sfdc
 public List<Purchase_Order__c> getPurchaseOrders(List<Acct_By_Sub__c> accountBySUBRecords)
    {

        for(Acct_By_Sub__c acctbySubRecd : accountBySUBRecords)
        {

            for(Billing_Group__c billingGroup : acctbySubRecd.Billing_Groups__r)
            {
                
                String strKey_AcctbySub = billingGroup.Account_by_Sub__r.Account__c+''+ billingGroup.BG_Misys_Billing_Entity__c +''+ billingGroup.NS_Currency__c;

                if(mp_DuplicateBG_AccountbySub.containsKey(strKey_AcctbySub))
                {

                    List<Billing_Group__c> billingGroupListacctbySub = mp_DuplicateBG_AccountbySub.get(strKey_AcctbySub);
                    billingGroupListacctbySub.add(billingGroup);
                    mp_DuplicateBG_AccountbySub.put(strKey_AcctbySub,billingGroupListacctbySub);
                }

                else {
                    mp_DuplicateBG_AccountbySub.put(strKey_AcctbySub, new List<Billing_Group__c>{billingGroup});
                }

            }
        }


        if(mp_DuplicateBG_AccountbySub!=null && mp_DuplicateBG_AccountbySub.values()!=null)
        {

   

            for(String billingGroupKey : mp_DuplicateBG_AccountbySub.keyset())
            {
                
                List<Billing_Group__c> billingGroupsForAccount = mp_DuplicateBG_AccountbySub.get(billingGroupKey);

                Purchase_Order__c PO_Order= createPurchaseOrder(billingGroupsForAccount);

                if(PO_Order!=null)
                {
                    purchaseOrders.add(PO_Order);
                }

            }
        }

        if(purchaseOrders!=null && purchaseOrders.size()>0)
        {
            return purchaseOrders;
        }

        return null;
    }
Ginny MahantGinny Mahant
Suggest you please put some try catch blocks in the code to see which statement is causing the exception. That will help in understanding how to get rid of the error.
Dev_AryaDev_Arya
hi learn_sfdc,

As I mentioned before, the SELECT statements result per transaction are cummulative (result count of each select statement is summed). Now lets say, each batch has 200 records to process and for each record in that batch you are calling  'createPurchaseOrders'. In this 'createPurchaseOrders' function you have three select statements, each with LIMIT of 15000.  Your code perhaps is hitting the query limit processing the second record of the batch. Please put proper debug or try catch  statements to get the right  error spot. 

Also please use 'Add a code sample' tool to add code for proper visualization and understanding of the code,

If this helps your question, please mark the solution as green.
Cheers,Dev
 
This was selected as the best answer