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
chanbasha nbskchanbasha nbsk 

how to create new contact from visualforce page to related list contacts in Account Details page?

Hi All,

   how to create a new contact from visualforce page to related list contacts in Account Details page?

Thanks,
Basha
SandhyaSandhya (Salesforce Developers) 
Hi Chanbasha,

Below is the sample code which creates an account and also a contact for that particular account so that it will be shown in the related list.If you want to create a contact for an existing account, then you need to query for account name and insert the contact.

Visualforce Page
 
<apex:page standardController="Contact" extensions="exampleClass1">

   <apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection >
     <apex:inputField value="{!Contact.FirstName}"/>
     <apex:inputField value="{!Contact.LastName}"/>
     </apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
     <apex:commandButton action="{!save}" value="Save"/>
     </apex:pageBlockButtons>
    </apex:pageBlock>

   </apex:form>

</apex:page>

Controller
 
public with sharing class exampleClass1 {

    Contact newCon;

    public exampleClass1(ApexPages.StandardController con) {
          this.newCon = (Contact)con.getRecord();

   

    }
    public pagereference save(){
      Account a = New Account(Name = newCon.FirstName + ' ' + newCon.LastName);
      Insert a;
      newCon.AccountID = a.id;
      insert newCon;

      Pagereference pr = New PageReference('/' + a.id);
      return pr;
   }


}

Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Sandhya

chanbasha nbskchanbasha nbsk
Hi Sandhya,

   Thanks for replying
 I don't need the new account creation, In account related list in contacts section i add a button called"Add Multiple Contacts" by clicking the button it will be redirected to a visualforce page we are  developed for entering the contact details.and then by clicking the save button it will be redirected to a account details page.Thats it.

Thanks,
Basha