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
Suresh_SFDCSuresh_SFDC 

How to hide/show Cutom button on opportunity detail page

Hi,
I have a requirement like hide/show custom button based on opportunity stagename.
1) Opportunity stagename is "Closed won" i want to enable cutom button while clicking this button it will redirect to vf page.
2)Not "Closed Won" hide the custom button like stand functionality

Any one suggest using on click java script
Best Answer chosen by Suresh_SFDC
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Suresh,

you can not hide the javascript button on the fly on standard page. 

these are only possible solutions
1) go for record type approach but that will happen at the time of record save 
2) keep some validations inside custom buttons and throw some error or warning message when user clicks on that button (if criteria is not matching or user is not authorized)

if you want this happen on the fly then you need to go for VF page where you can use <apex:detail and hide the buttons as required i guess.

One place i have seen possible is 
http://salesforce.stackexchange.com/questions/72111/can-i-hide-a-custom-button-on-standard-page-based-on-some-field-value 
but this also we can keep button as a field but not in the place of buttons sections

take a call as per your requirement 

let me know, if it helps you or need any help

shiva.sfdc.backup@gmail.com


 

All Answers

RAM AnisettiRAM Anisetti
Hi,

follow the below steps...

step 1)
Create two recordtypes of ur object,names like DisableButton ,EnableButton

for EnableButton,assign ur button to pagelayout 
for DisableButton,dont assign button to pagelayout 

step 2)
write a trigger like below
 
trigger TestCodetrigger on Opportunity (before insert, before update) {


RecordType rt = [select Id,name from RecordType where Name ='EnableButton' and SobjectType ='Opportunity'];
RecordType rt1 = [select Id,name from RecordType where Name ='DisableButton' and SobjectType ='Opportunity'];

 for (Opportunity ob : trigger.new)  {

    
 If(ob.Opportunitystage=="Closed won") {
          
                 
          ob.RecordTypeid = rt.id;
         
    }

else{

ob.RecordTypeid = rt1.id;
  
}
  }
}
you can also replace step 2 with workflows..
 
Suresh_SFDCSuresh_SFDC
Hi Ram thanks for your reply.
Is it possible to use on click java script in custom button?
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Suresh,

you can not hide the javascript button on the fly on standard page. 

these are only possible solutions
1) go for record type approach but that will happen at the time of record save 
2) keep some validations inside custom buttons and throw some error or warning message when user clicks on that button (if criteria is not matching or user is not authorized)

if you want this happen on the fly then you need to go for VF page where you can use <apex:detail and hide the buttons as required i guess.

One place i have seen possible is 
http://salesforce.stackexchange.com/questions/72111/can-i-hide-a-custom-button-on-standard-page-based-on-some-field-value 
but this also we can keep button as a field but not in the place of buttons sections

take a call as per your requirement 

let me know, if it helps you or need any help

shiva.sfdc.backup@gmail.com


 
This was selected as the best answer
Suresh_SFDCSuresh_SFDC

Thanks ShivaKrishna, in my org there is no record types to do the first approach.
I will try the second option

Thanks for quick update :)