ApexClass:
public class ContactCreation {
public contact myNewContact{set;get;}
Account acc;
public ContactCreation (ApexPages.StandardController st){
this.acc = (Account)st.getRecord();
myNewContact = new contact();
}
public pageReference SaveRecord(){
myNewContact.AccountId = acc.Id;
Insert myNewContact;
PageReference pageRef = new PageReference('/'+myNewContact.id);
pageRef.setredirect(true);
return pageRef;
}
}
Visualforce page:
<apex:page standardcontroller="Account" extensions="ContactCreation" sidebar="false" showHeader="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputfield value="{!myNewContact.lastname}"/>
<apex:inputfield value="{!myNewContact.firstname}"/>
<apex:inputfield value="{!myNewContact.email}"/>
<apex:inputfield value="{!myNewContact.MobilePhone}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!SaveRecord}" value="Save"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>