function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gautamgautam 

How to Retrieve more than 10000 records in controller

Hi All,

 

In my controller , i am trying to retrieve more than 10000 records. But because we have a limit of 10000 records in salesforce, i am not able to do this.

 

Can anybody please tell me how we can query >10000 records in controller or some workaround for this ?

Best Answer chosen by Admin (Salesforce Developers) 
gautamgautam

Values from batch apex cant be passed back to page.It has to be stored in some object and then call back in page.

Need to keep a refresh button on Page to see the latest values.

All Answers

paul-lmipaul-lmi

batch apex.  the limits like this are put in place so your processes don't impact other users (shared environment).  batch apex ensure you can do things with big sets of data, while ensuring your stuff doesn't hog resources on the instance your org is on.

gautamgautam

Thanks for your reply...

 

Can you please let me know how it is done using batch apex..

 

One example or sample would be helpful...

gautamgautam

Hi All,

 

I have written the below batch apex class. But its not running. In debug logs, i can see that it runs upto the variable and no methods(including navigate() ) are executed.Please let me know what i have missed out....

 

 

global class BatchApexMatrixBulk implements Database.Batchable<SObject>,Database.AllowsCallouts
{
    
  public String GMap_URL {get; set;}
  public String Gmap_marker {get; set;}
  public List<String> lstX0Y0= new List<String>();
  public List<String> lstX0Y1= new List<String>();
  public List<String> lstX1Y0= new List<String>();
  public List<String> lstX1Y1= new List<String>();
  public list<string> getmatrix1(){return lstX0Y0;}
  public list<string> getmatrix2(){return lstX1Y0;}
  public list<string> getmatrix3(){return lstX0Y1;}
  public list<string> getmatrix4(){return lstX1Y1;}
   
 
  public void navigate()
  {
  BatchApexMatrixBulk.BatchApexMethod();
  
  } 
  

  global database.Querylocator start(Database.batchableContext bc)
 {
   return Database.getQueryLocator([select id,Total_Sales__c,HP_SoW__c,MSA__c from Account]);
 }

 global void execute(Database.BatchableContext BC, List<SObject> scope)
 {
  Configuration_Parameter__c conpar=[select Plot_MSA_TAM__c,Plot_SoW__c from Configuration_Parameter__c limit 1];
  Map<String, List<Account>> mapMSAAcc = new Map<String, List<Account>>();
    

  for(sObject s : scope){
        Account A = (Account)s;
        if(mapMSAAcc.get(A.MSA__c) == null){
            mapMSAAcc.put(A.MSA__c, new List<Account>());
        }
        mapMSAAcc.get(A.MSA__c).add(A);
    }
  
  if(mapMSAAcc.size() > 0){
      for(String MSA : mapMSAAcc.keySet()){
          String mapcolor= '';        
          Double totTam=0;
          Double avgSoW=0;
          List<Account> msaaccs=[select id,Total_Sales__c,HP_SoW__c,MSA__c from Account where MSA__c=:MSA];
          for(Account A : msaaccs){
                   totTam = totTam + A.Total_Sales__c;
                   avgSoW = avgSoW + A.HP_SoW__c;
                    
                   }
          avgSoW = avgSoW / msaaccs.size();
          if(totTam>conpar.Plot_MSA_TAM__c && avgSoW <= conpar.Plot_SoW__c)
          {lstX0Y0.add(MSA);}
          else if(totTam>conpar.Plot_MSA_TAM__c && avgSoW > conpar.Plot_SoW__c)
          {lstX1Y0.add(MSA);}
          else if(totTam<=conpar.Plot_MSA_TAM__c && avgSoW <= conpar.Plot_SoW__c)
          {lstX0Y1.add(MSA);}
          else if(totTam<=conpar.Plot_MSA_TAM__c && avgSoW > conpar.Plot_SoW__c)
          {lstX1Y1.add(MSA);}
          
          for( string s: lstX1Y1)
          { system.debug(s);if(s.compareto(MSA)==0){ mapcolor = 'green' ; }}
          for (string t: lstX0Y1)
          { if(t.compareto(MSA)==0){ mapcolor = 'brown' ; }}
           for (string u: lstX1Y0)
          { if(u.compareto(MSA)==0){ mapcolor = 'blue' ; }}
           for (string v: lstX0Y0)
          { if(v.compareto(MSA)==0){ mapcolor = 'red' ; }}
          
          
          Gmap_marker = Gmap_marker +'&markers=color:'+ mapcolor+ '|' +MSA ;
        
        }
    } 
 }


 global void finish(Database.BatchableContext BC)
 {
 }

 webservice static void BatchApexMethod()
 {
  
  BatchApexMatrixBulk accBulk = new BatchApexMatrixBulk();
  ID accprocessid = Database.executeBatch(accBulk);
  
 }
  
 
}
gautamgautam

Values from batch apex cant be passed back to page.It has to be stored in some object and then call back in page.

Need to keep a refresh button on Page to see the latest values.

This was selected as the best answer