1

I know we cannot display more than 50000 records(or probably even much higher) in a Visualforce Page without avoiding viewstate. From an external webservice I am getting more than 50000 records and my senior manager tells me to display these records on the page.

My answer to this is its not possible but he wont listen.

4
  • 1
    you can try using remoting, and query them chunk by chunk – kurunve Feb 1 '17 at 13:04
  • 1
    at single page you cannot show 50000 records it will definitively hit VIEW STATE error. so show in format of pagination. – cloudZigZag Feb 1 '17 at 13:07
  • @kurunve, can u pls elaborate? I am aware of remote action, but didnt understand clearly the phrase chunk by chunk. In addition what else can be done without hitting viewstate? – Krishna Casukhela Feb 1 '17 at 13:08
  • 1
    You only have view state if your page contains an <apex:form> tag. – Adrian Larson Feb 1 '17 at 15:23
4

You can either use client-side or server-side pagination. Either way, you won't be able to use standard tools like StandardSetController or OFFSET.

Server-side, you need to use the @ReadOnly annotation to enable the 1,000,000 row limit, which might look like this:

@RemoteAction @ReadOnly public static SObject[] getRecords(Integer page) {
SObject[] results = new SObject[0];
for(SObject[] records: [SELECT ... FROM SObject WHERE ... LIMIT :page*200]) {
    results = records;
}
return results;
}

From there, you just repeatedly call the value until you get all of your results:

var buffer = []
function getPage(i) {
myController.getPage(i, function(event, data) {
     if(data.length) {
         data.forEach(function(v) {buffer.push(v)})
         getPage(i+1)
     }
}
}

Client-side, it'd look more like this example from the docs:

var result = sforce.connection.query("select id, name from account"); 
var it = new sforce.QueryResultIterator(result); 
var buffer = [];
while (it.hasNext()) { 
var account = it.next(); 
buffer.push(account);
} 
1
  • in server side the list variable "results" cannot hold more than 50K records. then how the loop is running? – Krishna Casukhela Feb 1 '17 at 13:28
0

I believe you just want them to be shown on VF page.

So you can use javascript remoting. JS remoting will call your websrrvice. You will get the JSON response.

On your VF page, parse the JSON response and display these in form of tables using Javascript.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting.htm

JS remoting doesnt consume ViewState. And once you get data in json onto browser you can create own tables and display in format you want.

Also if it is for Viewing purpose, its advised to not insert those records onto your database.

Note: You cannot insert more than 10000 records in a transaction.

1
  • cud u pls tell me how to call webservice thru javascript remoting,shud we use apex class to call webservice or call it in vf page?pls elaborate – Krishna Casukhela Feb 2 '17 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.