I have a batch working on approx 40k records in chunks of 200 records. Inside execute of the batch I am querying from one abject returning more than 50k records. Getting 500001 error. How to remove this error ? Any alternative solution to get rid of this. Start Query-

String str = 'SELECT 
        id, EQ_UsageSchedule__c, EQ_ValidationError__c, EQ_ProcessStatus__c,
        EQ_ServiceCode__c, EQ_Account__c, EQ_AssetLineItem__c,
        EQ_AccountLocation__c, EQ_IssueNumber__c, 
        EQ_SourceSystem__c, EQ_RecordKey__c, EQ_WorkId__c, EQ_QuantityID__c, 
        EQ_LoadMonth__c, EQ_Quantity__c 
    FROM EQ_TempSSP1Data__c 
    WHERE EQ_WorkId__c = :workid';

Execute Query:

for(Apttus_Config2__AssetLineItem__c x : [SELECT 
        id, Apttus_Config2__AccountId__r.Apttus_Billing__Active__c,
        Apttus_Config2__StartDate__c, Apttus_Config2__EndDate__c,
        Apttus_Config2__BillToAccountId__c, EQ_AssetFeeCode__c,
        Apttus_Config2__AssetStatus__c, Apttus_Config2__LocationId__c 
    FROM Apttus_Config2__AssetLineItem__c 
        (Apttus_Config2__BillToAccountId__c IN :accountIds) AND 
        Apttus_Config2__PriceType__c = 'Usage' AND 
        EQ_AssetFeeCode__c != null AND 
        Apttus_Config2__AssetStatus__c = 'Activated']
) {
    system.debug('value of asset line item-->'+x);

  • Have you tried to do batch on another object (that one, which gives 50k error)? and query first one from there?
    – kurunve
    Commented Feb 11, 2020 at 8:15
  • @kurunve I can not call a batch from execute. I dont think that is possible.
    – Suren
    Commented Feb 11, 2020 at 8:16
  • What I mean is that instead of having batch on first object (40k of records) -- do batch on second one (with more than 50k of records), and then query related records of first type.
    – kurunve
    Commented Feb 11, 2020 at 8:31
  • @Kurunve Actually here situation is, both the queries are expected to return more than 50k records. Also query which is in execute is completely dependednt on result of outer query.
    – Suren
    Commented Feb 11, 2020 at 8:51
  • It might help if you include some general idea on what your trying to achieve. Perhaps there is a different workaround. I have no idea if this would work, and it might be a bad idea anyway, but perhaps you could do a @ ReadOnly query in a scheduled batch, and then use an @ future method to do your update.
    – Kasper
    Commented Feb 11, 2020 at 12:16

1 Answer 1


Restructure your query so that the start method returns the data that there is the high volume of. So if you are presently returning a parent object and then querying the child object in execute, instead query the child object in start and include its related parent object in the start query too or query the parent object in execute.

This may or may not be logically possible, but it is the first thing to consider.

  • I can not use query in start returning more than 50 k due to some dependency on the outer query records precessing. I need batching on outer query at first place.
    – Suren
    Commented Feb 11, 2020 at 8:41
  • Hi @Suren, I suggest you include the queries in your question.
    – Keith C
    Commented Feb 11, 2020 at 10:19
  • Sure adding that piece of code.
    – Suren
    Commented Feb 11, 2020 at 11:07
  • Hi @Suren, You haven't provided quite enough info e.g. how is the accountIds variable populated from the data that originates in the start method.
    – Keith C
    Commented Feb 11, 2020 at 12:57
  • .@Keith Actualyy there is a lot of logic behind this. Not able to post the code here.
    – Suren
    Commented Feb 11, 2020 at 15:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .