7

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.

  • Content Source : Javascript, window.location.href="/?name={! SUBSTITUTE(Account.Name , "+", " ")}"; Is this what you used? – techtrekker Nov 5 '12 at 22:46
  • @techtrekker Slightly different: var url = "http://url?customer={!SUBSTITUTE(Account.Name,' ','%20')}"; window.open(url, 'Name of ext. system'); Which makes it kinda inconsistent. – eyescream Nov 5 '12 at 22:50
  • True, i got window.location.href="/?name={! SUBSTITUTE(Account.Name , "+", " ")}"; to work – techtrekker Nov 5 '12 at 23:03
  • @Benj I guess you're right, will do. It's been like that for some time, I remember struggling with it 3+ years ago. It just really annoyed me this time ;) – eyescream Nov 8 '12 at 22:12
6

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

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');

0

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);
    }
}
  • Two questions: does Apex not make a charAt() function available? For the encoding function, could you not just check for a space and otherwise return the default? – Mike Chale Nov 26 '12 at 20:32
0

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))}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.