Tuesday, December 6, 2016

Visualforce - Beyond Basics Series 2 - Use $Action & avoid URL Hacks

What is URL Hacks ?

The technique of reverse engineering standard salesforce URL & using them to build a custom solution.

Problems of using URL Hacks ?

The standard salesforce URL formats & parameters is undocumented. So, it could be changed anytime during standard release or during bug fixes. So, it's better to avoid


How to avoid it ?

Salesforce provides standard ways to access the URL & parameters in Visualforce & Apex. Developers must use them instead of hardcoding the URL's in the code.

Where is the code ?

Instead of using something like '/006/o' to redirect to opportunity tab page, one could use $Action.Opportunity.Tab with the help of URLFOR function to get the URL.

For illustration, i have shown how to get the URL's for create, view, edit delete for an opportunity record
<apex:page standardController="Opportunity">
What would you like to do with this Opportunity - {!Opportunity.Name}
<apex:panelGrid columns="1">
<!-- create -->
<apex:outputLink value="{!URLFOR($Action.Opportunity.New)}">Create New</apex:outputLink>
<!-- view -->
<apex:outputLink value="{!URLFOR($Action.Opportunity.View, Opportunity.Id)}">View</apex:outputLink>
<!-- edit -->
<apex:outputLink value="{!URLFOR($Action.Opportunity.Edit, Opportunity.Id)}">Edit</apex:outputLink>
<!-- delete -->
<apex:outputLink value="{!URLFOR($Action.Opportunity.Delete, Opportunity.Id)}">Delete</apex:outputLink>
</apex:panelGrid>
</apex:page>
view raw gistfile1.txt hosted with ❤ by GitHub

Where can i find more information ?

For more information, you can see the possible action values for all sObjects,
$Action - Salesforce doc


1 comment:

  1. Invalid parameter for function URLFOR
    Error is in expression '{!URLFOR($Action.Opportunity.View, Opportunity.Id)}' in component
    this error occur in this visualforce page.
    what is syntax of URLFOR and how to fatch Opportunity.Id. Which record is view

    ReplyDelete

Thanks for reading my blog !