Friday, January 13, 2012

Hide/Show and place a custom button anywhere on a standard layout!


Problem:
I want to create a custom button that calls a URL/VisualForce page but I don’t want to place the button on the top of the page. Plus I also want to be able to hide/display the button only based on a certain field criteria of the record.

Solution:

Ingredients:
  1.   An image of the button.
  2.   One Formula field.
  3.  Your VisualForce page.


Steps:
  • Instead of creating a custom button through the custom button section create an image (png, jpeg..etc) of the button.
  • Upload the button image to the Static Resources section.
  • Create a formula field of type TEXT as such:

IF (put_your_condition_here, HYPERLINK("apex/put_your_visualforce_page_here", IMAGE("/resource/put_your_button_image_name_here", "Button Label name"),"_self"), NULL)

Now you can place the formula field anywhere on a standard page and hide and display the button image based on your formula field condition.

Feel free to post variations of this solution on this site.

Enjoy!

11 comments:

  1. Hi Vardhan,

    Can we place the Formula field in the Button section (Top of the page) in the page layout.


    -Rakesh B

    ReplyDelete
    Replies
    1. No, Forumula fields cannot be placed in the Standard/Custom button sections..

      Delete
  2. a bit confused. we're using a standard page layout for assets. Can we dynamically display any button based on a field criteria without using any visualforce page?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. No, you need a visualforce page to perform your business logic. i.e. what needs to happen when the button is clicked. The formula field will reference that VisualForce page. The idea is that you don't have to recreate the whole page and you have to write custom code only for the business logic that you need to perform. Hope that helps..

      Delete
    3. Hi Gupta,

      Littile bit confustion started i need to place one custom button on my standard layout based on field changing i need to hide and display . but my problem is I have seen this Blog and it is good
      http://www.tehnrd.com/show-and-hide-buttons-on-page-layouts/ but while executing that code i am getting some script errors like: sforce is not defined. so again i have search for it and then finally came to your post..
      i didnt understand how can we place visualforce in custom button section..
      Please help me ..

      Delete
    4. Hi Veeru:

      If you already have the VF page ready that is calling an Apex Controller, then all you have to do is create a formula field as shown here.

      IF (put_your_condition_here, HYPERLINK("apex/put_your_visualforce_page_here", IMAGE("/resource/put_your_button_image_name_here", "Button Label name"),"_self"), NULL)

      Delete
  3. What if I only want to show a simple text input field when a checkbox gets checked. Is there a way to do this without visualforcr?

    ReplyDelete
    Replies
    1. No. Unfortunately to hide/show an input textfield, you will have to create a custom visualforce page.

      Delete
  4. Hi

    Vardhan Gupta,

    I Have created as you mentioned,i con't move it to button please tell how to move this as button in standard layout

    ReplyDelete