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
Karthikeyan ChandranKarthikeyan Chandran 

How to hide related list button?

Hi,

How do i hide a related list standard button 'New Opportunity', when i choose picklist values?

User-added image

User-added image
Here, the related list button should visible only when i choose the values "REQ", "Solution Sale" in the picklist field 'Pre Opportunity Type' and the picklist Approval Status valus is "Approved".

Any possibilities(VF page, Trigger) other than assigning record type to different layout?

Thanks & Regards,
Karthikeyan Chandran
Best Answer chosen by Karthikeyan Chandran
Alexander TsitsuraAlexander Tsitsura
Hi Karthikeyan Chandran

Problem is that account not specified on your record, and instead of {!Account.Id} you have empty. You can add condition as "if ('{!Account.Id}')"

Try this code
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 

if('{!Pre_Opportunity_Stage__c.Pre_Opportunity_Type__c}' == 'RFQ','{!Pre_Opportunity_Stage__c.Pre_Opportunity_Type__c}' == 'Solution Sale'){ 
  if ('{!Account.Id}') {
    window.open('https://cs50.salesforce.com/006/e?retURL=%{!Account.Id}&accid={!Account.Id}');
  } else {
    window.open('https://cs50.salesforce.com/006/e');
  }
} 
else{ 
  alert('You can not create new oppty'); 
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex

 

All Answers

Ketankumar PatelKetankumar Patel
You cannot hide list view button based on parent record conditions but you can create custom list view button (New Opportunity) on opportunity object and hide the standard new button and make this custom list view button available on opportunity related list. 

Apply some sort of JavaScript logic to create related opportunity record or display message.

Below is the JavaScript logic that you need to put in your button and change some of the logic based upon your requirements. 
My JavaScript code is checking if Account industry is Energy then open window to create new opportunity else display popup message "you cannot create new oppty).

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")}

if('{!Account.Industry}' == 'Energy'){
   window.open('https://na9.salesforce.com/006/e?retURL=%{!Account.Id}&accid={!Account.Id}');
}
else{
  alert('You can not create new oppty');
}
Karthikeyan ChandranKarthikeyan Chandran
Thank you Ketankumar Patel for your suggestion.

I just used the below script and i got an error,
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 

if('{!Pre_Opportunity_Stage__c.Pre_Opportunity_Type__c}' == 'RFQ','{!Pre_Opportunity_Stage__c.Pre_Opportunity_Type__c}' == 'Solution Sale'){ 
window.open('https://cs50.salesforce.com/006/e?retURL=%{!Account.Id}&accid={!Account.Id}'); 
} 
else{ 
alert('You can not create new oppty'); 
}

In my URL:
https://cs50.salesforce.com/006/e?retURL=%&oppid=

The error message is:

Illegal Request
You have sent us an Illegal URL or an improperly formatted request. 


I don't know what i am missing here.

Can you help me on this?

Thanks & Regards,
Karthikeyan Chandran
Alexander TsitsuraAlexander Tsitsura
Hi Karthikeyan Chandran

Problem is that account not specified on your record, and instead of {!Account.Id} you have empty. You can add condition as "if ('{!Account.Id}')"

Try this code
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 

if('{!Pre_Opportunity_Stage__c.Pre_Opportunity_Type__c}' == 'RFQ','{!Pre_Opportunity_Stage__c.Pre_Opportunity_Type__c}' == 'Solution Sale'){ 
  if ('{!Account.Id}') {
    window.open('https://cs50.salesforce.com/006/e?retURL=%{!Account.Id}&accid={!Account.Id}');
  } else {
    window.open('https://cs50.salesforce.com/006/e');
  }
} 
else{ 
  alert('You can not create new oppty'); 
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex

 
This was selected as the best answer
Ketankumar PatelKetankumar Patel
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 

if('{!Pre_Opportunity_Stage__c.Pre_Opportunity_Type__c}' == 'RFQ' && '{!Pre_Opportunity_Stage__c.Pre_Opportunity_Type__c}' == 'Solution Sale'){ 
window.open('https://cs50.salesforce.com/006/e?retURL=%{!Account.Id}&accid={!Account.Id}'); 
} 
else{ 
alert('You can not create new oppty'); 
}

if you are checking more than one condition using if statement then you should have && or || between each condition within if statement.

I corrected your if condition please use above code and let me know if you get any error.

You also might need to correct your URL 'https//cs50.salesforce...........' based upon the business requirement and when you move this code to production you need to change https://cs50.salesforce.... to https://naXX.salesforcce.com where XX is your salesforce production instance number.