Friday, January 27, 2017

Ways to resolve "Collection size xxxx exceeds maximum size of 1000" on VF page

Visualforce pages are not designed to display more than 1000 records in UI. So if you have controller method which returns more than 1000 records and you are displaying the records on UI, then you will receive error "Collection size xxxx exceeds maximum size of 1000"  where xxxx is number of records returned by your controller method.



So now if you have to display more than 1000 records on UI, then you can use below options:

  • Use @ReadOnly annotation on method or on page attribute.

As per Salesforce Documetation:

Normally, queries for a single Visualforce page request may not retrieve more than 50,000 rows. In read-only mode, this limit is relaxed to allow querying up to 1 million rows.

In addition to querying many more rows, the readOnly attribute also increases the maximum number of items in a collection that can be iterated over using components such as <apex:dataTable>, <apex:dataList>, and <apex:repeat>. This limit increased from 1,000 items to 10,000.

  • You can restrict the number of records returned by method and provide pagination option using controller method.

For this you have to write the logic in controller and every time you will send request to server in order to render the page.


You can also jquery datatables and pass the JSON data to it to create table for you and you will get pagination, sorting or filtering of records on client side itself which will be very quick.

So we will pass more than 1000 records as a JSON to VF page and by using jQuery, we can build table.

Below is sample apex class and VF page code:


VF page Output (Displaying more than 50000 records)



If you have to dispaly hyperlink on account name, then modify the account name value before adding it to fieldvalues list at line number 19 in apex class as shown below:
sb.name= '<a href=\'/' + sb.id + '\'  target=\'_blank\'>' + sb.name+ '</a>';



This will display hyper link to account record. In my example code, I am adding new records to list so id are not present. If you are using query to add records in account list, then you can use above code snippet to show hyperlink.



6 comments:

  1. wow really nice. It will be helpful for the people those who are ready to crack the interview and please also for remind what they have learned throughout concept.

    HR Consultancy in Chennai

    ReplyDelete
  2. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

    Digital Marketing Company in India

    ReplyDelete
  3. well and nice tips about uk and their culture. it really good for everything should know before to reach a london. keep update more updates about uk culture. thanks.
    SAT Training Centre in Chennai

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Well, and how you get all this data from an object?
    I see you are creating data in the for loop.

    Can you please give some light to this?

    ReplyDelete
    Replies
    1. Hi Yelmo,

      I have created account in for loop and stored in list.

      You can just query account data using SOQL and store it in list and then can use it.

      Thanks,

      Delete