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, 2 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 7 years, 10 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

    Platform Events in Salesforce

    Learn All About Platform Events in Salesforce | The Developer Guide

    Blog in Others

    Platform events are based on an event-driven architecture for delivering secure, extensible, and customizable notifications in Salesforce or external applications.  This is a real-time integration…

    Apex Triggers, API, API Client, API Name, Cloud Flow Designer Tool
    Kirandeep Feb 1, 2021
    6,191  Views
    Visualforce Basics

    Visualforce Basics | Salesforce Learning Guide

    Blog in Visualforce

    Visualforce is a powerful tool and a stimulating framework allowing developers to explain the interface component and to create sophisticated custom interfaces which will be…

    Components, Controller, Controllers and Extensions, Custom Controller, Custom Interfaces
    Narendra Sa Jan 14, 2022
    4,381  Views

    Everything You Need to Know About Customer Data Platform

    Blog in Salesforce

    Considering that businesses need to do all they can to remain competitive, technology is surely one thing that you’ll want to invest in. And while…

    app visits, CDP, churning, Compile Data, contact center interactions
    Apphienz Dec 1, 2022
    2,879  Views

    Popular Salesforce Videos

    Traditional AI vs Generative AI | What the difference? | Salesforce

    Traditional AI vs Generative AI | What the difference? | Salesforce

    Video in AI and ML, Salesforce Einstein

    Explore how traditional AI differ from Generative AI from this extract from our latest webinar! We're going to explore the strengths and not-so-strong points of…

    Salesforce Training, salesforce, Artificial Intelligence, Salesforce Community, Salesforce Webinar
    Cyntexa Oct 31, 2023
    1,117  Views
    Salesforce Diagram | Salesforce Diagramming Framework

    Salesforce Diagram | Salesforce Diagramming Framework

    Video in Salesforce Stories, Salesforce Training

    Diagrams help drive key project decisions, align delivery teams, and help everyone understand where you’re going and what’s been done. Consistent, easy-to-understand diagrams are a…

    Salesforce Training, salesforce, Salesforce Video, Salesforce Learning, Connectors
    Prafull Aug 5, 2021
    1,718  Views
    Salesforce Commerce Cloud Migration Key Considerations

    Salesforce Commerce Cloud Migration Key Considerations

    Video in Salesforce Cloud Platform, Salesforce Training

    Embarking on a Salesforce Commerce Cloud migration journey? This extract from our latest webinar outlines the key considerations and best practices to ensure a seamless…

    salesforce, Salesforce Commerce Cloud, Salesforce Video, Commerce Cloud, Latest Webinar
    Cyntexa Nov 24, 2023
    884  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

    © 2025 - 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.