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
tedevangtedevang 

How do you escape an apostrophe in a Merge field when using Javascript button

I have a custom button which populates the account name on a custom activity field using Javascript OnClick. Works great except if the account name has an apostrophe. How would I escape any potential apostrophes from Account.Name in the following javascript?

 

navigateToUrl('00T/e?who_id={!Account.Primary_Contact_Lookup__c}&retURL={!Account.Id}&00N80000004QVPs={!Account.Name}');

Best Answer chosen by Admin (Salesforce Developers) 
werewolfwerewolf

Since you're doing Javascript there, single quotes and double quotes are interchangeable.  So you can do

 

navigateToUrl("00T/e?who_id={!Account.Primary_Cont

 act_Lookup__c}&retURL={!Account.Id}&00N80000004QVP s={!Account.Name}");

 

And that will work, unless the name has double quotes in it, which seems less likely.

All Answers

werewolfwerewolf

Since you're doing Javascript there, single quotes and double quotes are interchangeable.  So you can do

 

navigateToUrl("00T/e?who_id={!Account.Primary_Cont

 act_Lookup__c}&retURL={!Account.Id}&00N80000004QVP s={!Account.Name}");

 

And that will work, unless the name has double quotes in it, which seems less likely.

This was selected as the best answer
NitishMishraNitishMishra

I was also facing the same problem. But finally got the solution. you can use {!JSENCODE(Account.Name)} for general custom button JS code and for URL , u can use urlencode() function which will handle unsafe charcters of javascript.