Skip to content
  • Surfmyindia |
  • Lifedreamguide |
  • w3webmart |
  • Overstockphoto |
  • News24globalworld |
  • News24classictimes |
  • Incriediableindia |
  • TechW3web |
  • W3Force ..
w3web.net
Sign Up to Get Free Code Access โ†’ |
  • Home
  • Tutorial
    • Lightning Component
    • Salesforce LWC
    • Salesforce Integration
    • Visualforce
    • Trigger
    • JQuery
  • Get Free Courses
  • Integration
  • Aura Comp
  • Salesforce LWC
  • Visualforce
  • Trigger
  • JQuery
  • React Js
  • More
    • About Us
    • Contact Us
    • Sitemap
    • Course Pricing
    • Blog
    • Gallery
    • Most Popular Articles
    • Download E-Book
    • YouTube Channel


Create multiple Contacts in Salesforce Using a LWC Using Database.insert for bulk record insert, Single LWC NO template loop NO dynamic rows Insert multiple Contacts related to that Account

February 8, 2026February 7, 2026 by Vijay Kumar
120 views

Insert multiple Contacts related to that Account Use Database.insert() for bulk records in LWC Salesforce

SCENARIO

  • Enter Account Name, Phone, Description
  • Enter details for 3 Contacts (simple static fields)
  • Click button
  • Apex will:
    • Create Account
    • Insert all Contacts using Database.insert()
    • Attach them to Account

 

Create Apex Class โ†’ AccountContactBulkController.cls

Step 1:- AccountContactBulkController.cls

๐Ÿ‘‰ Download Free Ebook โ†’

๐Ÿ‘‰ Get Complementary โ†’

right side banner -- www.w3webmart.com

AccountContactBulkController.cls [Apex Controller]

  1.    public WITH sharing class AccountContactBulkController {
  2.  
  3.     @AuraEnabled
  4.     public static String createAccountWithContacts(Account acc, List<Contact> contactList) {
  5.  
  6.         try {
  7.             // Step 1: INSERT Account
  8.             INSERT acc;
  9.  
  10.             // Step 2: Assign AccountId TO Contacts
  11.             FOR(Contact c : contactList){
  12.                 c.AccountId = acc.Id;
  13.             }
  14.  
  15.             // Step 3: Bulk INSERT Contacts
  16.             DATABASE.SaveResult[] results = DATABASE.insert(contactList, FALSE);
  17.  
  18.             INTEGER successCount = 0;
  19.             INTEGER failCount = 0;
  20.  
  21.             FOR(DATABASE.SaveResult sr : results){
  22.                 IF(sr.isSuccess()){
  23.                     successCount++;
  24.                 } ELSE {
  25.                     failCount++;
  26.                 }
  27.             }
  28.  
  29.             RETURN 'Account Created: ' + acc.Name +
  30.                    ' | Contacts Total: ' + contactList.size() +
  31.                    ' | Success: ' + successCount +
  32.                    ' | Failed: ' + failCount;
  33.  
  34.         } catch(Exception e){
  35.             throw NEW AuraHandledException(e.getMessage());
  36.         }
  37.     }
  38. }

 

Component Name: accountContactSimple.html โ†’

Step 2:- LWC HTML โ€” Input Fields for Multiple Contacts Related to Account

accountContactSimple.html [HTML Component]

  1.    <template>
  2.     <lightning-card title="Create Account & 3 Contacts">
  3.  
  4.         <div class="slds-m-around_medium">
  5.  
  6.             <!-- ACCOUNT FIELDS -->
  7.             <h2>Account Details</h2>
  8.  
  9.             <lightning-INPUT label="Account Name" name="accName" onchange={handleChange}></lightning-input>
  10.             <lightning-INPUT label="Phone" name="accPhone" onchange={handleChange}></lightning-input>
  11.             <lightning-INPUT label="Description" name="accDesc" onchange={handleChange}></lightning-input>
  12.  
  13.             <br/>
  14.  
  15.             <!-- CONTACT 1 -->
  16.             <h2>Contact 1</h2>
  17.             <lightning-INPUT label="First Name" name="c1First" onchange={handleChange}></lightning-input>
  18.             <lightning-INPUT label="Last Name" name="c1Last" onchange={handleChange}></lightning-input>
  19.             <lightning-INPUT label="Email" name="c1Email" TYPE="email" onchange={handleChange}></lightning-input>
  20.  
  21.             <br/>
  22.  
  23.             <!-- CONTACT 2 -->
  24.             <h2>Contact 2</h2>
  25.             <lightning-INPUT label="First Name" name="c2First" onchange={handleChange}></lightning-input>
  26.             <lightning-INPUT label="Last Name" name="c2Last" onchange={handleChange}></lightning-input>
  27.             <lightning-INPUT label="Email" name="c2Email" TYPE="email" onchange={handleChange}></lightning-input>
  28.  
  29.             <br/>
  30.  
  31.             <!-- CONTACT 3 -->
  32.             <h2>Contact 3</h2>
  33.             <lightning-INPUT label="First Name" name="c3First" onchange={handleChange}></lightning-input>
  34.             <lightning-INPUT label="Last Name" name="c3Last" onchange={handleChange}></lightning-input>
  35.             <lightning-INPUT label="Email" name="c3Email" TYPE="email" onchange={handleChange}></lightning-input>
  36.  
  37.             <br/><br/>
  38.  
  39.             <lightning-button 
  40.                 label="Create Account & Contacts"
  41.                 variant="brand"
  42.                 onclick={handleCreate}>
  43.             </lightning-button>
  44.  
  45.         </div>
  46.  
  47.     </lightning-card>
  48. </template>

 

accountContactSimple.js โ†’

Step 3:- LWC JavaScript

accountContactSimple.js [HTML Component]

  1.  
  2.   import { LightningElement, track } FROM 'lwc';
  3. import createAccountWithContacts FROM '@salesforce/apex/AccountContactBulkController.createAccountWithContacts';
  4. import { ShowToastEvent } FROM 'lightning/platformShowToastEvent';
  5.  
  6. export DEFAULT class AccountContactSimple extends LightningElement {
  7.  
  8.     @track formData = {};
  9.  
  10.     handleChange(event) {
  11.         this.formData[event.target.name] = event.target.value;
  12.     }
  13.  
  14.     handleCreate() {
  15.  
  16.         // CREATE Account Object
  17.         let acc = {
  18.             Name: this.formData.accName,
  19.             Phone: this.formData.accPhone,
  20.             Description: this.formData.accDesc
  21.         };
  22.  
  23.         // CREATE Contact List
  24.         let contactList = [
  25.             {
  26.                 FirstName: this.formData.c1First,
  27.                 LastName: this.formData.c1Last,
  28.                 Email: this.formData.c1Email
  29.             },
  30.             {
  31.                 FirstName: this.formData.c2First,
  32.                 LastName: this.formData.c2Last,
  33.                 Email: this.formData.c2Email
  34.             },
  35.             {
  36.                 FirstName: this.formData.c3First,
  37.                 LastName: this.formData.c3Last,
  38.                 Email: this.formData.c3Email
  39.             }
  40.         ];
  41.  
  42.         createAccountWithContacts({ acc: acc, contactList: contactList })
  43.             .then(RESULT => {
  44.                 this.showToast('Success', RESULT, 'success');
  45.             })
  46.             .catch(error => {
  47.                 this.showToast('Error', error.body.message, 'error');
  48.             });
  49.     }
  50.  
  51.     showToast(title, message, variant) {
  52.         this.dispatchEvent(
  53.             NEW ShowToastEvent({
  54.                 title: title,
  55.                 message: message,
  56.                 variant: variant
  57.             })
  58.         );
  59.     }
  60. }

 

accountContactSimple.js-meta.xml โ†’

Step 4:- LWC accountContactSimple.js-meta.xml

accountContactSimple.js-meta.xml [meta.xml Component]

  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>61.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__HomePage</target>
  7.         <target>lightning__AppPage</target>
  8.         <target>lightning__RecordPage</target>
  9.     </targets>
  10. </LightningComponentBundle>

 

Further post that would you like to learn in Salesforce

  • LWC how to get selected radio button value on handle-change function and displaying content of radio button checked value Using โ€˜lightning-radio-groupโ€™ element in Lightning Web Component (LWC)
  • Apex Trigger on Contact to Create Account Automatically and map to related account Id to Contact Whenever New Contact is Created in Salesforce
  • How to use output-field label/value represents as a read-only help-text Using of lightning:outputField element in Salesforce LWC

  • Creating a Lightning Datatable with Row Actions and Display a Modal Popup on Click View Icon Button in Salesforce Lightning Web Component – LWW
  • How to disabled all of input field values dynamically based on button click Uses of foreach loop in javascript in Lightning Web component Salesforce โ€“ LWC
  • Fetching picklist values dynamically through apex class method and display selected picklist value in Salesforce lightning web component โ€“ LWC
  • LWC to Get Logged in User Id, Name, Email, IsActive, Alias details without apex class in LWC
  • How to get/set checkbox value in lwc component and display the selected value using of “lightning-checkbox-group” property in LWc
  • How to display static resource image uses of Import library of “@salesforce/resourceUrl” in Salesforce Lightning Web Component — LWC
  • Retrieve picklist values dynamically of Opportunity Object without apex class using ‘uiObjectInfoApi’ property in LWC
  • How do you use template if:true and template if:false condition to display content in lightning web component – LWC
  • How to display account related contacts based on AccountId using the CustomEvent & dispatchEven in Salesforce lightning web component – LWC

 

FAQ (Frequently Asked Questions)

What is the difference between @param and RequestParam?

Here, Employee is JPA Entity, and @Param is used to bind the method parameter departmentId to Query parameter deptId. In your case, you are trying to fetch URL Parameter value. @RequestParam need to be used. @RequestParam is used to bind method parameter to web URL request parameter.

Can we have multiple HTML files in LWC?

Although it's possible for a component to render multiple templates, we recommend using the lwc:if|elseif|else directives to render nested templates conditionally instead. Create multiple HTML files in the component bundle.

What is the difference between Slds and Slds 2?

The original SLDS is now called SLDS 1. SLDS 2 is built with a new CSS framework that separates the structure from the visual design using styling hooks. Styling hooks are CSS custom properties that enable deeper customization and theming, allowing you to create unique experiences faster.

Related Topics | You May Also Like

  • Add Multiple Rows button (Dynamic Contact Rows) Bulk Insert Contacts Related to Account using Database.insert() in LWC SalesforceAdd Multiple Rows button (Dynamic Contact Rows) Bulk Insert Contacts Related to Account using Database.insert() in LWC Salesforce
  • Create multiple Contacts in Salesforce Using a Lightning Web Component (LWC) Calling an Apex method Using Database.insert for bulk record insert and Displaying success & error messagesCreate multiple Contacts in Salesforce Using a Lightning Web Component (LWC) Calling an Apex method Using Database.insert for bulk record insert and Displaying success & error messages
  • Whenever an Accountโ€™s Phone or Email is updated, Update all related Contacts with the same Phone & Email Use @future method (asynchronous processing) in SalesforceWhenever an Accountโ€™s Phone or Email is updated, Update all related Contacts with the same Phone & Email Use @future method (asynchronous processing) in Salesforce
  • How to create account related contacts automatically uses of LWC Apex Framework based on database.insert bulk records in Salesforce โ€“ LWC | Create Contact automatically whenever Account is created using LWC object Apex Framework in LWC – Lightning Web Component Salesforce | How to create LWC for Account with Contact at the same time through Apex Object Framework in LWC SalesforceHow to create account related contacts automatically uses of LWC Apex Framework based on database.insert bulk records in Salesforce โ€“ LWC | Create Contact automatically whenever Account is created using LWC object Apex Framework in LWC – Lightning Web Component Salesforce | How to create LWC for Account with Contact at the same time through Apex Object Framework in LWC Salesforce
  • How to create Contacts related to Account uses of LWC Apex Framework based on database.insert bulk records in Salesforce | Pass Data from Parent to Child Component in LWC SalesforceHow to create Contacts related to Account uses of LWC Apex Framework based on database.insert bulk records in Salesforce | Pass Data from Parent to Child Component in LWC Salesforce
  • How to fetch current record based on Record Id uses of lightning web component and apex class method in LWC Salesforce | How to pass dynamic record id and display current record based on Record Id in lwc Salesforce | How to fetch and display record data in lightning web component – LWCHow to fetch current record based on Record Id uses of lightning web component and apex class method in LWC Salesforce | How to pass dynamic record id and display current record based on Record Id in lwc Salesforce | How to fetch and display record data in lightning web component – LWC
  • Do not allow create duplicate record of Contact if associated Account has same Phone number in Salesforce || If Contact is created or updated then we will check associated Accountโ€™s Phone, if associated Account has same phone, then we do not allow to create or update contact record, here we will display error message in SalesforceDo not allow create duplicate record of Contact if associated Account has same Phone number in Salesforce || If Contact is created or updated then we will check associated Accountโ€™s Phone, if associated Account has same phone, then we do not allow to create or update contact record, here we will display error message in Salesforce
  • Create a record on Contact to insert record Id of lookup field value with passing parameters to apex class Database.insert in LWC | How to insert lookup field in salesforce using apex framework in LWC | Error handling in apex LWC | Exception handling in apex | Custom error message in LWCCreate a record on Contact to insert record Id of lookup field value with passing parameters to apex class Database.insert in LWC | How to insert lookup field in salesforce using apex framework in LWC | Error handling in apex LWC | Exception handling in apex | Custom error message in LWC

๐Ÿ‘‰ Get Complementary โ†’

right side banner -- www.w3webmart.com
 
  
๐Ÿ‘‰ Get Free Course โ†’

๐Ÿ“Œ Salesforce Administrators

๐Ÿ“Œ Salesforce Lightning Flow Builder

๐Ÿ“Œ Salesforce Record Trigger Flow Builder

๐Ÿ‘‰ Get Free Course โ†’

๐Ÿ“Œ Aura Lightning Framework

๐Ÿ“Œ Lightning Web Component (LWC)

๐Ÿ“Œ Rest APIs Integration



Vijay Kumar

Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Categories Salesforce LWC, Tutorial Tags Accounts and Contacts in Salesforce, Accounts Salesforce, Create multiple contacts in salesforce using a lwc example, How to pass multiple parameters in lwc, Relationship between account and Contact in Salesforce, Salesforce Contacts, Salesforce create many to many relationship, Understand Account and Contact Relationships, User contact relatedaccount
Create multiple Contacts in Salesforce Using a Lightning Web Component (LWC) Calling an Apex method Using Database.insert for bulk record insert and Displaying success & error messages
Add Multiple Rows button (Dynamic Contact Rows) Bulk Insert Contacts Related to Account using Database.insert() in LWC Salesforce

Archives โ†’

Categories โ†’

  • Agentforce (4)
  • Blog (4)
  • More (7)
  • Tutorial (326)
    • Formula Field (1)
    • JQuery (13)
    • Lightning Component (60)
    • React Js (15)
    • Salesforce Integration (41)
    • Salesforce LWC (115)
    • Trigger (57)
    • Validation Rule (9)
    • Visualforce (16)
  • Uncategorized (4)

Global Visitors โ†’

Popular Posts โ†’

  • Salesforce REST API Integration Using Postman โ€“ Complete Tutorial | Understanding How to Call Salesforce Web Service | Salesforce REST API: A Step-by-Step Guide | Tech W3Web 3.33 views per day
  • add page header or footer into a visualforce -- w3web.net How to make header and footer fixed and repeat on Visualforce page rendered as PDF in Salesforce | How to add header and footer in Visualforce page rendered as PDF in Salesforce 3 views per day
  • trigger to create account Automatically whenever contact is is created -- w3web.net Apex Trigger on Contact to Create Account Automatically and map to related account Id to Contact Whenever New Contact is Created in Salesforce | trigger to create account automatically whenever contact is created in salesforce 2.50 views per day
  • create lightning datatable row actions in lwc -- w3web.net Creating a Lightning Datatable with Row Actions and Display a Modal Popup on Click View Icon Button in Salesforce Lightning Web Component – LWC | how to create lightning datatable row actions in lwc 2.33 views per day
  • create form fields horizontally in lwc -- w3web.net How to align lightning-input form elements horizontally uses of slds-form-element_horizontal css and lightning-card tag in Lightning Web Component – LWC | How to create horizontal input label using slds-form-element/slds-form-element_horizontal... 2.33 views per day
  • Do not allow delete Account, if any of related Contact associated with Account in Salesforce | Prevent the deletion of Account which have related contact through apex trigger in Salesforce 2.33 views per day
  • display different types of toast message in lwc -- w3web.net How to display different types of custom toast message as Error, Success, Info and warning toast notifications on click button, uses of ShowToastEvent property in Salesforce Lightning Web Component — LWC | display custom toast notification in... 2 views per day
  • change stateful button value in lwc -- w3web.net How to change the state of button value as ‘follow/unfollow’ or ‘like/dislike’ through onclick handler method uses of ‘lightning-button-stateful’ tag elements in Salesforce LWC (Lightning Web Component) | How to c... 2 views per day

Get Free Courses โ†’

right side banner -- www.surfmyindia.com
right side banner -- overstockphoto.blogspot.com

Join Our Newsletter โ†’

Loading
right side banner -- www.lifedreamguide.com

Recent Posts โ†’

  • Create a hand-on example as pass input value from parent to child component through PubSub Model LWC in Salesforce
  • How to pass input value from parent to child component through LMS method in Salesforce | Hand-on example as pass input value from parent to child component through Lightning Message Service (LMS) in Salesforce
  • Apex Trigger Whenever Case is: Inserted/Updated/Deleted/Undeleted We need to count Cases based on Priority: High, Medium, Low And update counts on Account
right side banner -- www.w3webmart.com


header banner -- www.overstockphoto.blogspot.com
  • Follow Me โ†’
โžก : Facebook
โžก : Twitter
โžก : Linkedin
โžก : Instagram
โžก : Reddit
โžก : Forcetalks
โžก : Pinterest
โžก : Github
โžก : Medium
โžก : Tumblr
โžก : Telegram
 

๐Ÿงฉ Get Free Courses

๐Ÿ‘‰ Get Complementary โ†’

right side banner -- www.w3webmart.com
header banner -- www.overstockphoto.blogspot.com

Recent Posts โ†’

  • Create a hand-on example as pass input value from parent to child component through PubSub Model LWC in Salesforce
  • How to pass input value from parent to child component through LMS method in Salesforce | Hand-on example as pass input value from parent to child component through Lightning Message Service (LMS) in Salesforce
  • Apex Trigger Whenever Case is: Inserted/Updated/Deleted/Undeleted We need to count Cases based on Priority: High, Medium, Low And update counts on Account
  • Apex Trigger Whenever Opportunity is: Inserted/Updated/Deleted/Undeleted We need to: Calculate total Amount of all Opportunities Where StageName = ‘Closed Won’ And update it on Account
  • Apex Trigger Whenever Opportunity is Inserted/Updated/Deleted/Undeleted We need to count how many Opportunities have: StageName = ‘Closed Won’ And update that count on Account.
  • Apex Trigger Whenever Contact is Inserted/Updated/Deleted/Undeleted We need to count Contacts based on LeadSource and update Account, LeadSource values: Web, Phone Inquiry, Partner Referral, Advertisement
header banner -- www.lifedreamguide.com

Popular Posts โ†’

  • How to pass input value from parent to child component through LMS method in Salesforce | Hand-on example as pass input value from parent to child component through Lightning Message Service (LMS) in Salesforce 7 views
  • Create a hand-on example as pass input value from parent to child component through PubSub Model LWC in Salesforce 6 views
  • How to Navigate to External Web Page in LWC – Lightning Web Component | How to use Navigation Service (NavigationMixin.Navigate) to Navigate to External Web Page in LWC 3 views
  • create lightning-card container with custom tab in lwc -- w3web.net How to create lightning-card container with custom tab functionality uses a button in the actions slot where we are opening a lightning popup inside a custom form, displaying plain text in the footer slot in Salesforce LWC | How to apply lightning-card to create stylized container around a grouping of information to display different section like Header, Body, Footer and button actions with open a popup inside form in Salesforce LWC (Lightning Web Component) 3 views
  • Surfmyindia |
  • Lifedreamguide |
  • w3webmart |
  • Overstockphoto |
  • News24classictimes |
  • TechW3web |
  • Refund Policy |
  • Delivery Policy |
  • Privacy Policy |
  • Term & Conditions
© 2026 w3web.net • Built with GeneratePress

Chat Now