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
theitdeptrockstheitdeptrocks 

Escape single quote\apostrophe within a formula

Hi all,

 

I'm using the following case statement to within a VF page to adjust some language.  Within one of the options is an apostrophe and I'm having difficulty escaping it.

 

Here's what I have:

{!case(MyOBJ__c.MyField__c,
'Any time',' at any time.',
'Based on Employer policy',' based on your employer\'s policy.',
'MERGE ERROR')}

 Doing this results in "based on your employer\'s policy" being displayed on the page.

 

Using \\'s results in a nasty looking Error: EL Expression Unbalanced: ...

 

And for grins, using \\\'s results in "based on your employer\\'s policy"

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

  

Hi,

        "JSENCODE" function in visual force page is used to  encodes text and merge field values for use in JavaScript. It inserts escape characters, such as a backslash (\), before unsafe JavaScript characters, such as the apostrophe (').This should solve the issue faced by you. For more information you can refer visual force guide documentaion under the topic "Advanced functions".

 

Syntax: {!JSENCODE(text)} and replace text with the merge field or text string that contains the unsafe JavaScript characters.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

All Answers

alexbalexb

I don't know the answer, but maybe this helps:

 

Check out Wes' discovery and workaround for escaping stuff on VF pages:

http://th3silverlining.com/2010/02/17/visualforce-ids-in-jquery-selectors/

 

Particularly this quote:

"Note 2: We have to escape the escape as the VisualForce parser seems to unescape the first escape :$"

Ispita_NavatarIspita_Navatar

  

Hi,

        "JSENCODE" function in visual force page is used to  encodes text and merge field values for use in JavaScript. It inserts escape characters, such as a backslash (\), before unsafe JavaScript characters, such as the apostrophe (').This should solve the issue faced by you. For more information you can refer visual force guide documentaion under the topic "Advanced functions".

 

Syntax: {!JSENCODE(text)} and replace text with the merge field or text string that contains the unsafe JavaScript characters.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

This was selected as the best answer
theitdeptrockstheitdeptrocks

I can see where it would work, but am currently having a problem with the apostrophe within the JSENCODE function.

 

JSENCODE('employer's')

 

How would I handle that?

bryan.gilbertbryan.gilbert

I came here is a similar question.  Here is a snippet that does not work when the account name contains the single quote (e.g. O'Brians Company)

<apex:outputPanel id="buttonBar" layout="block"> 
  <apex:commandButton value="Create Account" action="{!doCreateAccount}"
    onclick="if(!confirmCreate('{!accountObject.name}')) return false;"
    immediate="true" 
    rerender="block" status="status">
  </apex:commandButton>
</apex:outputPanel>

 

Thanks to the hints above I got it to work with this code:

<apex:outputPanel id="buttonBar" layout="block"> 
  <apex:commandButton value="Create Account" action="{!doCreateAccount}"
    onclick="if(!confirmCreate('{!JSENCODE(accountObject.name)}')) return false;"
    immediate="true" 
    rerender="block" status="status">
  </apex:commandButton>
</apex:outputPanel>