+ Start a Discussion
_vv__vv_ 

Why is URLENCODE not allowed in a formula field?

I have a formula field where I create a link using HYPERLINK. I want one of the parameters in the url to be encoded. So I tried to use URLENCODE. But I get an error that URLENCODE is not allowed in a formula field.

 

My formula (field type is Formula (Text)):

 

HYPERLINK('http://foo.com?bar=' + URLENCODE(Bar__c))

 

Error:

 

Function URLENCODE may not be used in this type of formula (Related field: Formula)

 

It seems like a perfect fit to use URLENCODE to encode parameters for use in HYPERLINK. Am I missing something?

 

I can use SUBSTITUTE and do the encoding myself, but I am trying to see if there is a better way.

 

Thanks,

VV

Best Answer chosen by Admin (Salesforce Developers) 
Ashish_SFDCAshish_SFDC

Hi VV, 

 

In that case SUBSTITUTE should work. 

Please post this request on Ideas exchange for this feature be considered for future releases. 

https://success.salesforce.com/ideaPost 

 

Regards,

Ashish

All Answers

Ashish_SFDCAshish_SFDC

Hi VV, 

 

Please try {!URLENCODE(Bar__c)} , refer to the H&T link below for further information, 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_functions.htm

 

Regards,

Ashish, Salesforce.com

If this post answers your question, please mark this post as Solved.

 

Ashish_SFDCAshish_SFDC

Hi VV, 

 

Please note: This function is only available in custom buttons and links.

Hence it cannot be used in a formula field, rather try creating a custom link under the path below, 

Setup | Customize | Object | Buttons, Links and Actions| New Button or Link | 

See the link below for further details on the "URLENCODE" function. 

http://help.salesforce.com/HTViewHelpDoc?id=customize_functions_i_z.htm

 

Regards,
Ashish, Salesforce.com
If this post answers your question, please mark this post as Solved.

_vv__vv_

I am using a formula field and not a custom link/button because I want this link to be available for use in reports. As far as I can see, a custom link/button is not available when creating a report.

 

Thanks for clarifying that it cannot be used in a formula field. I am just wondering why not. I suppose I will have to stick with SUBSTITUTE and roll my own version of url encoder.

 

Thanks,

VV

Ashish_SFDCAshish_SFDC

Hi VV, 

 

In that case SUBSTITUTE should work. 

Please post this request on Ideas exchange for this feature be considered for future releases. 

https://success.salesforce.com/ideaPost 

 

Regards,

Ashish

This was selected as the best answer
Vinita_SFDCVinita_SFDC

Hello,

 

Unfortunately this is as per design, would suggest you to post this idea to idea exchange portal if you wish to have such feature in near future release: https://success.salesforce.com/ideaSearch

 

At present make use of Hyperlink function by providing direct link, or if the link is the combination of two fields then refer that fields combination in the Hyperlink function.

 

One work around is to encode the value of the field through apex code and refer that encoded field in the Hyperlink formula.

 

 

_vv__vv_

I tried adding this as a suggestion in Ideas, but I encountered an error.

 

We've encountered an intermittent problem. Please click back on your browser and try again. If you continue to encounter the problem, please log a case in your Salesforce org to help us solve the issue.

 


I then tried to access the page to open a case - Help > Contact Support > Open a Case. But that page kept redirecting me to https://help.salesforce.com/hthome?err=1. Huh?

 

Anyway, here is the content I tried to add in Ideas. Can you please add it for me?

 

Title:

Allow URLENCODE function in formula field

 

Description:

HYPERLINK function is allowed in a formula field to construct a link. But URLENCODE is not allowed. It would be nice if it is allowed so that parameters for the url being constructed can be encoded. For now, I am working around this by using SUBSTITUTE and encoding the offending character.


Ideal Usage:
HYPERLINK('http://foo.bar?param1=' + URLENCODE(BAR__c))

 

Work around:
HYPERLINK('http://foo.bar?param1=' + SUBSTITUTE(BAR__c, '+', '%2B')

 

Assume that the value of BAR__c is 'hello+world'. If not encoded, + ends up being treated as a space.

The original discussion on this topic can be found here: http://boards.developerforce.com/t5/General-Development/Why-is-URLENCODE-not-allowed-in-a-formula-field/td-p/645803



Thanks,

VV

Ashish_SFDCAshish_SFDC

Hi VV, 

 

Thanks for letting us know, 

I have added the idea on your behalf - the description is well drafted (i did not enounter any issue),

Please find the link below and cast your vote. 

https://success.salesforce.com/ideaView?id=08730000000krzlAAA

 

Regards,

Ashish

SFRajSFRaj
Hi,

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.
narsavagepnarsavagep
Substitution that is safer (includes more un-safe URL characters):
 
SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(FieldName__c, '%','%25'), '+','%2B'), '&','%26'), '#','%23'), '?','%3F')