Showing posts with label PageReference. Show all posts
Showing posts with label PageReference. Show all posts

Tuesday, July 19, 2016

Salesforce :: Can not add Visual Force page to Custom button

1. At first create a custom controller (Apex Class) with following contents:


public with sharing class CommonController {

    private ApexPages.StandardController standardController;
    
    public Account account { get; private set; }
    
    public CommonController (ApexPages.StandardController standardController) {
        this.standardController = standardController;
        Id recordId = standardController.getId();
        account = (Account) standardController.getRecord();
    }
    
    public PageReference doSomething() {
        //after some tasks return to Account details view
        return standardController.view();
    }
    
    public PageReference cancel() {
        // return to account details view
        return standardController.view();
    }
    
}


2. Create a "Visualforce Pages" page with following contents:

<apex:page standardController="Account" extensions="CommonController">
    Account_Selected=<b>{!Account.Name}</b><br/>
    <apex:form >
        <apex:commandButton value="Do something in CommonController.doSomething()" action="{!doSomething}"/>
        <apex:commandButton value="Cancel this process & return to details view" action="{!cancel}"/>
    </apex:form>
</apex:page>

3. Go to "setup/Customize/Accounts/Buttons, Links, and Actions" and click on "New button or link"
4. Select "Display Type" as "Detail Page Button"
5. Select "Behavior" as "Display in existing window without sidebar or header"
6. Select "Content Source" as "Visualforce Page"
7. And finally select a controller created before from "Content" dropdown.
8. Now go to "setup/Customize/Accounts/Page Layouts"
9. Edit any of your layout you used to test this case
10. Select "Buttons" panel and drop the button created before in details panel
11. Now go to your account details page and now you can see the button available.