How do I call Apex code from a Custom Button?
Detail Page Custom Button Template
The steps are as follows to add either a Detail or a List View button (as illustrated below) to Standard or Custom Object. It’s well worth going through the topics and general reference guides I’ve linked in more detail. I’ve given some examples of my own, but there are also plenty of them in the help topics I’ve linked to if you need more examples.
Steps to Create a Custom Button that runs Apex Code
- Create a Apex Extension Controller class as shown in the examples below.
- Create a Visualforce page, using the ‘standardController‘ and ‘extensions‘ attributes on apex:page
- Create a Custom Button using the Visualforce page as its Content Source
- Add the Custom Button to the appropriate Layout of the object
- Use either the ‘action‘ attribute (see warning below) or apex:commandButton‘s on your page to invoke Apex logic.
Detail Page Custom Button Template
Example page and class using apex:commandButton to invoke the logic.
1
2
3
4
5
| < apex:page standardController = "Test__c" extensions = "DetailButtonController" > < apex:form > < apex:commandButton value = "Do something" action = "{!doSomething}" /> </ apex:form > </ apex:page > |
Apex controller code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| public with sharing class DetailButtonController { private ApexPages.StandardController standardController; public DetailButtonController(ApexPages.StandardController standardController) { this .standardController = standardController; } public PageReference doSomething() { // Apex code for handling record from a Detail page goes here Id recordId = standardController.getId(); Test__c record = (Test__c) standardController.getRecord(); return null ; } } |
Or to have your Apex logic run as soon as the user presses the Custom Button use the action attribute.
1
2
| < apex:page standardController = "Test__c" extensions = "DetailButtonController" action = "{!doSomething}" > |
To add the Custom Button should look something like this…
List View Custom Button Template
Example page and class, using apex:commandButton to invoke the logic.
1
2
3
4
5
6
| < apex:page standardController = "Test__c" extensions = "ListButtonController" recordSetVar = "TestRecords" > < apex:form > < apex:commandButton value = "Do something" action = "{!doSomething}" /> </ apex:form > </ apex:page > |
Apex controller code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| public with sharing class ListButtonController { private ApexPages.StandardSetController standardSetController; public ListButtonController(ApexPages.StandardSetController standardSetController) { this .standardSetController = standardSetController; } public PageReference doSomething() { // Apex code for handling records from a List View goes here List<Test__c> listViewRecords = (List<Test__c>) standardSetController.getRecords(); List<Test__c> selectedListViewRecords = (List<Test__c>) standardSetController.getSelected(); Boolean hasMore = standardSetController.getHasNext(); return null ; } } |
Or to have your Apex logic run as soon as the user presses the Custom Button use the action attribute.
1
2
| < apex:page standardController = "Test__c" extensions = "ListButtonController" action = "{!doSomething}" recordSetVar = "TestRecords" > |
No comments:
Post a Comment