5

I am new person in salesforce. Can any one please detail explanation of how to create custom Button in either standard object or custom object using Apex code?

  • Can you please elaborate what exactly you want? Thanks. – Tushar Kumawat Jul 16 '13 at 6:05
  • All i need is to add a custom button to a standard object using another apex page without using the objects layout or adding weblinks in the object , is it possible or is there any other approach – Vimal Balan Jul 16 '13 at 6:11
  • @TusharKumawat Please add as comment.Could you please delete the answer you gave – Mohith Shrivastava Jul 16 '13 at 6:22
  • @TusharKumawat : then what happens if salesforce has updated its standard object , will the custom buttons added thru layout will be there or else it will be deleted – Vimal Balan Jul 16 '13 at 6:29
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. – Samuel De Rycke Jul 16 '13 at 7:31
10

General Steps

The general steps are as follows for either Detail page button or a List View button. It's 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.

Follow these steps and review the links for more details...

  1. Create a Visualforce page, using the 'standardController' and 'extensions' attributes on apex:page *
  2. Create a Custom Button using the Visualforce page as its Content Source
  3. Add the Custom Button to the appropriate Layout of the object
  4. Use either the 'action' attribute or apex:commandButton's on your page to invoke Apex logic.

*** You must also use the 'recordSetVar' attribute on apex:page if you wish to create List View button.

Use of action attribte on apex:page. If you use the 'action' attribute as per step 4 your Apex code will execute as soon as the Custom Button is pressed. However if your Apex code performs database updates this is considered unsecured as its possible that your code will be open to a CSRF attack. See this excellent topic from Salesforce for more information. If this is your case its better to use the apex:commandButton option and provide a confirmation button to your user before invoking your Apex code.

Example: Detail Button

Example page and class using apex:commandButton to invoke the logic.

<apex:page standardController="Test__c" extensions="DetailButtonController">
    <apex:form >
        <apex:commandButton value="Do something" action="{!doSomething}"/>
    </apex:form>
</apex:page>

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.

<apex:page standardController="Test__c" extensions="DetailButtonController" action="{!doSomething}">

When adding the Custom Button the page should look like this...

enter image description here

Example: List View Button

Example page and class, using apex:commandButton to invoke the logic.

<apex:page standardController="Test__c" extensions="ListButtonController" recordSetVar="TestRecords">
    <apex:form >
        <apex:commandButton value="Do something" action="{!doSomething}"/>
    </apex:form>
</apex:page>

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.

<apex:page standardController="Test__c" extensions="ListButtonController" action="{!doSomething}" recordSetVar="TestRecords">

When adding the Custom Button the page should look like this...

enter image description here

Hope this helps!

4

This needs Metadata api to create a custom button in detail page layout. Here is the working code-

    MetadataService.WebLink webLink = new MetadataService.WebLink();
    webLink.fullName = 'Contact.googleButton';
    webLink.availability = 'online';
    webLink.displayType = 'button';
    webLink.encodingKey = 'UTF-8';
    webLink.hasMenubar = false;
    webLink.hasScrollbars = true;
    webLink.hasToolbar = false;
    webLink.height = 600;
    webLink.isResizable = true;
    webLink.linkType = 'url';
    webLink.masterLabel = 'google test meta';
    webLink.openType = 'newWindow';
    webLink.position = 'none';
    webLink.protected_x = false;
    webLink.showsLocation = false;
    webLink.showsStatus = false;
    webLink.url = 'http://www.google.com';
    webLink.width = 600;
    service.createMetadata(new List<MetadataService.Metadata> { webLink })[0];

    // Add the Custom Button to the Layout
    MetadataService.Layout layout1 = (MetadataService.Layout) service.readMetadata('Layout', new String[] { 'Contact-Contact Layout' }).getRecords()[0];
    if(layout1.customButtons==null)
        layout1.customButtons = new List<String>();
    layout1.customButtons.add('googleButton');

    // Update the Layout
    service.updateMetadata(new MetadataService.Metadata[] { layout1 })[0];
0

If you want to add your button on list view of the object or the detail page of any record, then you need to use layout only. You can't directly modify these standard layouts using apex code.

Even if salesforce update the standard object, it won't affect your custom button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.