3

How can i check if a list has certain string in salesforce. For example I have : anotherList which is from controller and has this value [NAME_A, NAME_B, NAME_C]

Then in my page:

    <apex:repeat var="list" value="{!tesList}">
        <input type="checkbox" style="display:{!IF(CONTAINS(anotherList, list['name']), '','none')}" value="{!list['name']}" />
    </apex:repeat>

But i got an error saying that Error: Incorrect parameter type for function 'CONTAINS()'. Expected Text, received Object

So parameters should only be Strings right? But how can i do something like this in Visualforce page?

<!-- JSTL sample -->
    <c:forEach var="list" items="${testList}">
            <c:if test="${fn:contains(anotherList, list.name)}">
                <input type="checkbox"  value="${list.name}" />
            </c:if>
    </c:forEach>            
3
  • Have you tried removing ['name'] since the repeat is already iterating over the values in the tesList
    – Eric
    Commented Mar 24, 2015 at 10:53
  • Hmmm i havent but i'll check.
    – elL
    Commented Mar 24, 2015 at 11:05
  • @Eric Nope, it is still looking for text. I guess i have to create and actionFunction if there's no other way
    – elL
    Commented Mar 24, 2015 at 11:09

1 Answer 1

6

There is apparently no support of visualforce text functions for apex lists. While pure output of the list content on the visualforce page works perfect, an attempt to access a list with any text function will produce an error. Lets try to

Apex class:

testList = new List<String>{'NAME_A','NAME_B','NAME_C'};

Visualforce Page:

<!-- The following output works good as if it where a string -->
testList: {!testList}

On the page: testList: [NAME_A, NAME_B, NAME_C]

What if we could to convert the list to string somehow directly on the visualforce page? Let's try to use a magic apex:variable tag and assign a list to it.

But with one trick: we will concatenate it with an empty string:

<apex:variable var="list" value="{!''}{!testList}" />

Now let's try again to use a CONTAINS function that actually works only with strings:

NAME_A: {!IF(CONTAINS(list, 'NAME_A'),'OK','FALSE')} <br/>
BLAAHH: {!IF(CONTAINS(list, 'BLAAHH'),'OK','FALSE')} 

Well, it works just awesome!

NAME_A: OK 
BLAAHH: FALSE 

In your case it could look like:

<apex:variable var="list" value="{!''}{!anotherList}" />

<apex:repeat var="list" value="{!tesList}">
    <input type="checkbox" style="display:{!IF(CONTAINS(anotherList, list['name']), '','none')}" value="{!list['name']}" />
</apex:repeat>
3
  • You will need to be careful with this approach as it is doing string matching rather than matching on elements in a List. For example if your list was new List<String>{'NAME B','NAME ABC','NAME C'}; and you searched for NAME A it would find that string even though the String that was in the List was NAME ABC
    – BarCotter
    Commented Mar 24, 2015 at 11:20
  • it's kinda confusing since the var in variable is also list and also for repeat..
    – elL
    Commented Mar 24, 2015 at 11:28
  • 1
    @BarCotter Yes this is a tricky one and I would better use an apex for such comparison. But it is always interesting to crack out some "undocumented" functionality :) Commented Mar 24, 2015 at 12:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .