+ Start a Discussion
Kelly KKelly K 

Add a custom button to a visualforce page?

Hi All,


I'm new to this (admin moving to code) so I apolgize if this seems like an exceedingly simple question. I've looked in the VF workbook and looked at a few other examples online and they all seemed a little too complex for what I'm trying to do.


I've created a custom button on the Contract object with the following setup:

Label: Edit

Name: Console_Edit

Behavior: Display in existing window without sidebar

Content Source: Visualforce Page

Height (in pixels): 600

Display Type: Detail page Button

Visualforce Page: Console: Account Mini Page Edit Mode


I'm trying to set up some small visualforce pages for one of our service cloud consoles but realized that the standard view, edit, and save buttons try to render the whole detail page within the tiny area of a console component. To go around this I set up two visualforce pages: a mini detail and a mini edit. For my edit button in the console (the custom button I am trying to create) I set it to redirect to the edit page.


My challenge comes in trying to reference to this button in the detail layout. 


<apex:page standardController="Contract">
  <apex:form >
    <apex:pageblock tabStyle="Account" id="pageblock" mode="detail" title="Account Detail">
    <apex:pageBlockButtons >
        <apex:commandButton action="{!Console_Edit}" id="editButton" value="Edit"/>
        <apex:pageBlockSection title="Account Details" columns="1" ShowHeader="false" collapsible="false">
            <apex:outputField style="text-align:left" value="{!Contract.AccountId}"/>
            <apex:outputField style="text-align:left" value="{!Contract.Account.Phone}" />
            <apex:outputField style="text-align:left" value="{!Contract.Account.ParentId}" />
            <apex:outputField style="text-align:left" value="{!Contract.Account.Customer_ID__c}" />
            <apex:outputField style="text-align:left" value="{!Contract.Account.Tax_ID__c}" />


I would assume that the custom button I created would still use the standard Contract conroller since the functionality is designed in the Custom Buttons and Links within salesforce and not a class. Is this not the case? Or is there different way to reference a custom button? How do I go about getting the button I defined on this particular visualforce page?


Best Answer chosen by Admin (Salesforce Developers) 
Kelly KKelly K

I figured this out - for anyone who stumbles upon the same question, this is what worked:



        <apex:commandButton action="{!URLFOR($Action.Contract.FC_Edit, Id)}" id="editButton" value="Edit"/>


All Answers

Kelly KKelly K

I figured this out - for anyone who stumbles upon the same question, this is what worked:



        <apex:commandButton action="{!URLFOR($Action.Contract.FC_Edit, Id)}" id="editButton" value="Edit"/>


This was selected as the best answer

I want to add that if you have your button in a Managed Package, you will need to add your namespace in both places, otherwise you will receive errors


<apex:commandButton action="{!URLFOR($Action.myApp__myObject__c.myApp__MyNewButton, Id)}"  value=" My Managed Package Custom Button "/>

Any ideas on how this would work if the custom button was on the child record?
Kelly KKelly K
I'm not sure I understand your question fully? But I'll try to see if I can add clarity.

For example above, my custom button was defined through Setup | Accounts | buttons, Links, and Actions. I wanted to add this to my visualforce page since it was mostly to be used as a page redirect.

I imagine, if you're looking at a child record (say a child record of an account) and you have your custom button listed there, it would depend if you've overriden the default view page with a visualforce page or not. If you've overriden it with a visualforce page, you should be able to use similar code above to add it to the visualforce page. If you haven't, you should be able to use the account page layout editor to add it to the page.

Now if you're asking more specifically about how you can use <apex:detail> and have the button only show for child records, you would probably need to use some logic in your visualforce page to identify that it's a child record and then render if that condition is met.