[SalesForce] Proper urlencoding of spaces in formulas syntax

1 week ago user has asked me to display a custom link on Account that would point to another system we use. Really straightforward stuff, idea being that user is already logged in to both SF and that system.

URL should look like http://internal/unimportant?customer=ABC%20Telecom%20Ltd.
Custom link, content source "URL", "display in new window", UTF-8 encoding.

  1. Naive attempt: ?customer={!Account.Name} renders ?customer=ABC+Telecom+Ltd.

  2. URLENCODE promises to replace spaces with %20 but {!URLENCODE(Account.Name)} renders ABC%2BTelecom%2BLtd.

  3. {!SUBSTITUTE(Account.Name,' ','%20')} renders ABC%2520Telecom%2520Ltd.

The other system doesn't like plus signs, these really have to be percent-encoded spaces.
A percent-encoded plus sign in the #2 is… wow, just wow. 3rd one is even better.

At that point I gave up on the URL and solved it with 1 line of JavaScript. Any idea why it didn't want to work?

EDIT: Got consistent results in Firefox, Chrome and IE although I don't think it's a case of browser trying to fix it for me.

Best Answer

The behavior for #2 is actually documented, because Salesforce URLENCODES the final output from the custom link, regardless of your specific URLENCODE-ing:

Custom buttons and links with URL content sources have separate encoding settings. If you use the URLENCODE function to encode a custom button or link that has an encoding setting specified, Salesforce first encodes the URL according to the custom button or link setting, then encodes the result. For example, if the URL in a custom link contains a space and its encoding setting is UTF8, Salesforce first encodes the space to a plus sign (+), then the URLENCODE function converts the plus sign to its character code, %2B. -- Operators & Functions Documentation

The same logic explains #3: You replace "" with %25, and then the final encoding replaces the % with %25.


Use a Custom Field instead of a Custom Link. Field Type: Formula (Text):

HYPERLINK("http://example.com/path?customer="&SUBSTITUTE(Name, " ", "%20"), "Link to Customer in Other System")
Related Topic