Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Marketplace
  • Advertise With Us
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Marketplace
    • Advertise With Us
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › How to insert Account and its related contacts through Salesforce Visualforce page?

    Tagged: Contact List in Salesforce, Contact Object, Contact Record, Insert Account, Multiple Contacts, Multiple Records, Salesforce Visualforce Page, Visualforce Component, Visualforce Controller in Salesforce

    • Salesforce® Discussions

      How to insert Account and its related contacts through Salesforce Visualforce page?

      Posted by shariq on August 8, 2017 at 6:07 AM

      I want to insert Account and its multiple contacts by filling fields on visualforce page.

      shariq replied 7 years, 6 months ago 3 Members · 3 Replies
      • Contact List in Salesforce
      • Contact Object
      • Contact Record
      • Insert Account
      • Multiple Contacts
      • Multiple Records
      • Salesforce Visualforce Page
      • Visualforce Component
      • Visualforce Controller in Salesforce
    • 3 Replies
    • Ajay Prakash

      Member
      January 13, 2018 at 3:05 PM

      Hi Shariq,

      You can not insert Account and its related contacts in the single transaction. You need to insert account first and then its related contacts. To insert related contacts you need the id of the parent Account.

      Please let us know what you have tried so far, in your scenario then we may be more helpful.

      Thanks.

       

      • This reply was modified 8 years, 2 months ago by  Ajay Prakash.
    • Parul

      Member
      September 15, 2018 at 2:42 PM

      In single transaction it's not possible to insert account and its related contacts because you have to insert first parent then its child record.

       

       

      Thanks

    • shariq

      Member
      September 16, 2018 at 10:19 AM

      Hi Ajay,

      This is the code I have written and it is working fine for me -

      Apex class - 

      public with sharing class AddContactsAccounts1
      {

      // public List <AddCon> customContacts {get; set;}
      public List <Account> accounts ;
      public List <Contact> contacts1{get; set;}
      public Map <Integer,List<Contact>> mapIntVsConList{get; set;}
      public Map <Integer,Account> mapIntVsAcc{get; set;}
      public Integer i ;
      public Integer j ;
      public Account acc {get; set;}
      public Contact con {get; set;}

      public AddContactsAccounts1()
      {
      i=0;j=0;
      accounts = new List<Account>();
      mapIntVsConList = new Map <Integer,List<Contact>>();
      mapIntVsAcc = new Map <Integer,Account>();
      // customAccounts = new List <AddAcc> ();
      //customContacts = new List <AddCon> ();
      contacts1 = new List<Contact>();
      // AddAcc customAccount = new AddAcc();
      // customAccounts.add(customAccount);
      acc = new Account();
      mapIntVsAcc.put(i,acc);
      mapIntVsConList.put(i,contacts1);
      }

      public void addNewAccount()
      {
      i++;
      //customContacts = new List <AddCon> ();
      acc = new Account();
      mapIntVsAcc.put(i,acc);
      contacts1 = new List<contact>();
      mapIntVsConList.put(i,contacts1);

      }
      public void addNewContact()
      {
      con = new Contact();
      contacts1.add(con);
      mapIntVsConList.put(i,contacts1);
      }

      public PageReference insertAccountsContacts()
      {
      //i=0;
      //Map <Id,List<Contact>> mapAccIdVsCon = new Map <Id,List<Contact>>();
      PageReference pageRefer = new PageReference('/apex/AddContactsAccountsPage1');
      /*for (AddAcc customAccount : customAccounts)
      {
      accounts.add(customAccount.acc);
      }*/
      //system.debug('accounts::::'+accounts);
      List<Contact> conList = new List<Contact>();
      for(Integer i : mapIntVsAcc.keySet())
      {
      accounts.add(mapIntVsAcc.get(i));
      }
      insert accounts;
      j = 0;
      for(Account a : accounts)
      {
      //List <Contact> contacts = new List <Contact>();
      for(Contact c : mapIntVsConList.get(j))
      {
      c.accountId = a.Id;
      conList.add(c);
      }
      // mapAccIdVsCon.put(a.Id, contacts);
      j++;
      }
      //system.debug('contacts::::'+contacts);

      /* for(Id ide :mapAccIdVsCon.keySet())
      {
      for(Contact c : mapAccIdVsCon.get(ide))
      {
      conList.add(c);
      }
      }*/
      insert conList;

      pageRefer.setRedirect(True);
      return pageRefer;
      // return Page.AddContactsAccountsPage1;
      }
      }

      Visualforce page - 

      <apex:page controller="AddContactsAccounts1" sidebar="false" >
      <apex:form id="form">
      <apex:pageBlock >
      <apex:pageBlockButtons >
      <apex:commandButton action="{!addNewAccount}" value="Add New Account" rerender="pageId"/>
      <apex:commandButton action="{!addNewContact}" value="Add New Contact" rerender="pageId"/>
      <apex:commandButton action="{!insertAccountsContacts}" value="Insert All" />
      </apex:pageBlockButtons>

      <apex:pageBlockTable var="customAccount" value="{!mapIntVsAcc}" id="pageId">
      <apex:column ><h1>
      Account Name
      </h1> <apex:inputfield value="{!mapIntVsAcc[customAccount].Name}" /></apex:column>
      <apex:column > <apex:pageBlockTable value="{!mapIntVsConList[customAccount]}" var="cont" columns="6" >
      <apex:column ><h1>
      Contact Last Name
      </h1> <apex:inputfield value="{!cont.LastName}" /> </apex:column>
      <apex:column ><h1>
      Gender
      </h1> <apex:inputfield value="{!cont.Gender__c}" /> </apex:column>
      <apex:column > <h1>
      Start Date
      </h1> <apex:inputfield value="{!cont.Start_Date__c}" /> </apex:column>
      <apex:column ><h1>
      End Date
      </h1> <apex:inputfield value="{!cont.End_Date__c}" /> </apex:column>
      </apex:pageBlockTable></apex:column>
      </apex:pageBlockTable>

      </apex:pageBlock>
      </apex:form>
      </apex:page>

      Thanks.

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    application solution

    Popular Salesforce Blogs

    Enabling and Creating Accounts in Salesforce

    Enabling and Creating Personal Accounts And Business Accounts in Salesforce

    Blog in Others

    Salesforce accounts are used to store information about customer and entities with which you are working such as government agencies, schools, companies, or individuals. You…

    Salesforce Customization, Salesforce Fields, Salesforce Objects, Salesforce Records
    Tanu Sep 24, 2016
    24,789  Views
    Salesforce - The Customer Success Platform To Grow Your Business Automation

    Salesforce: The Customer Success Platform To Grow Your Business Automation

    Blog in Salesforce Implementation, Salesforce Tools, Salesforce Tutorial

    If you’ve ever thought how great it would be to have a personal assistant, you should consider exploring Salesforce and its automation features. Imagine having…

    Approval Process, Business Automation, Customer Success, Customer Success Platform, Process Builder
    KeyNode Apr 3, 2018
    8,332  Views
    Sharing Settings in Salesforce | The Ultimate Guide

    Sharing Settings in Salesforce | The Ultimate Guide

    Blog in Others

    Salesforce is a powerful Customer Relationship Management (CRM) platform that offers a wide range of features and functionalities to its users. One of the most…

    Organization, Organization-wide Default, Owner-Based Sharing Rules, Permission Sets, Profiles
    Nikhil Kumar Mar 28, 2023
    2,682  Views

    Popular Salesforce Videos

    How To Send Email Using Apex | Apex Salesforce Tutorial

    How To Send Email Using Apex | Apex Salesforce Tutorial

    Video in Salesforce Apex

    This tutorial will show how you can send emails using Apex. The following points will be covered - 1. Introduction Take a look at the…

    Salesforce Training, Salesforce Tutorial, Salesforce Apex, Apex, Salesforce Video
    Algoworks Mar 17, 2021
    3,714  Views
    Spring'21 Prior Value in Salesforce Record Triggered Flows - ISCHANGED IN Salesforce Flows

    Spring'21 Prior Value in Salesforce Record Triggered Flows - ISCHANGED IN Salesforce Flows

    Video in Salesforce Admin

    In this video, I'm talking about the Prior value feature in the Spring'21 pre-release that is for the record triggered flow.  Pre-Release Org URL: https://www.salesforce.com/form/signup/prerelease-spring21/…

    Salesforce Training, salesforce, Salesforce VIdeos, salesforce record, Salesforce Learning
    Shubham Jan 14, 2021
    2,641  Views
    Salesforce Nonprofit Success Pack Implementation Guide

    Salesforce Nonprofit Success Pack Implementation Guide

    Video in Salesforce For Nonprofits

    There are so many things to consider when using Salesforce Nonprofit Success Pack for your nonprofit organization that it can be tricky to know where…

    Salesforce Training, Salesforce Tutorial, Salesforce Development, Salesforce Implementation, salesforce
    DB Aug 23, 2023
    1,260  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.