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>

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS