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
carlsosgcarlsosg 

Create Opportunity from Contact

I would like to be able to create an Opportunity from a Contact - using a VF page to limit the fields required for the Opportunity. I want to make sure the current Contact's Account is pre-populated as a read-only field. I would appreciate any direction on doing this.

 

I'm having some difficulty learning how to call a VF page and pass a Contact's ID when a non-standard controller is used on the VF page.

 

Thank you

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

I threw this together quickly but if you creat a button on the contact page to call this vf page you can creat a new opportunity and associate it to the account related to the contact.

 

you will have to clean up the save function to redirect to another page when done, etc, but it works.

 

VF Page:

 

<apex:page standardController="Contact" extensions="ConExtensionExample">

<apex:form >

<apex:pageBlock >
<table id="iTable" cellspacing="10" align="center" width="100%" >

                <tr style="text-align: center" >
                 
                    <td>Opportunity Name</td>
                    <td>Opportunity Type</td>  
                    <td>Stage</td>
                    <td>Departments</td>
                    <td>Close Date</td>
                    <td>Who Initiated Contact</td>

                </tr>
                 
                        <tr style="text-align:center">

                                <td><apex:inputField id="idA" value="{!oOpp.Name}" required="true"/></td>
                                <td><apex:inputField id="idB" value="{!oOpp.Opportunity_Type_ED_or_CC__c}" required="true"/></td>
                                <td><apex:InputField id="idC" value="{!oOpp.StageName}" required="True" /></td>
                                <td><apex:InputField id="idD" value="{!oOpp.Departments__c}" required="True" /></td>
                               <td><apex:InputField id="idE" value="{!oOpp.CloseDate}" required="True" /></td>
                                <td><apex:InputField id="idF" value="{!oOpp.Who_Initiated_Contact__c}" required="True" /></td>
            
                                <td><apex:commandLink action="{!iSave}" Value="Save" /></td>
          
                </tr>
            </table>
        
        </apex:pageBlock>
</apex:form>

</apex:page>

 

VF Controller Extension

 

public class ConExtensionExample {

Private Contact thisCon;
Public ID idAcct{get;set;}
Public Opportunity oOpp{get;set;}



    public ConExtensionExample(ApexPages.StandardController controller) {
        
        thisCon=(Contact)controller.getRecord();
        init();

   }

private void init(){

idAcct = [Select  id, AccountID From Contact where id=: thisCon.ID].AccountID;

oOpp = New Opportunity(AccountID = idAcct);

}

public pagereference iSave(){


    if(oOpp.Id == Null){
        oOpp.AccountID = idAcct;
        insert oOpp;
     }else{
        upsert oOpp;  
     }  
return null;       
         
} 
}

 

All Answers

Starz26Starz26

You could user the Contact controller as a standard controller

 

Create a ContactControloler Extension

 

Private Contact thisCon;

Public ID idAcct{get;set;}

Public Opportunity oOpp;

 

in the initalizing of your controllerExtension

 

thisCon=(Contact)controller.getRecord();

init();

 

 

in your init() function:

 

get reference to current contact record

idAcct = [Select  id, AccountID From Contact where id=: thisCon.ID].AccountID;

oOpp = New Opportunity(AccountID = idAcct);

 

In your VF Page:

 

Reference the fields on the Opportunity on your VF page

 

This may not be the shortest way to do it and assumes you are calling the page from the contact record.

carlsosgcarlsosg

For some reason, I still can't get this to work quite right. Could you please post your APEX code solution?

Starz26Starz26

I threw this together quickly but if you creat a button on the contact page to call this vf page you can creat a new opportunity and associate it to the account related to the contact.

 

you will have to clean up the save function to redirect to another page when done, etc, but it works.

 

VF Page:

 

<apex:page standardController="Contact" extensions="ConExtensionExample">

<apex:form >

<apex:pageBlock >
<table id="iTable" cellspacing="10" align="center" width="100%" >

                <tr style="text-align: center" >
                 
                    <td>Opportunity Name</td>
                    <td>Opportunity Type</td>  
                    <td>Stage</td>
                    <td>Departments</td>
                    <td>Close Date</td>
                    <td>Who Initiated Contact</td>

                </tr>
                 
                        <tr style="text-align:center">

                                <td><apex:inputField id="idA" value="{!oOpp.Name}" required="true"/></td>
                                <td><apex:inputField id="idB" value="{!oOpp.Opportunity_Type_ED_or_CC__c}" required="true"/></td>
                                <td><apex:InputField id="idC" value="{!oOpp.StageName}" required="True" /></td>
                                <td><apex:InputField id="idD" value="{!oOpp.Departments__c}" required="True" /></td>
                               <td><apex:InputField id="idE" value="{!oOpp.CloseDate}" required="True" /></td>
                                <td><apex:InputField id="idF" value="{!oOpp.Who_Initiated_Contact__c}" required="True" /></td>
            
                                <td><apex:commandLink action="{!iSave}" Value="Save" /></td>
          
                </tr>
            </table>
        
        </apex:pageBlock>
</apex:form>

</apex:page>

 

VF Controller Extension

 

public class ConExtensionExample {

Private Contact thisCon;
Public ID idAcct{get;set;}
Public Opportunity oOpp{get;set;}



    public ConExtensionExample(ApexPages.StandardController controller) {
        
        thisCon=(Contact)controller.getRecord();
        init();

   }

private void init(){

idAcct = [Select  id, AccountID From Contact where id=: thisCon.ID].AccountID;

oOpp = New Opportunity(AccountID = idAcct);

}

public pagereference iSave(){


    if(oOpp.Id == Null){
        oOpp.AccountID = idAcct;
        insert oOpp;
     }else{
        upsert oOpp;  
     }  
return null;       
         
} 
}

 

This was selected as the best answer
carlsosgcarlsosg

Thank you. One part I'm missing is how can I force a field selection in the new Opportunity. For example, how can I force a specific stage name to be saved on the new Opportunity?

Starz26Starz26

Change the construct of the new Opp to

 

oOpp = New Opportunity(AccountID = idAcct, StageName = '**your value here**');

 

And if you do not want them to change the value change the inputfield for StageName to:

 

<apex:Outputfield id="idC" value="{!oOpp.StageName}" />

carlsosgcarlsosg

Thank you. This has been very helpful!