1

I have a List of List of account declared in controller like this:

public List<List<Account>> listofListOfAccount {get; set;}

Now I want to determine size of 1st list item in the List of List from VisualForce rendered.

I am trying do something like this, but this is not working:

<apex:messages rendered="{!listofListOfAccount[0].size>100}" layout="table"></apex:messages> 

Please advice.

Edit: Added VF/Controller code

VF:

<apex:page controller="RendererListofListOfAccount" >
  {! listofListOfAccount.size} 
  <apex:messages rendered="{!AND(listofListOfAccount.size>0, listofListOfAccount[0].size>100)}" layout="table"></apex:messages>
</apex:page>

Controller:

public class RendererListofListOfAccount {
    public List<List<Account>> listofListOfAccount {get; set;}
    public RendererListofListOfAccount() {
        listofListOfAccount = new List<List<Account>>();
        List<Account> acLst = [select id, name from account limit 2];
        listofListOfAccount.add(acLst);
         acLst = [select id, name from account limit 1];
        listofListOfAccount.add(acLst);
    }
}
  • 5
    Can you elaborate on "not working"? Try outputing the value directly into the page text, like {!listofListOfAccount[0].size}, to see what's being compared. Seems likely that listofListOfAccount may not be correctly populated. You might also output {!listofListOfAccount.size} to confirm the top-level list as well. – Jason Clark Apr 12 '18 at 13:16
  • @JasonClark: For {!listofListOfAccount.size} = 0..but {!listofListOfAccount[0].size} results an error: Incorrect parameter type for subscript. Expected Number, received Text Error is in expression '{!listofListOfAccount[0].size}' in component <apex:page> in page rendererlistoflistofaccount – Kamruzzaman Apr 12 '18 at 13:49
  • As my List contains only one record. So, this code is working fine for me: <apex:repeat value="{!listofListOfAccount}" var="listofAccount"> <apex:messages rendered="{!listofAccount.size >= 0}" layout="table"></apex:messages> </apex:repeat> – Kamruzzaman Apr 12 '18 at 14:00
2

Assign your sublist to a variable and then access the size of the new variable.

Controller:

public class testPageController {
    public List<List<String>> listoflistofstrings {get; set;}

    public testPageController() {
        listoflistofstrings = new List<List<String>>();

        for(Integer x = 0; x < 5; x++) {
            listoflistofstrings.add(new List<String>());

            for(Integer y = 0; y < 5; y++) {
                listoflistofstrings[x].add(string.valueOf(y));
            }
        }
    }
}

Page:

<apex:page controller="testPageController">
    <apex:variable value="{!listoflistofstrings[0]}" var="subList"/>
    {!AND(listoflistofstrings.size > 0, subList.size > 3)}
</apex:page>

My best guess as to why it doesn't work the way you did it is because the formula engine doesn't have as robust of a typecasting engine as what is built into the apex:variable component. The formula probably only checks basic types such as string, int, date/time, etc and ends up casting it as a generic object where as the apex:variable component also checks more complex types such as lists and maps and type casts it correctly.

  • Wow! Your suggestion worked! I added this line in VF <apex:variable value="{!listofListOfAccount[0]}" var="listofAc"/> and then compared listofAc.size>100 in rendered. Can you please explain the logic why it was not working with previous logic, if possible. Anyway I am choosing it as best answer.. – Kamruzzaman Apr 12 '18 at 19:44
2

The syntax is correct. As I noted in my comment, a good next step is to add the expression you are trying to debug directly to the page output, like so:

<p>list of List[0] size:  {!listofListOfAccount[0].size}</p>

If the top level list is empty, you will receive an error; you can also check the top level list directly:

<p>list of List size:  {!listofListOfAccount.size}</p>

Since you noted in your comment that this returns 0, the issue is not in your rendered property formula, but in your apex controller; listofListOfAccount is an empty list when the page is rendered. Take a look at where in your controller this is populated, and make sure that the code will be executed. You may also need to add debugging output (system.debug(...)) to your controller to help you determine what's happening there, for example if the list<list<account>> is being populated by a query, it may not be returning anything.

Also: if you are getting errors when the list is empty without the extra output (due to the expression in rendered), you can change the formula to check the list size before checking element 0:

<apex:messages rendered="{!AND(listofListOfAccount>0, listofListOfAccount[0].size>100)}" layout="table"></apex:messages>

This way, if the top-level list is empty, the check of [0] will never happen because AND() is a short-circuit operation (i.e., if the first item is false, it need not check the second item).

  • If I provide only {!listofListOfAccount.size} in code, it results 2. But if add <apex:messages rendered="{!AND(listofListOfAccount.size>0, listofListOfAccount[0].size>100)}" layout="table"></apex:messages> it results an error as Incorrect parameter type for subscript. Expected Number, received Text Error is in expression '{!AND(listofListOfAccount.size>0, listofListOfAccount[0].size>100)}' in component <apex:messages> in page rendererlistoflistofaccount I have added VF/Controller code sample in the question for your reference. – Kamruzzaman Apr 12 '18 at 14:24
  • 1
    @Robi my answer has an example of how to fix the "Incorrect parameter type for subscript" error – gNerb Apr 12 '18 at 17:26

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.