function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DesaiDesai 

Show/Hide a custom button based on the status field value.

Hi ,

I am new to SFDC and i need to know to show or hide a custom button based on the status field value.
I have created a custom button "Test" on custom object "Offer". When staus is New, the test button should be hidden . How can this be acheived.

Regards,
Pallavi
Rohit K SethiRohit K Sethi
Hi Pallavi,

Your button is on Visusalforce Page or  Detail page Button.
JyothsnaJyothsna (Salesforce Developers) 
Hi Pallavi,

You need to create the Custom button as Detail Page Button with Execute JavaScript behavior, and in the onClick section add the logic to change the status to New. Below is the code segment;
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

// identify the record
var yourObjectName = new sforce.SObject("Name of your Object");
yourObjectName.id = "{!yourObject.Id}";

yourObjectName.Status__c = 'New';

// save the change
sforce.connection.update([yourObjectName]);

//refresh the page
window.location.reload();

As I know, there is no way to inactive or grayed out the custom buttons in page layouts. Hence, you need to create two page layouts here, 

Page layout 1 – for New and Modified statuses à You can insert your custom button in this page layout
Page layout 2 – for Submitted and Synced status à you are not going to add your custom in this page layout 

Then create two Record Types for respective Page Layouts as you need to switch between page layouts.

Record Type 1 à for Page Layout 1
Record Type 2 à for Page Layout 2 

Finally, create a workflow rule to change the Record Type. Because to click the button you should be in Page Layout 1, after clicking the button, you should be switched to Page Layout 2.
 

Another Way:

1. Please create a formula field and keep return type a text.
2. Now you can write your logic to show a button image if the status is New otherwise put NULL so that nothing would be visible. Or you can show an image of grayed button. 
3. you can embed an URL on clicking on this formula field so that a new VF page can be hit you can update status and it will get back you on the same page. 
Here is sample code
 
IF((your condition logic) ,HYPERLINK("/apex/yournewVFpage?id="&Id,IMAGE("/resource/Buttonimage", "ButtonName"),"_self"),NULL)

Hope this helps you!
Best Regards,
Jyothsna
DesaiDesai
Hi Rohit,

Its a detail page button.

Regards,
Pallavi
DesaiDesai
Hi Jyothsna,

i have added a custom button and written the logic to change the stauts. I need to know how to hide the button based on the status value. And i tried created a formula field but picklist fields are not accepted in the condition.

Regards,
Pallavi
Vamshi KrishnaVamshi Krishna
Hi Jyothsna,
The idea of image formula field works good but I caanot view the image in Communities. I stored the image as a static resource.
Access:Public
Any solution?
Jagannathan MJagannathan M
Create a Boolean variable on the apex class as 'public boolean hidebutton {get;set;}'. Make the hidebutton as true when the status condition is met on the 'Constructor'. Use 'rendered' condition on the custom button based boolean value. (ie) rendered="{! !hidebutton }"
Vamshi KrishnaVamshi Krishna
Hi Jyothsna,
I tried creating it as a document and Externally Available Image: True. I have a folder with images visible to everyone.It worked. I can see it in communities.Let me know If this helped.Thank you