0

I am trying to implement a dynamic SOQL like this:

List<Contact> conList = Database.query(query);
System.debug('ConList size: ' +conList.size());

if(!conList.isEmpty()){
      //Some code
}

I have received this error on my VF page as I am using <apex:repeat> over the list of Contact records:

Collection size 1,500 exceeds maximum size of 1,000.

So I have updated my List collection to a Map like this:

Map<Id, Contact> conMap = new Map<Id, Contact>((List<Contact>)Database.query(query));

I am still getting the same error. Is there a way I can use a Map for this issue?

References

  1. Solution from this thread
  2. Another SFSE thread that proves Map can be exempted from this limitation. And I am still find this issue even after using Map.
Austin Evans
  • 721
  • 22
  • 49
  • 2
    The type you'll use won't matter, if there's over a thousand items in it, you will get this error. If you're page does not do any DML you can use the readonly attribute on the page element to increase the limit to ten thousand; https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_readonly_context_pagelevel.htm – user254875486 Nov 07 '19 at 21:14
  • 1
    @rael_kid I thought the same, until I came across this answer. – Austin Evans Nov 07 '19 at 21:30
  • Did you try the second option there? Taking to a list, then moving to a map with sorted indexes. Just thinking they casting when using dynamic query(Database.query) causes something. – highfive Nov 08 '19 at 06:34
  • That's quite suprising, I did not know that, thanks! One other thing I did once was to create a list of lists, and a nested repeat, works up to a million. I will always run into trouble when I use a Map in a repeat or pageblock table, so I never use that anymore, I always use lists, so I think I still would recommend readOnly first, and list of lists second. – user254875486 Nov 08 '19 at 06:56
  • @AustinEvans I could not resist and did try it out. It works very nicely with the map, I will definitely keep that in mind. – user254875486 Nov 08 '19 at 07:15
  • @rael_kid Great that it works for you. Do you see any issue within my implementation? I am still getting the Collection error even after using a Map. Is there a way I can run my query Database.query(query) such that I won't get this error? – Austin Evans Nov 08 '19 at 15:40
  • @highfive I haven't. I will try that out now. – Austin Evans Nov 08 '19 at 15:44
  • @Austin Can you show your complete controller and page code (or at least the parts related to this)? – user254875486 Nov 11 '19 at 11:02

0 Answers0