11

Say i got a list

list<account> mylist = [select name from account];

what is the best way to get first 100 records in this list.

I mean convert mylist to mylist with first 100 records

  • May be worth specifying if your list will always be SOQL based or if you're curious about pure list apex possibilities. – Samuel De Rycke Aug 1 '13 at 12:48
  • Yes my list will be always based on SOQL.I actually need to display a message if my SOQL query returns more than 100 records.So i thought i cannot apply limit on SOQl.But need to get everything and then ,display message and get first 100records – sfdc99999 Aug 1 '13 at 13:31
  • It's not really advised to query without selective criteria or limit clause. Maybe you can abstract your message thing by first doing a separate "select count(id) from account" query and base your message on that. – Samuel De Rycke Aug 1 '13 at 13:35
  • there is a selective criteria..but yeah no limit.may be i can apply a limit of 101 to reduce no.of records coming back..but my message,is something that is displayed along with this 100 records.So even if do count(id) again i need to write soql for getting those records – sfdc99999 Aug 1 '13 at 13:37
12

You could have queried the list that way originally:

account[] mylist = [select id,name from account limit 100];

Or, you could copy them one at a time:

account[] mylist2 = new account[0];
integer counter = 0;
while(counter<100 && counter<mylist.size()) {
    mylist2.add(mylist[counter++]);
}

Update To find out if there are more records than can be displayed at once, set your limit one higher, and check for the presence of the max value:

account[] mylist = [select id,name from account limit 101];
if(mylist.size()==101) {
    mylist.remove(101);
    apexpages.addmessage(new apexpages.message(apexpages.severity.info,'More than 100 results found. Please narrow your search criteria.'));
}
  • In addition: If size() would only be a little over 100, you could also remove some items which may be marginally faster for small numbers(like 100). – Samuel De Rycke Aug 1 '13 at 12:50
  • my list will be always based on SOQL.I actually need to display a message if my SOQL query returns more than 100 records.So i thought i cannot apply limit on SOQl.But need to get everything and then ,display message and get first 100records – sfdc99999 Aug 1 '13 at 13:34
  • 1
    Updated answer to include a way you might check for more than 100 records using LIMIT. – sfdcfox Aug 1 '13 at 13:40
2

You can use limit in SOQL statements:

list<account> mylist = [select name from account limit 100];

Remember to order it so that your list is deterministic.

  • my list will be always based on SOQL.I actually need to display a message if my SOQL query returns more than 100 records.So i thought i cannot apply limit on SOQl.But need to get everything and then ,display message and get first 100records – sfdc99999 Aug 1 '13 at 13:34

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.