+ Start a Discussion
arunadeveloperarunadeveloper 

Breaking the list (if more than 50,000 records)

Hello There,

 

I need to pull the case records in soql query, but i am getting more than 50,000 records in a list.

So here it is saying limit exceeds.

 

Can we break a list based on limit variable? if we can any one suggest me how to do this.

If anyone is having sample code  please share with me .

 

 

Thank you,

 

vriavmvriavm

I just typed the code please fix in case of any typing mistakes...

List<Sobject> main = new List<Sobject>();

Map<Integer,List<Sobject> lstMap = new Map<Integer,List<Sobject>();

Integer i = 0;

Integer counter = 0;

List<Sobject> soqlresult = [select id from Sobject where id limit 100000];

for (Sobject sobj : soqlresult) {

       i++;

       main.add(sobj);

       if (Math.MOD(i,50000) == 0) {

             counter++;

             lstMap.add(counter, main);

             main = new List<Sobject>();

       }

}

arunadeveloperarunadeveloper

Hi Thank for the replay,

but still i am getting  "System.LimitException: Too many query rows: 50001"  error

 

 

List<Case> caseMain=new List<Case>();
Map<Integer,List<Case>> isMap=new Map<Integer,List<Case>>();
Integer i=0;
Integer counter=0;
List<Case> soqlresult=[select id from Case limit 100000];
for(Case c :soqlresult){
    i++;
    caseMain.add(c);
    if (Math.MOD(i,50000) == 0) {
         counter++;
        isMap.put(counter,caseMain);
        caseMain=new list<Case>();
     }

}

vriavmvriavm

This limitation is on the maximum number of rows that can be retrieved by a query in Vifualforce page. this is SFDC limitation... You may need to think at reducing the count and reduce the percentage value in my code to lesser value...

JitendraJitendra

Hi,

 

You cannot get more than 50,000 records in single query, however you can implement the pagination logic.

 

Refer below article, offset released in Spring 12:

http://blogs.developerforce.com/developer-relations/2012/01/soql-offset-in-spring-12.html