While developing your Visualforce pages you may need to be able to obtain the URL of certain actions, s-controls or your static resources.
I found it personally a challenge since the documentation for "URLFOR" function is not included in "Visualforce Developer Guide" itself and instead included in the general help area of Salesforce.
Generally you can use the "URLFOR" function for three purposes:html
In this article I will demonstrate usages of the three above.
Generally, URLFOR function returns a relative URL for an action, s-control, or a file in a static resource archive in a Visualforce page. Following the syntax of the function:
{!URLFOR(target, id, [inputs], [no override])}
Parameters shown in brackets ([]) are optional.app
Obtaining URL of a s-control:
less
<!-- Use $SControl global veriable to reference your s-control and pass it to the URLFOR function -->
<apex:outputLink value="{!URLFOR($SControl.MySControlName)}">Link to my S-Control</apex:outputLink>
Obtaining URL of a Static Resource
ide
<!-- Use $Resource global veriable to reference your resource file -->
<apex:image url="{!URLFOR($Resource.LogoImg)}" width="50" height="50" />
<!-- If your file is in another ZIP file, then pass the path of the file as id to URLFOR -->
<apex:image url="{!URLFOR($Resource.CorpZip, 'images/logo.gif')}" width="50" height="50" />
Obtaining URLs of an Object's Actions:
In order to get URL of the an object's actions you need to know what actions that object supports. Below are some of the common actions most Objects support:ui
However, each object may support additional actions for example Contactalso supports "Clone" action and Case supports "CloseCase" action.this
<!-- Use $Action global varialble to access the New action reference -->
<apex:outputLink value="{!URLFOR($Action.Account.New)}">New</apex:outputLink>
<br/>
<!-- View action requires the id parameter, a standard controller can be used to obtain the id -->
<apex:outputLink value="{!URLFOR($Action.Account.view, account.id)}">View</apex:outputLink>
<br/>
<!-- Edit action requires the id parameter, id is taken from standard controller in this example -->
<apex:outputLink value="{!URLFOR($Action.Account.Edit, account.id)}">Edit</apex:outputLink>
<br/>
<!-- Delete action requires the id parameter, also a confirm message is added to prevent deleting the record when clicked by mistake -->
<apex:outputLink value="{!URLFOR($Action.Account.delete, account.id)}" onclick="return window.confirm('Are you sure?');">Delete</apex:outputLink>
<br/>
<!-- From all custom buttons, links, s-controls and visualforce pages you can use the following to get the link of the object's homepage -->
<apex:outputLink value="{!URLFOR($Action.Account.Tab, $ObjectType.Account)}">Home</apex:outputLink>