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
Charles McDowellCharles McDowell 

I am new to Apex. I have the following markup for a new account. I need to add a contact record based on the firstname and lastname inputText fields.Can anyone give me some direction. Thanks

<apex:page standardController="Account">
    <apex:pageBlock >
        <apex:form >
            <apex:pageBlockSection >
            <apex:pageBlockSection columns="1">
                <apex:commandButton action="{! Save}" value="Save"/>
                <apex:inputField value="{! Account.Name}"/>
                <apex:inputText id="cFirstName" label="FirstName" />
                <apex:inputText id="cLastName" label="LastName" />
                <apex:inputField value="{! Account.BillingStreet}"/>
                <apex:inputField value="{! Account.BillingCity}"/>
                <apex:inputField value="{! Account.BillingState}"/>
                <apex:inputField value="{! Account.Phone}"/>
                <apex:inputField value="{! Account.BillingStreet}"/>
                </apex:pageBlockSection>
            </apex:pageBlockSection>
            
            <apex:pageBlock title="Contacts">
                <apex:pageBlockSection >
                    <apex:pageBlockSection columns="2">
 
                    </apex:pageBlockSection>
                </apex:pageBlockSection>
            </apex:pageBlock>
           
        </apex:form>
    
    </apex:pageBlock>
</apex:page>
Prateek Singh SengarPrateek Singh Sengar
Hi Charles,
You will need to add an controller or extension to save both account and contact from same page. You can try the below code that utilzes an custom controller.
 
<apex:page controller="AccountContactController">  
   <!-- ADD SECTION HEADER -->  
   <apex:sectionHeader title="Demo VF Page" subtitle="Create Account and Contact"/>  
   <apex:form >  
   <!-- CREATE PAGE BLOCK -->  
     <apex:pageBlock mode="edit">  
         <!-- SECTION TO CREATE ACCOUNT -->  
         <apex:pageBlockSection title="Account Info" columns="2">  
              <apex:inputField value="{!acc.Name}"></apex:inputField>  
           <apex:inputField value="{!acc.Type}"></apex:inputField>  
           <apex:inputField value="{!acc.ShippingStreet}"></apex:inputField>  
           <apex:inputField value="{!acc.ShippingCity}"></apex:inputField>  
           <apex:inputField value="{!acc.ShippingState}"></apex:inputField>  
           <apex:inputField value="{!acc.ShippingCountry}"></apex:inputField>  
           <apex:inputField value="{!acc.Shippingpostalcode}"></apex:inputField>  
         </apex:pageBlockSection>  
         <!-- SECTION TO CREATE CONTACT -->  
         <apex:pageBlockSection title="Contact Info" columns="2">  
              <apex:inputField value="{!con.FirstName}"></apex:inputField>  
           <apex:inputField value="{!con.LastName}"></apex:inputField>  
           <apex:inputField value="{!con.HomePhone}"></apex:inputField>  
           <apex:inputField value="{!con.MobilePhone}"></apex:inputField>  
         </apex:pageBlockSection>  
         <!-- ADD BUTTONS -->  
         <apex:pageBlockButtons >  
           <apex:commandButton value="Clear"/>  
           <apex:commandButton value="Save" action="{!saveAccCont}"/>  
         </apex:pageBlockButtons>  
     </apex:pageBlock>  
   </apex:form>  
 </apex:page>

Your Controller will be
public class AccountContactController {  
   public Account acc{get;set;}  
   public Contact con{get;set;}  
   public AccountContactController()  
   {  
     acc = new Account();  
     con = new Contact();  
   }  
   public pageReference saveAccCont()  
   {  
     PageReference pg = null;  
     if(acc != null)  
     {  
       insert acc;  
       if(con != null && acc.Id != null)  
       {  
         con.AccountId = acc.Id;  
         insert con;  
         pg = new PageReference('/'+acc.Id);  
         return pg;  
       }  
       else  
       {  
         return pg;  
       }  
     }  
     else  
     {  
          return pg;  
     }  
   }  
   
 }
Please add / remove fields that you dont need. Hope this helps