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 

How to Create contact and opportunity from account by using controller extention.

Hello i want to create contact and opportunity from Accounts detail page by using button.

i created controller extention but it created contact and opportunities they are not related to that particular account.

 here is my code :
public class OppClass3
    {
      Public Account Account;
      public Opportunity opportunity;
      public contact contact;
      public string ConName {get;set;}
      public String fstName {get;set;}
      public string Email{get;set;}
      public string conacc{get;set;}
      public string oppacc{get;set;}
      public string Phone {get;set;}
      public String OppName{get;set;}
      public Date d {get;set;}
     
    private final Account acc;
     public String OpportunityName {get;set;}
public String OpportunityStage {get;set;}
public Date ClosedDate {get;set;}
                
     public OppClass3(ApexPages.StandardController stdController) {
        this.acc= (Account)stdController.getRecord();
     }
 
      public account getAccount()
      {
      if(account == null)
      {
      Account = new Account();
      system.debug('Account------>'+account);
      
      }
      return Account;
      }
          
          public contact getcontact()
          {
          
          if(contact==null)
          contact = new contact();
          System.debug('contact--->'+contact);
          return contact;
          
          
          }
          
          public Opportunity getOpportunity()
          {
          if(opportunity == null)
          opportunity = new opportunity();
          
          System.debug('opportunity--->'+opportunity);
          return opportunity;
          
          }
 
    public PageReference NewOpp(){
         opportunity = new opportunity();
         contact = new contact();
        
               return new PageReference('https://sreeee.ap1.visual.force.com/apex/page14');
 
        }
        
        public PageReference mySave()
        {
        
        
        contact = new contact(lastname = conname,firstname = fstName,Email = email,phone = phone,accountid=acc.id );
        //contact.accountid = account.id;
        insert contact;
        
        
        
        opportunity = new opportunity(name = OppName,StageName='New Opportunity',CloseDate=d,accountid=acc.id);
       // opportunity.accountid = account.id;
        insert opportunity;
        
        PageReference pr = new Apexpages.StandardController(contact).view();
        
        
        pr.setRedirect(true);
         PageReference pr1 = new Apexpages.StandardController(opportunity).view();
        
        
        pr1.setRedirect(true);
        
      return new PageReference('https://sreeee.ap1.visual.force.com/apex/page14');
      // return null;
        
        }
 
}
<apex:page standardcontroller="Account" extensions="OppClass3">
    <apex:form >
 <apex:pageBlock >
 <apex:pageBlockButtons >
  <apex:commandButton value="Save" action="{!mysave}"/>
 <apex:commandButton value="Cancel" action="{!cancel}"/>
 <apex:commandButton value="New opp" action="{!NewOpp}"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Contact Information">
<apex:pageBlockTable value="{!contact}" var="o">
<apex:column headerValue="LastName"><apex:inputText value="{!ConName}"/></apex:column>

<apex:column headerValue="FirstName"><apex:inputText value="{!fstName}"/></apex:column>
<apex:column headerValue="phone"><apex:inputText value="{!phone}"/></apex:column>
<apex:column headerValue="Email"><apex:inputText value="{!Email}"/></apex:column>
<apex:column headerValue="AccountName"><apex:inputField value="{!o.accountid}"/></apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>


   <apex:pageBlockSection title="Opportunity Information">
   <apex:pageBlockTable value="{!Opportunity}" var="o">
   
   <apex:column headerValue="OpportunityName">
   <apex:inputText value="{!OppName}"/> </apex:column>
   <apex:column headerValue="AccountName"><apex:inputField value="{!o.accountid}"/></apex:column>
   <apex:column headerValue="Stage" >
   <apex:inputField value="{!o.stagename}"/> </apex:column>
    <apex:column headerValue="CloseDate" ><apex:inputText value="{!d}"/> </apex:column>
   
  </apex:pageBlockTable>
   
          <!-- <apex:pageBlockSectionItem >
                   <apex:outputLabel value="Opportunity Name"/>
                   <apex:inputText value="{!opportunityName}" />
           </apex:pageBlockSectionItem>
             
           <apex:pageBlockSectionItem >
                   <apex:outputLabel value="Opportunity Amount"/>
                  <apex:inputText value="{!opportunitystage}" />
           </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                   <apex:outputLabel value="ClosedDate"/>
                  <apex:inputText value="{!ClosedDate}" />
           </apex:pageBlockSectionItem>
            
            <apex:pageBlockSectionItem >
                    <apex:outputLabel value="AccountName"/>
                    <apex:outputField Value="{!Account.name}"/>
            </apex:pageBlockSectionItem>-->
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Thanks in advance !!!!
BalajiRanganathanBalajiRanganathan
1) You dont need to use standardcontroller="Account" for you VF. OppClass3 can be a controller.
2) In the button you created on account, pass the accountId and name in the query String and in the controller constructor get the accountId and set it to your Opportunity.accountId. 
3) Create the new Opportuniy object and contact object in the controller constructor,
4) you dont need to create the following variables

public string ConName {get;set;}
      public String fstName {get;set;}
      public string Email{get;set;}
      public string conacc{get;set;}
      public string oppacc{get;set;}
      public string Phone {get;set;}
      public String OppName{get;set;}
      public Date d {get;set;}

instead of this <apex:inputText value="{!ConName}"/>
you can use <apex:inputText value="{!contact.firstName}"/> etc