[SalesForce] Hash symbol when binding variables in URL

I'm trying to populate the Contact lookup when a User creates a new related list object from the Contact page.

I am storing the Contact parameter in a Custom Setting so it can be changed when deploying to Production.

For some reason, whenever I try to bind this custom setting to the URL I get a leading '#' and none of the bindings work correctly. This is despite using the URLENCODE function in the Visualforce

So when I put this (contactParameterId is != null)

<apex:outputLink value="/{!relList.keyPrefixObject}/e?{!IF(relList.contactParameterId != null, 'CF{!URLENCODE(relList.contactParameterId)}={!URLENCODE(contact.Id)}&', '')}retURL=/{!contact.Id}&saveURL=/{!contact.Id}"></apex:outputLink>

I get the URL

https://na12.salesforce.com/a0R/e?#{URLENCODE(relList.contactParameterId)}={!URLENCODE(contact.Id)}&retURL=/003U000000PL9LNIA1&saveURL=/003U000000PL9LNIA1

What am I doing wrong?

Best Answer

EDIT: You already are in the "merge fields syntax" with the {!IF ... }. There's no need to use { ... } again inside this IF but you have 'CF{!URLENCODE...

So probably something like this will be better:

<apex:outputLink 
    value="/{!relList.keyPrefixObject}/e?{!IF(relList.contactParameterId != null, 'CF' & URLENCODE(relList.contactParameterId) & '=' & URLENCODE(contact.Id),'')}&retURL=/{!contact.Id}&saveURL=/{!contact.Id}">
</apex:outputLink>

(you probably also don't need to URLENCODE anything here, both field Ids and record Ids should be "safe")


I don't think you're doing anything wrong.

Server-side parser gets in my opinion stupid overzealous with URLENCODE'ing too much stuff you'd consider safe. Might be even related to my question Proper urlencoding of spaces in formulas syntax

I have a massive <apex:commandButton /> to redirect user from my overridden VF page to standard edit page while salvaging what I can (prepopulating some fields) if he needs to fall back to original behavior of the page.

<apex:commandButton value="classic 'new' page"
    immediate="true"
    action="{!URLFOR($Action.Purchase_Order_Item__c.New, $ObjectType.Purchase_Order_Item__c, 
        [retURL=URLFOR($Action.Purchase_Order__c.View, po.Id),
        saveURL=URLFOR($Action.Purchase_Order__c.View, po.Id),
        CF00ND0000003F2Gk_lkid=po.Id,
        CF00ND0000003F2Gk=po.Name,
        CF00ND0000003F2Gn_lkid=po.Cost_Code__c,
        CF00ND0000003F2Gn=po.Cost_Code__r.Name,
        CurrencyIsoCode=po.CurrencyIsoCode],
        true) + '&00ND0000003F2Gw=' + po.PO_Type__c + '&00ND0000003F2Gv=' + po.PO_Sub_Type__c}"
    rendered="{!Purchase_Order_Item__c.id = ''}" />

po is the "Purchase Order" header and this button sits on the page we use to create new line items.

As you can see this abomination deals with your "CF" problem outside of URLFOR. Additionally fields with names that begin with 0 will prevent you from saving the VF page so I had to put them outside of the URLFOR too (they're picklists if that's relevant).

Related Topic