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
sasya.ravisasya.ravi 

I want to throw error while creating new opportunity

Account having a checkbox field if that flag value is true then clicking on newopportunity button it should throw error?
Brenda S FinnBrenda S Finn
It depends on whether or not the org you are in is still using classic SF or if they have moved to Lightning. You can achieve this quickly and easily with a Javascript button that overrides the New button on Opportunity. However, Javascript buttons are not supported in Lightning. So the longer term solution is to build a Visualforce page using Lightning Design System library to get the Lightning look&feel and to ensure that your org is ready to move to Lightning when the time comes, unless you are already in Lightning.

Both approaches are pretty quick, but Javascript is definitely faster if it is an option for you. Just check for your field's value off Account and if true, display a Javascript alert button to the end-user. If false, then redirect user to the standard Opportunity create page. (/006/e).

For Ligthning, override the New button with a Visualforce page. Use the standard Controller. Use apex:outputPanel to either render an error or to redirect them to the Opportunity create page. It would look something like this (replacing reference to LDS,   field name and error message).
 
<apex:page standardStyleSheets="false" 
    standardController="Opportunity"
    docType="html-5.0">

    <!-- lightning design system -->
    <link rel="stylesheet" type="text/css" href="{!URLFOR($Resource.SLDS0121, 
        'assets/styles/salesforce-lightning-design-system-vf.css')}" />
        <div class="slds">
            <apex:outputPanel rendered="{!NOT(Opportunity.Account.Prevent_New_Opp__c)}">
                <div class="slds-notify-container">
                    <div class="slds-notify-alert slds-theme--error">
                        You cannot create an Opportunity.
                    </div>
                </div>
            </apex:outputPanel>
            <apex:outputPanel rendered="{!NOT(Opportunity.Account.Prevent_New_Opp__c)}">
                <script language="javascript">
                     window.location.href="/006/e";   
                     // optionally add a retUrl to return the user to the Account page
                </script>
            </apex:outputPanel>
        </div>
</apex:page>

Hope that helps!
 
Deepak GulianDeepak Gulian
Create a new custom button in Opportunity , Display Type - List Button , Execution - onclick Javascript & override the standard
"New Opportunity" Button in related list of Account page layout.

Javascript code:

if({!Account.checkbox_field_api_name})
window.location = '/006/e?retURL={!Account.Id}&accid={!Account.Id}';
else
alert('Field is not checked');
sasya.ravisasya.ravi
I want to override with vf page how can i do that
Deepak GulianDeepak Gulian
You can navigate to the visualforce page
If you want to pass some parameter also do below
window.location = '/apex/VF_Page_Name?accId={!Account.Id}';
Else do this
window.location = '/apex/VF_Page_Name';

if({!Account.checkbox_field_api_name})
window.location = '/apex/VF_Page_Name';
else
alert('Field is not checked');
sasya.ravisasya.ravi
Hi Deepak,

Thanks for the solution , here i have different record types in Opportunity, Already this new button is overrided with Vf page.
Now i want to add my logica on Same method and want to call this method on another new vf page and show the error message
sasya.ravisasya.ravi
I want to add my logic into redirect method and create a new page to show the error message

public class NewOpportunity {
    String recordId;
    
    String oppAcctId = ApexPages.currentPage().getParameters().get('accid'); 
    

    public NewOpportunity(ApexPages.StandardController controller) {
        
        this.recordId = controller.getId();
    }
    
   
    public PageReference redirect() {
    
    f(oppAcctId == null)
            {
                PageReference pr1 = new PageReference('/setup/ui/recordtypeselect.jsp?ent=Opportunity&retURL=%2F006%2Fo&save_new_url=%2F006%2Fe%3FretURL%3D%252F006%252Fo'); 
                pr1.getParameters().put('nooverride','1');
                return pr1; 
            }
            else
            {
                PageReference pr2 = new PageReference('/setup/ui/recordtypeselect.jsp?ent=Opportunity&retURL=%2F0011100000pm5gT&save_new_url=%2F006%2Fe%3FretURL%3D%252F' + oppAcctId + '%26accid%3D' +  + oppAcctId);
                pr2.getParameters().put('nooverride','1');
                return pr2; 
            }
        
       
    }
}
Deepak GulianDeepak Gulian
Id oppAcctId = ApexPages.currentPage().getParameters().get('accid'); 

public PageReference redirect() {
    
   if(oppAcctId == null)
   {
                PageReference pr1 = new PageReference('/setup/ui/recordtypeselect.jsp?ent=Opportunity&retURL=%2F006%2Fo&save_new_url=%2F006%2Fe%3FretURL%3D%252F006%252Fo'); 
                pr1.getParameters().put('nooverride','1');
                return pr1; 
            }
            else
            {
              Account acc = [Select Checkbox_field From Account Where id =: oppAcctId] ;
             if(acc.Checkbox_field != true){
                    //Redirect to new VF page
            }
            else{
                             PageReference pr2 = new PageReference('/setup/ui/recordtypeselect.jsp?ent=Opportunity&retURL=%2F0011100000pm5gT&save_new_url=%2F006%2Fe%3FretURL%3D%252F' + oppAcctId + '%26accid%3D' +  + oppAcctId);
                pr2.getParameters().put('nooverride','1');
                return pr2; 
          } 
  }
        
       
    }