Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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>            
share|improve this question
    
Have you tried removing ['name'] since the repeat is already iterating over the values in the tesList –  Eric Mar 24 at 10:53
    
Hmmm i havent but i'll check. –  elL Mar 24 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 Mar 24 at 11:09

1 Answer 1

up vote 1 down vote accepted

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>
share|improve this answer
    
sounds good. i'll give it a try. thanks –  elL Mar 24 at 11:16
    
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 Mar 24 at 11:20
    
it's kinda confusing since the var in variable is also list and also for repeat.. –  elL Mar 24 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 :) –  mast0r Mar 24 at 12:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.