Good day everyone I have created a batch process in salesforce. To explain more, below code is quite similar with my problem.

public with sharing class AS_batchUpdateContactStatus implements Database.batchable<Contact>, Database.Stateful{

    public AS_batchUpdateContactStatus() {


    public Iterable<Contact> start(Database.batchableContext batchableContext) {
        return [Select FirstName,LastName, (Select Id,Name,AS_userGroup__c from AS_User_userGroups__r) from Contact];

    public void execute(Database.BatchableContext BC, List<Contact> scope) {
        // some process here 

    public void finish(Database.BatchableContext BC) {



My problem is that from the very start of running my batch, it will give an error "First error: Too many query rows: 50001" It is because I already have lots of data inserted in salesforce. Can you please give me some idea how to reconstruct my batch in order to avoid this issue ?

  • 2
    If you use Iterable in the start method, you will be limited by the governor limit of 50k rows. Use database.querylocater instead which has a limit of 50M records. And since you're using nested query make sure the batch is comparatively smaller.
    – Sam
    Feb 8, 2016 at 8:54
  • @Mugambo thank you so much I will give it a try. I commended your comment.
    – Hope
    Feb 9, 2016 at 1:33

1 Answer 1


You can do couple of things:

1) Reduce the batch size, this will reduce the number of records in the scope of execute method

2) Add a selective where clause to restrict the number of records

  • I already tried to reduce the batch size but unfortunately, the problem still occur. About adding a selective where clause, I can't do that because I need to get all the record.
    – Hope
    Feb 8, 2016 at 8:38

You must log in to answer this question.

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