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
ricohusaricohusa 

how to deal with special characters in opportunity name

Hi,
 
I have a question about use special characters in opportunity name:
 
 
I created an opportunity which has double quotation " and the prime ', the name is:
 
 
special"ABC'123/\*
 
 
Now I want to use this name in my S-Control which is using HTML code, actually
I want to remove the speical characters,  to make it as
 
specialABC123
 
But if I put the above string in a Javascript function like this:
 
var str = "{!Opportunity.name}"                      or   var str = '{!Opportunity.name}' 
 
 
Since  {!Opportunity.name} only replaces the whole string directly
without encoded or escaped, the string is like this:
 
"special"ABC'123/\*"                                       or  'special"ABC'123/\*'
    
 
None of them could work properly because either the double quotation " should be
escaped as \" or the prime should be escaped as \' to make the Javasrcipt string
as a valid string.
 
Does anybody know how to use the opportunity name string  as encoded or escaped?
The only way I know how to use it is:
 
{!Opportunity.name}
 
but this does not work properly in the above case.
 
 
 
Thanks.
 
 
 
 
 
 
 
sfdcfoxsfdcfox
There are two ways to deal with this in JavaScript in Salesforce.

Method 1: Use retrieve()

Code:
{!RequireScript("/soap/ajax/12.0/connection.js")}

var Opportunity = sforce.connection.retrieve('Name','Opportunity',['{!Opportunity.Id}'])
// You can now use Opportunity[0].Name for the opportunity name.
// You may retrieve multiple fields using a comma separator, i.e. 'Name,Amount,AccountId'

Method 2: Use Substitute() (repeatedly)

Code:
var OpportunityName = "{!Substitute(Substitute(Opportunity.Name,"\\","\\\\"),"\"","\\\"")}"

This should work for any name, as it escapes the escape character, and also escapes double-quotes. You should escape single quotes if you prefer to mark your strings with single quotes instead.

Honestly, some developer in Salesforce needs to make it so S-Controls automatically escape quotes like my formula above does. And line breaks, too.

KringoKringo
Wow, Thanks sfdcfox!

I've run into this a few times but haven't had a fix. This is great.