[SalesForce] How to specify an Action for an URLFOR in apex

I have the following command link:

<apex:commandLink value="Edit" action="{!URLFOR($Action.Contact.Edit, rec.id)} />

Which works fine. But I'm trying to make the code generic, to work with multiple objects (not just Contact), so I'd like to specify the action from my controller. Based on the docs for ApexPages.Action, I tried the following:

<apex:commandLink value="Edit" action="{!URLFOR(RecordEditAction, rec.id)} />

With this in the controller:

public ApexPages.Action getRecordEditAction() {
    return new ApexPages.Action('{!$Action.Contact.Edit}');
}

As a test, planning to compute the actual string in the final code. But the test above gives me an "Incorrect argument type for function URLFOR()" error when trying to save the VF page. Looks like ApexPages.Actions are intended to be passed directly as an action param, that is, not via URLFOR, but then there's no way to specify to which object id to apply the action.

How can I specify the desired action from Apex? I don't want to use an url hack (e.g., 003/e); other uses may include delete actions, etc.

Best Answer

Per Dynamic References to Action Methods Using $Action in the VF Developers Guide:

The $Action global variable allows you to dynamically reference valid actions on an object type, or on a specific record. The most likely way to make use of this is to create a URL to perform that action. For example, you can use the expression {!URLFOR($Action[objectName].New)} in an <apex:outputLink>, with a controller method getObjectName() that provides the name of the sObject.

Related Topic