I need to create custom buttons for all custom object page layouts through code, is it possible ?

  • Welcome to SFSE. What have you tried so far? What did you read? What is your knowledge with APEX code? assuming that's the way you want to go.
    – Saariko
    Oct 17, 2013 at 10:30
  • I am creating an app and add this feature on all the custom objects available on user's org so I need to create detailpage button through code..is it possible???
    – Mahi
    Oct 17, 2013 at 10:39
  • So your actual question is "how to add a custom button to all custom object detail pages in an org". Not something I have done before but it would amaze me if this is that complex. Oct 18, 2013 at 8:28

2 Answers 2


You can use the the following code from the Apex Metadata API to create the buttons via WebLink type.

    // Metadata Weblink (aka Custom Button) for button
    DetailButton = new MetadataService.WebLink();
    DetailButton.fullName = 'MyObject__c.MyButtonName';
    DetailButton.availability = 'online';
    DetailButton.displayType = 'button';
    DetailButton.encodingKey = 'UTF-8';
    DetailButton.linkType = 'page';
    DetailButton.masterLabel = 'My Custom Button';
    DetailButton.openType = 'replace';
    DetailButton.page_x = 'myvfpage';
    DetailButton.protected_x = false;
        new List<MetadataCreateJob.Item> { new MetadataCreateJob.Item(webLink) },
        new MetadataCreateJob.EmailNotificationMetadataAsyncCallback());

Note: The above demo polls the Metadata API (which is an async API) via Batch Apex, however if you are writing a Visualforce page you can use action:poller. Which is how the next part of what you need works as it happens.

You can write code to retrieve, manipulate (via Apex XML classes) and redeploy layouts. Studying and adapt the Retrieve and Deploy demo code to see the mechanics of how to do this.

enter image description here

So calling the Metadata API is easier from Apex with this library. Though one thing that makes the API complex is it is async, regardless of language this adds some handling overhead to you. As you can see i have given a helper Batch Apex wrapper and/or examples of using action:poller to handle this. Once you get your head round this, its pretty easy to use action:poller to create dynamic solutions.

  • Hi Andrew Fawcett, It works thanks.. can you please send me some more code snippet to use javascript in the button and how to put the code for that??
    – Mahi
    Oct 21, 2013 at 4:45
  • Take a look at this, salesforce.com/us/developer/docs/api_meta/Content/… there are some XML examples below, they are not for JavaScript buttons though, however... if you create a JavaScript Custom Button, then use Eclipse to retrieve the Custom Object you can view the XML within and see what to set. This is what I did for the above example. Oct 21, 2013 at 8:44

Custom buttons are not really something you usually create (or modify) through code in salesforce. They are very easy to create by point-and-click configuration.

See: How to create custom buttons and the examples and references to the documentation given there.

While it may theoretically be possible to use the metadata api from Apex, that would be a huge workaround for something simple and basic. (See Andrew's answer)

Alternativly, you can overwrite a detail page with visulforce pages which give you full UI control.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .