0

When building links in Visualforce, I typically prefer using the $Action global variable and URLFOR(). e.g.:

<apex:outputLink value="{!URLFOR($Action.Account.View,Account.Id)}" >

As far as I can tell, these actions are specific to an object, and there aren't actions available that work on the generic SObject. There is however a dynamic syntax that can be used. The problem then being that not every object will support that action. You might think that if a record has been viewed that the object will support the view action, but that is unfortunately not the case for some object types:

Field $Action.ListView.View does not exist. Check spelling
Error is in expression '{!URLFOR($Action[rv.Type].View,rv.Id)}' in component in page analysis:recentlyviewed

If a Visualforce page has a collection of Ids that could be from different records, is there an effective way to use the $Action global variable?

Controller

public class RecentlyViewedController{
    public list<RecentlyViewed> getRecentlyViewed(){
        return [SELECT Id, Name, Type FROM RecentlyViewed LIMIT 20];
    }
}

Page

<apex:page controller="RecentlyViewedController">
<apex:pageBlock>

<apex:pageBlockTable data="{!RecentlyViewed}" var="rv">
  <apex:column headerValue="{!$ObjectType.RecentlyViewed.fields.Name.Label}">
    <apex:outputLink value="{!URLFOR($Action[rv.Type].View,rv.Id)}">
      <apex:outputText value="{!rv.Name}" />
    </apex:outputLink>
  </apex:column>
</apex:pageBlockTable>

</apex:pageBlock>
</apex:page>

1 Answer 1

1

You would get object name from the controller and use a single VF component to be dynamic:

For example, you can use the expression {!URLFOR($Action[objectName].New)} in an , with a controller method getObjectName() that provides the name of the sObject.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_globals_action.htm

You must log in to answer this question.

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