Passing URL parameters into a Visualforce page from a custom button or link
by Quinton Wall on May 20, 2009 at 10:32 AM
It has been a while since my last post. I have been deeply entrenched in a client project with some pretty complex lead assignment, and scoring logic that has taken all of my time. The good thing is that I learnt a LOT of platform tips and tricks that I intend to share with the developer community. One of these tips is how to call an apex class from a custom button or link.
Although calling an apex class directly is not currently supported, you can mimic this behavior by creating a simple 'pass-through' Visualforce page. using the Content Source picklist from the Edit Custom Link or Button screen, and selecting URL. You can certainly select Visualforce directly as well but you are no longer able to take advantage of the URL FieldType, and Merge Field picklists to pass in url parameters.
In order to call the Visualforce pass-through page class URL from the Content Source picklist, on the Edit Custom Link or Button screen, and enter the following in textfield:
/pages/MyPage?myparam=
From here you can select a Field Type, and Merge field to dynamically add the parameter value you need, and that is all you need to do. From within your apex class The code snippet, and screenshot below shows a sample from the Salesforce Labs simple quote app, available on appexchange, where this functionality is used very intelligently to generate a PDF quote.
Happy coding!
<apex:page standardController="SFDC_520_Quote__c" Extensions="salesQuotes" action="{!attachQuote}">
{!SFDC_520_Quote__c.Opportunity__c}{!SFDC_520_Quote__c.name}
</apex:page>
TrackBack
TrackBack URL for this entry: http://www.typepad.com/services/trackback/6a00d8341cded353ef01157099f55d970b
Listed below are links to weblogs that reference Passing URL parameters into a Visualforce page from a custom button or link:

Comments
Posted by Richard Durrant on May 22, 2009 07:34 AM:
This is a nice way to link VisualForce with standard buttons.
One thing to bear in mind if you are intending to (ultimately) create a managed package is that as you aren't linking to the page directly with a $Page.myPage statement the namespace won't be appended to the url. The url should end up being /apex/myNamespace__myPage?id=xxx but will remain /apex/myPage?id=xxx.
You will just need to remember to prepend manually the 'namespace__' part.
Apart from that it works a treat.
Rich.
Posted by Andy Rouse on November 25, 2009 05:54 AM:
This blog doesn't really explain what the developer is supposed to do:
1) "/pages/MyPage?myparam=" isn't reflected in the image below
2) What is meant by "select a Field Type" and in what context?
3)Also, grammar and punctuation need a tidy-up to clarify the post.
With thanks,
Andy