+ Start a Discussion
Mike_M_2Mike_M_2 

getting Account from overridden New Opportunity button which executes visualforce page?

Posting now to General Development because no answer on APEX Code Development. 

We had a 3rd party do a customization for us. They overrode the New Opportunity button with a button that invokes a visualforce page. The controller for this VF page marshalls the data and then creates an opportunity. The problem is when the user clicks the NEW Opportunity button from an Account screen. The Account Name is not autopopulated to the Opportunity.Is there any way to Access the Account Name within the controller for this VF page? 

Thanks 
VinayVinay (Salesforce Developers) 
Hi Mike,

Below is sample code for your reference on Opportunity which will populate Opportunity Name.

Code is for reference and understanding it will be different in your use case.
 
<apex:page standardController="Opportunity" extensions="CreateOppExtension"  >
    <apex:sectionHeader title="Opportunity Edit" subtitle="New Opportunity Edit" />

    <apex:form>
        <apex:inputField label="Account Name" value="{! Opportunity.AccountId }" required ='true' />
    </apex:form>
</apex:page>


public class CreateOppExtension 
{
    Opportunity opp {get;set;}
    String Owner {get;set;}
    public string accname{get;set;}  
    ApexPages.StandardController GstdController;

    public CreateOppExtension(ApexPages.StandardController stdController) {   
        GstdController= stdController;
        opp = (Opportunity)stdController.getRecord();
        // opp.Notes__c = 'Testing is done in this Profile,It should be visible for only PM-I record type.';
        // opp.AccountId=apexpages.currentpage().getparameters().get('AccountId');
    }

    public PageReference save() {
        insert opp;
        return new PageReference('/'+opp.Id);
    }
}

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
Mike_M_2Mike_M_2
Hi Vinay,
Based on your answer, I can see that my question was not clear. 
I will try to make my question more clear. When using the standard New Opportunity button from the Opportunity home page, if the user wants to have the new opportunity be associated with an account, the user must click on the 'Account Name Lookup' widget to the right of the Account Name field on the new oportunity screen. If, however, the user goes first to an existing Account screen and from there clicks on the New Opportunity button, the Account name will be prepopulated into the new opportunity screen. That's the standard functionality, but now the NEW Opportunity button has been overridden. This override applies to the button on the Opportunity Home screen as well as the New Opportunity button on the Account's opportunity Related List. The button override invokes a VF page. The Controller on the VF page inserts a new opportunity (as you would expect). However, if said VF page is invoked via the button on the Account's opportunity Related List, the controler has no knowlege of the fact that it was launched via the Account, as opposed to the Opportunity Home screen. So, my question is, how can the controller get the Account name of the Account where the new opportunity button was invoked? I want it to work like the standard new opportunity button where the account name is pre-populated into the Opportunity Account Name field when invoked from and existing account. 
TIA,
Mike