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:
- Obtain URL of a s-control
- Obtain URL of a static resource
- Obtain URL of an object's action
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.
- target: You can replace target with a URL or action, s-control or static resource.
- id: This is id of the object or resource name (string type) in support of the provided target.
- inputs: Any additional URL parameters you need to pass you can use this parameter.
you will to put the URL parameters in brackets and separate them with commas
ex: [param1="value1", param2="value2"] - no override: A Boolean value which defaults to false, it applies to targets for standard Salesforce pages. Replace "no override" with "true" when you want to display a standard Salesforce page regardless of whether you have defined an override for it elsewhere.
Obtaining URL of a s-control:
<!-- 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>
<!-- 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:
- View: Shows the detail page of an object
- Edit: Shows the object in Edit mode
- Delete: URL for deleting an object
- New: URL to create a new record of an object
- Tab: URL to the home page of an object
<!-- 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>
How are you able to use URLFOR to reference an image embedded into a zip file? Can you please explain - I can't make it work.
ReplyDeleteHello
ReplyDeleteCan i open a url of a new form and put some info the fields (ie account name ='XX')?
it's possible ?
Thanks
is there any way i could check if the object is null and perform a different action so that i dont get the error "Invalid parameter for function URLFOR"
ReplyDeleteThanks for this, just what I was looking for after exhausting the resources you mention.
ReplyDeleteNice work buddy:) Comprehensive and concise.
ReplyDeleteme too.. i cant use the zip files with this code.. and i never make it work
ReplyDeleteThis is good help, Salesforce docs for URL for really needs to get upto the mark.
ReplyDeleteRE:How are you able to use URLFOR to reference an image embedded into a zip file? Can you please explain - I can't make it work.
ReplyDeleteI am able to solve this. The format of the image URL should be StaticResourceName/ZipFileName/ImageName. I got confused too assuming that that StaticResourceName and ZipFileName are same..
Sam, I've used your page so many times in the last two years, I feel like I owe you royalty fees. Thanks for your post and examples.
ReplyDeleteKalyan, it'd look something like this:
ReplyDelete{!URLFOR($Resource.StaticResourceName, 'imagename')}
Similar to how the jQuery library is used:
{!URLFOR($Resource.jquery, 'js/jquery-1.6.2.min.js')}
Hi Sam,
ReplyDeleteIs it possible to use the URLFOR function to view the parentAccount from an account record. Below is my scenario. But it is showing error "Invalid parameter for function URLFOR".
My scenario:
URLFOR($Action.Account.View,a.parentId)
Please help me.
Thanks in advance.
Thanks
ashii
Thanx for this
ReplyDeleteExcellent article! Thanks!
ReplyDeleteThis is the best article I\ve read on this topic. Thanks a lot for writing it. I've tried to implement this to override the New opportunity button, so the stage name is set to New by default.
ReplyDelete{!URLFOR( $Action.Opportunity.New , null, [
Opportunity.StageName='New'
],
true)
}
I tried it with the amount field as well and it's still not working
{!URLFOR($Action.Opportunity.New,null, [ Opportunity.Amount =5000] ,false)}"
I'm sure it's obvious, but where am I going wrong?
Try
Delete{!URLFOR($Action.Opportunity.New,null, [Amount ="5000"])}"
thank q BOSS i really get help with your blog
ReplyDeleteVery useful post. Looks like Salesforce has better documentation on this topic now.
ReplyDeleteI have a Vf page where i have a link when I click on that I am navigating to account detail page.
ReplyDeleteMy requirement is to navigate and scroll to product relatedlist .
How to achieve this.??
I used "More"
Any idea how to get that
I am getting following error from the CONSOLE for detail page buttons which are calling a VF page with URL as a content source. The VF page is using a opportunity std controller and another extension. The URL is build using URLFOR
ReplyDeleteError: Id value is not valid for the Opportunity standard controller
Button URL: {!URLFOR($Site.Prefix+"/apex/CreateOrderorQuote?Id=")}{!Opportunity.Id}&calledFrom=ConvertToSalesOrder
For some reason the ID value is getting blank value
Please let me know if there is anything wrong with the URL or why this error may be occuring
Thanks
Is there a way to check for and handle null values for the target or id?
ReplyDeleteThough it an old thread. But can anybody answer chris anatalio question ? I am facing the same issue
ReplyDeletehow to assign id in the url
ReplyDeleteThank you Sam - very helpful.
ReplyDeleteThank you Sam!! Really helpful notes !!
ReplyDeleteIs there any way to Call an Object in Create Mode and fill some of the value using URLFOR?
Thanks, Ramani
Yes there is however SFDC has made it a bit difficult.
DeleteFor custom fields you need to use the field ID as parameter name.
Thanx a lot.... You Rock!!!
ReplyDeleteI think this is no longer supported in Lightning. can anyone confirm?
ReplyDelete