How to Create Account With Contact In LWC – Lightning Web Component | How to create account related contacts in Lightning web component – LWC

3,885 views


Hey guys, today in this post we are going to learn about How to Create Account With Contact In LWC – Lightning Web Component Salesforce.

 

Other related post that would you like to learn in Salesforce

 

Final Output β†’

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

Create Account With Contact In LWC -- w3web.net
 

 

You can download file directly from github by Click Here.

 
 

  • Find the below steps β–Ύ

 

Create Lightning Web Component HTML

Step 1:- Create Lightning Web Component : createConRelAcc.html

SFDX:Lightning Web Component >> New >> createConRelAcc.html

createConRelAcc.html [Lightning Web Component HTML]

  1.   <template>
  2.     <lightning-card title="How to Create Account With Contact In LWC" icon-name="standard:account">
  3.         <div class="slds-grid slds-wrap">
  4.             <div class="slds-col slds-col-size_12-of-12 slds-p-horizontal_medium slds-m-bottom_medium">
  5.               <lightning-input label="Name"
  6.                 onchange={handleNameChange} 
  7.                 value={accountName}
  8.                 name="accountName"
  9.                 class="slds-m-bottom_x-small"></lightning-input>
  10.               </div>
  11.  
  12.  
  13.               <div class="slds-col slds-col-size_12-of-12 slds-p-horizontal_medium slds-m-bottom_medium slds-p-top_medium">
  14.                 <lightning-button label="Save"
  15.                 variant="brand"
  16.                 onclick={saveAction}></lightning-button>
  17.               </div>
  18.            </div>   
  19.      </lightning-card>
  20. </template>

 

Create Lightning Web Component JavaScript

Step 2:- Create Lightning Web Component : createConRelAcc.js

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

SFDX:Lightning Web Component >> New >> createConRelAcc.js

createConRelAcc.js [LWC JavaScript File]

  1.    import { LightningElement, track } from 'lwc';
  2.  import { createRecord } from 'lightning/uiRecordApi';
  3.  
  4.  import { ShowToastEvent } from 'lightning/platformShowToastEvent';
  5.  import accObj from '@salesforce/schema/Account';
  6.  import accFld from '@salesforce/schema/Contact.AccountId';
  7.  import nameFld from '@salesforce/schema/Account.Name';
  8.  import conObj from '@salesforce/schema/Contact';
  9.  import conNameFld from '@salesforce/schema/Contact.LastName';
  10.  
  11. export default class CreateConRelAcc extends LightningElement {
  12.  
  13.     @track accountName;
  14.     @track accountPhone;
  15.     @track accountId; 
  16.     @track contactId; 
  17.  
  18.     handleNameChange(event){   
  19.         if(event.target.name == 'accountName'){
  20.             this.accountName = event.target.value;
  21.         }  
  22.  
  23.  
  24.  
  25.     }
  26.  
  27.  
  28.  
  29.  
  30.     saveAction() {
  31.         const fields = {};
  32.         fields[nameFld.fieldApiName] = this.accountName;
  33.         const accRecordInput = { apiName: accObj.objectApiName, fields};
  34.  
  35.         createRecord(accRecordInput)
  36.             .then(account => {
  37.                 this.accountId = account.id;
  38.  
  39.                 this.dispatchEvent(
  40.                     new ShowToastEvent({
  41.                         title: 'Success',
  42.                         message: 'Account created',
  43.                         variant: 'success',
  44.                     }),
  45.                 );
  46.  
  47.  
  48.                 const fields_Contact = {};
  49.                 fields_Contact[conNameFld.fieldApiName] = this.accountName + "'w3web contact";
  50.                 fields_Contact[accFld.fieldApiName] = this.accountId; 
  51.                 const recordInput_Contact = { apiName: conObj.objectApiName,
  52.                                               fields : fields_Contact};
  53.  
  54.                   createRecord(recordInput_Contact)
  55.                     .then(contact => {
  56.                         this.contactId = contact.id;
  57.                         this.dispatchEvent(
  58.                             new ShowToastEvent({
  59.                                 title: 'Success',
  60.                                 message: 'Contact created',
  61.                                 variant: 'success',
  62.                             }),
  63.                         );
  64.  
  65.                        this.accountName = ''; 
  66.                     })
  67.  
  68.             })
  69.             .catch(error => {
  70.                 this.dispatchEvent(
  71.                     new ShowToastEvent({
  72.                         title: 'Error creating record',
  73.                         message: error.body.message,
  74.                         variant: 'error',
  75.                     }),
  76.                 );
  77.             });
  78.     }
  79.  
  80.  
  81. }

 

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component : createConRelAcc.js-meta.xml

SFDX:Lightning Web Component >> New >> createConRelAcc.js-meta.xml

createConRelAcc.js-meta.xml [LWC Meta Data XML]

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

 

Further post that would you like to learn in Salesforce

 

 

 

FAQ (Frequently Asked Questions)

How do I set record ID in LWC?

Create a public recordId property by declaring it using @api decorator. @api recordId; If a lightning web component with an objectApiName property is used on a Lightning record page, then the page sets the API name of the object associated with the record being viewed.

How do I enable related contacts in Salesforce?

From Setup, enter Account Settings in the Quick Find box, then select Account Settings. Select Allow users to relate a contact to multiple accounts. You can use custom fields to capture unique information about relationshipsβ€”for example, the best time to call a contact.

How do I enable Lightning login for users?

From Setup, enter Session Settings in the Quick Find box, then select Session Settings. Review the default settings for Lightning Login. Make sure that Allow Lightning Login is enabled. You can disable Allow Lightning Login at any time to switch users back to username and password logins.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses β†’

πŸ‘‰ 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



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

Leave a Comment