function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian Dombrowski 10Brian Dombrowski 10 

How to check if string is in an array/list of strings in VisualForce?

This can be done easily using javascript, so am wondering if it's possible with VisualForce.

Here is what I'm trying to do, but doesn't work:
<apex:repeat value="{!account.AttachedContentDocuments}" var="att">
    <apex:outputPanel rendered="{!att.FileType IN ['JPG', 'PNG']}" layout="none">
        <apex:variable var="attachmentCount" value="{!attachmentCount + 1}"/>
    </apex:outputPanel>
</apex:repeat>

One idea is to concat the string array into a string and then use CONTAINS, but that is a little sloppy because there could be false positives.

Concat idea: https://salesforce.stackexchange.com/questions/70094/checking-a-list-if-a-certain-string-exist-in-visualforce-page/70098#70098
 
jigarshahjigarshah
Update the code as follows. Note that CONTAINS() performs a case sensitive comparison. You can use the == operator for exact comparison instead of CONTAINS as well.
<apex:repeat value="{!account.AttachedContentDocuments}" var="att">
    <apex:outputPanel rendered="{!OR(CONTAINS(att.FileType, "JPG"), contains(att.FileType, "PNG"))}" layout="none">
        <apex:variable var="attachmentCount" value="{!attachmentCount + 1}"/>
    </apex:outputPanel>
</apex:repeat>
Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Brian Dombrowski 10Brian Dombrowski 10
@jigarshah thanks for the reply. Do you know of a way to do a comparison over an array though?
jigarshahjigarshah
Brian,

There are 2 ways you could to this.

1. Compare the Attachment FileType values within Apex using a for loop. Once done, store the match in a public Boolean property, say isImage within Apex which can be used to decide if the output panel is rendered or not.
<apex:outputPanel rendered="{!isRendered}" layout="none">
2. Use a Javascript function within to iterate on the AttachedContentDocuments using a for loop and then rerender it from the client side. Refer the following link to understand How to compare Strings in Javascript. (https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript)

Please mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Brian Dombrowski 10Brian Dombrowski 10
Thanks again. Sounds like there is no standard way to do this, but you outlined all of the workaround ways. Thanks!
jigarshahjigarshah
Please mark this thread as solved and answer as the best answer if it has helped address your issue. ⁣ Regards, Jigar Shah
Brian Dombrowski 10Brian Dombrowski 10
I'm unsure if this is solved yet -- does anyone else know if there is a VisualForce way to do array comparisons like this without Apex class?