0

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?

1 Answer 1

1

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

4
  • I see. What do those square brackets do? I assume I can't use it in my output link. When I remove URLEncode the output doesn't change. Does SF do some encoding of its own?
    – George S.
    Commented Nov 22, 2012 at 14:33
  • Edited it a bit, you might not need this long "rant" I've made after all. Brackets denote array of params you pass to URLFOR and effectively tell the function "build me the &key=value long string from these". See the docs for URLFOR: help.salesforce.com/apex/…
    – eyescream
    Commented Nov 22, 2012 at 14:39
  • I copy-pasted your code and it worked a charm. Though I realised I need to pass the Contact name and not the ID to a Lookup.
    – George S.
    Commented Nov 22, 2012 at 14:43
  • 1
    Pass both! Like I pass CF00ND0000003F2Gk=(record Name) and CF00ND0000003F2Gk_lkid=(record Id). Otherwise if you have more than one contact with same name you'll see the dropdown "please select proper John Doe". If you can provide exact match - by all means do it!
    – eyescream
    Commented Nov 22, 2012 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .