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
SinbadCSinbadC 

A problem with apostrophe in Custom Javascript button

Hi All,
Can anyone tell me how I can load a string in Javascript when the variable contains an apostrophe. For example  I would like to load oppName with the Opportunity name where the Opportunity name is Bob's Country Bunker Inc. Using     var oppName =  '{!Opportunity.Name}';    fails. Any help would be much appreciated, thanks
Best Answer chosen by SinbadC
SinbadCSinbadC
Hi NishB,
Doh! Just found out that if I enclose {!Opportunity.Name} with double quotes instead of single quotes I can read in the string.
Best Regards

All Answers

NishBNishB
Hi,
    Can you post the code? I tried alerting a javascript var which was assigned a field value having a apostrophe and it worked
SinbadCSinbadC
Hi NishB,

The following code fails:
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}
var s = '{!Opportunity.Name}';

It generates an alert box with the message "A problem with the OnClick JavaScript for this button or link was encountered: Unexpected identifier" if run in Chrome and the error "A problem with the OnClick JavaScript for this button or link was encountered: missing ; before statement" in Firefox.
I have tried using the custom button on several Opportunities and the alert is only generated on Opps that have an apostrophe  in the field.
Best Regards
SinbadCSinbadC
Hi NishB,
Doh! Just found out that if I enclose {!Opportunity.Name} with double quotes instead of single quotes I can read in the string.
Best Regards
This was selected as the best answer
Raja JammulaRaja Jammula
if('{!$UserRole.Name}'=='XYZ' || '{!$UserRole.Name}'=='ABC'){ 
var url = '/a0Y/e?retURL={!Opportunity.Id}&RecordType=01250000000J5bY&ent=01I50000000QcYO&CF00N50000002DH5h={!Opportunity.Account}&CF00N50000002DH5h_lkid={!Opportunity.AccountId}&CF00N50000002DH5v={!Opportunity.Name}&CF00N50000002DH5v_lkid={!Opportunity.Id}&CF00N50000002DH5i_lkid={!$User.Id}&CF00N50000002Dnha={!Opportunity.Primary_Third_Party_Acc__c}& CF00N50000002Dnha_lkid={!Opportunity.Primary_Third_Party_Account_ID__c}&CF00N50000002DX3C={!Opportunity.Primary_Third_Party_Contact__c}&CF00N50000002DX3C_lkid={!Opportunity.Primary_Third_Party_Contact_ID__c}&CF00N50000002DH5i={!$User.FirstName}+{!$User.LastName}&00N50000002DK1t={!TODAY()}'; 
window.open(url,'_self'); 

else{ 
alert('You don\'t have permission to access this button'); 
}

Getting the same error
A problem with the OnClick JavaScript for this button or link was encountered:
 
missing ; before statement

can you help me plz?
Farzana Khan 7Farzana Khan 7
Anyone found solution to this issue?