Find your content:

Search form

You are here

Proper urlencoding of spaces in formulas syntax

 
Share

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.


Attribution to: eyescream

Possible Suggestion/Solution #1

My testing found with the Account Name test % + & Account {!URLENCODE(Account.Name)} is rendered to test+%25+%2B+%26+Account.

When used in a custom button to create a record linked to the Account, the associated Salesforce lookup field does not get populated.

When Saleforce natively creates this URL it uses %2B for the '+' characters.

To get my custom button to do the same I used

 {!URLENCODE(URLENCODE(Account.Name))}

Attribution to: Damien

Possible Suggestion/Solution #2

You can use Javascript for Encoding the url. Eg.

HYPERLINK("javascript:function encURI(){return encodeURIComponent(window.location);};javascript:window.location='http://site.com?name="+Name+"&saveURL='+encURI()+'&retURL='+encURI()" ,'Click', '_top')

Format the quotes. This should work.


Attribution to: Muthu

Possible Suggestion/Solution #3

I did resolve the issue on my custom link requirement by using the javascript onclick event to redirect to the required URL and then call the javascript replace function to replace the + to %20 something like this window.location = 'URLFOR()'.replace(/+/g,'%20');


Attribution to: Jeys

Possible Suggestion/Solution #4

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.

Work-Around

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")

Attribution to: Benj

Possible Suggestion/Solution #5

I created my own encoder, that's based loosely on salesforce's encoder, in order to conform to GWT's very picky History encoding standard:

public class GWTEncodeUtil {

    public static String urlEncode(String unencodedString)
    {
        String returnVal = null;
        if(unencodedString != null)
        {
            returnVal = '';
            for(Integer i = 0; i < unencodedString.length(); i++)
            {
                String c = charAt(unencodedString, i);
                if('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;,/?:@&=+$-_.!~*();'.contains(c))
                {
                    returnVal += c;
                } else if(' '.equals(c)){
                    returnVal += '%20';
                } else {
                    returnVal += EncodingUtil.urlEncode(c, 'UTF-8');
                }
            }
        }
        return returnVal;
    } 

    private static String charAt(String str, Integer index) {
        if(str == null){
            return null;
        }
        if(str.length() <= 0){
            return str;    
        }
        if(index < 0 || index >= str.length()){
            return null;    
        }
        return str.substring(index, index+1);
    }
}

Attribution to: Ryan Shillington
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4074

My Block Status

My Block Content