This post is about creating a custom button to be placed in a detail page using Visualforce page instead of an S-control (deprecated). I will be using Opportunity object for this example.
The Controller:
publicclass VFController
{privatefinal ApexPages.StandardController theController;public List<Opportunity> listOpps =new List<Opportunity>();public VFController(ApexPages.StandardController stdController){
theController = stdController;}// Code we will invoke on page load.public PageReference autoRun(){for(Opportunity opp:[select id, name from Opportunity where id =:theController.getId()]){
opp.Name= opp.name+' test';
listOpps.add(opp);}
Database.update(listOpps);return theController.view().setRedirect(true);}}
The Visualforce Page:
You don’t need to do anything special here. The most important part here is the parameter action = “{!autoRun}”.
<apex:pagestandardController="Opportunity"extensions="VFController"action="{!autoRun}"><apex:sectionHeadertitle="Invoking Apex from a Button"/><apex:outputPanel>
You shoudn't see this page...
</apex:outputPanel></apex:page>
After that, go to Setup –> App Setup –> Customize –> Opportunities –> Buttons and Links. Add a new custom button with the following configuration.
This example simply appends “test” to the Opportunity name when you click on that custom button. This is only to show you that it works. Hope this helps.
The first time I heard this song I already liked it. Originally, it’s a Gilmore Girls soundtrack. Check the YouTube video. I hope you guys like this too.
Lyrics:
How to Dream by Sam Phillips
Ah… Ah… Ah Ahhh Ah Ahhh
When we open our eyes and dream
We open our eyes
Ah… Ah… Ah Ahhh Ah Ahhh
When we open our eyes and dream
We open our eyes