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 β
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]
<template>
<lightning-card title="How to Create Account With Contact In LWC" icon-name="standard:account">
<div class="slds-grid slds-wrap">
<div class="slds-col slds-col-size_12-of-12 slds-p-horizontal_medium slds-m-bottom_medium">
<lightning-input label="Name"
onchange={handleNameChange}
value={accountName}
name="accountName"
class="slds-m-bottom_x-small"></lightning-input>
</div>
<div class="slds-col slds-col-size_12-of-12 slds-p-horizontal_medium slds-m-bottom_medium slds-p-top_medium">
<lightning-button label="Save"
variant="brand"
onclick={saveAction}></lightning-button>
</div>
</div>
</lightning-card>
</template>
Create Lightning Web Component JavaScript
Step 2:- Create Lightning Web Component : createConRelAcc.js
SFDX:Lightning Web Component >> New >> createConRelAcc.js
createConRelAcc.js [LWC JavaScript File]
import { LightningElement, track } from 'lwc';
import { createRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import accObj from '@salesforce/schema/Account';
import accFld from '@salesforce/schema/Contact.AccountId';
import nameFld from '@salesforce/schema/Account.Name';
import conObj from '@salesforce/schema/Contact';
import conNameFld from '@salesforce/schema/Contact.LastName';
export default class CreateConRelAcc extends LightningElement {
@track accountName;
@track accountPhone;
@track accountId;
@track contactId;
handleNameChange(event){
if(event.target.name == 'accountName'){
this.accountName = event.target.value;
}
}
saveAction() {
const fields = {};
fields[nameFld.fieldApiName] = this.accountName;
const accRecordInput = { apiName: accObj.objectApiName, fields};
createRecord(accRecordInput)
.then(account => {
this.accountId = account.id;
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Account created',
variant: 'success',
}),
);
const fields_Contact = {};
fields_Contact[conNameFld.fieldApiName] = this.accountName + "'w3web contact";
fields_Contact[accFld.fieldApiName] = this.accountId;
const recordInput_Contact = { apiName: conObj.objectApiName,
fields : fields_Contact};
createRecord(recordInput_Contact)
.then(contact => {
this.contactId = contact.id;
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Contact created',
variant: 'success',
}),
);
this.accountName = '';
})
})
.catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message: error.body.message,
variant: 'error',
}),
);
});
}
}
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]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>55.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Further post that would you like to learn in Salesforce
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
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 |