New child object default UI page via URLFOR

I have a custom page from which I want to create new child objects where the child object’s default UI is used. The problem is how to get the parent object’s id and name passed into the child UI to so the parent lookup field is correctly populated.

The only solution I found on http://developer.force.com/ (can’t find the specific post that inspired this though) is to obtain the name attribute of the parent lookup field in the child object’s default UI by inspection (e.g. Chrome’s “Inspect element”). In my example below, this name is “CF00Nt0000000yyVC”. There doesn’t appear to be a way to obtain this programmatically and I am unsure how fragile this value is… Then use that name to pass the displayed name (parentName from the controller) and the corresponding non-displayed id (parentId from the controller) using a magic _lkid suffix like this:

<apex:commandButton
value="New Child"
action="
{!
    URLFOR(
        $Action.ChildType__c.New,
        null,
        [
            CF00Nt0000000yyVC=parentName,
            CF00Nt0000000yyVC_lkid=parentId,
            saveURL=URLFOR($Action.ParentType__c.View, parentId)
        ]
    )
}
"/>

The URL to go to after the save – back to the parent page – is also being passed.

Now this all has the feel of a fragile reverse-engineered solution. But given there is no documented way of accomplishing this what other choice is there?

A couple of other comments on URLFOR:

  • The best documentation for this is via the Help link of an instance of Force.com; the information is missing from the PDF etc.
  • For unknown reasons, it isn’t possible to reference the actions of the standard Task object (and so perhaps other standard objects) using e.g. $Action.Task.View.
Advertisements

3 thoughts on “New child object default UI page via URLFOR

  1. Nice post, URLFOR is a bit of a tricky function as it can be used in slightly different ways, but the documentation on those different usages are in different places (one in the help and the other in the Visualforce Developer’s Guide under ‘Functions’). Something to note is that sometimes you can just use the new objects field name as the key (in the key value pair), but at other times (as you’ve noted above) SF uses wierd ids to identify fields on objects. Inconsistency grrrr… Also the keys are case-sensitive.

  2. Pingback: Lkid hack value is fragile « Force 201 – Force.com Development

  3. Activity is a polymorphic object; Task is one of its shapes. So, you can actually reference it with URLFOR. Just, in their wisdom, Salesforce neglected to document this for us.

    URLFOR($Action.Activity.NewTask)

    URLFOR($Action.Activity.LogCall)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s