0

Please don't mind if it's a simple question. I want to compare two strings but one string(Pates de L'Ile) contains this ' which causes an error. Please check the code below

<apex:outputPanel rendered="{!(oppli.Product2.family =='Pates de L'Ile' && rowNum == 1)}" style="margin-left:10px" >
    <apex:image value="{!$Resource.PatesdeLIle}" width="180" height="100"/>
    <apex:variable var="rowNum" value="{!rowNum + 1}"/>
</apex:outputPanel>

This code oppli.Product2.family =='Pates de L'Ile' throws error

Error: EL Expression Unbalanced: ... {!(oppli.Product2.family =='Pates de L'Ile' &amp;&amp; rowNum == 1)}

So how to compare String if its contain this '

2 Answers 2

2

In programming (in general, not just on the Salesforce platform), when you have a string that contains a character with a special meaning, you need to escape that character to be able to use it. "Escaping" a character tells the underlying system to treat this thing differently than you normally would.

The way you "escape" something is generally to add a backslash \ right before the character you want to escape.

rendered="{!(oppli.Product2.family =='Pates de L'Ile' && rowNum == 1)}"

should be

rendered="{!(oppli.Product2.family =='Pates de L\'Ile' && rowNum == 1)}"

4
  • The code now saves successfully but it does not give me the correct result. I mean image should display when both strings are equal
    – fawad khan
    Oct 6, 2021 at 15:13
  • @fawadkhan You also have the condition that rowNum must be 1 (and both conditions must be satisfied because you're using the AND operator (&&) ). Your use of <apex:variable> seems to be incorrect (it has no initial value), and it appears that you may be using it in an iteration component, a situation that the documentation on <apex:variable> specifically warns against.
    – Derek F
    Oct 6, 2021 at 15:22
  • the value for `` rowNum `` i initialize above <apex:variable value="{!1}" var="rowNum"/> <apex:repeat value="{!Opportunity.opportunityLineItems}" var="oppli"> i used many `` <apex:outputPanel`` to display image based on prouct.family all are working good but only this one is not working
    – fawad khan
    Oct 6, 2021 at 15:29
  • I think this \ is not working on the VF page. Is there is any other way?
    – fawad khan
    Oct 6, 2021 at 16:19
0

So by using backslash \ in a string like Pates de L\'Ile will make string like this Pates de L\'Ile and the value coming from oppli.Product2.family is this Pates de L'Ile so both strings are not equal. To solve this using JSENCODE() which will add backslash \ in a string like

{!(JSENCODE(oppli.Product2.family)    == 'Pates de L\'Ile' && rowNum == 1)}"

Now both strings are equal

You must log in to answer this question.

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