Create-parent-child-records in Lightning WebComponent
Apex Class
==========
public class AccountController{
@AuraEnabled
public static Account createAccount(Account acc){
try{
insert acc;
}
catch(Exception e){
System.System.debug('unable to insert the record due to'+e.getMessage());
}
return acc;
}
@AuraEnabled
public static void createOppty(Opportunity opp,String accId,String opptyStageName){
System.debug('***AccountId***'+accId);
try{
opp.AccountId=accId;
opp.closeDate=System.today();
opp.StageName=opptyStageName;
insert opp;
}
catch(Exception e){
System.System.debug('unable to insert the record due to'+e.getMessage());
}
}
}
createRecords.html
==================
<template>
<template if:false={isShow}>
<lightning-input type="text" label="AccountName" class="accName" onchange={handleChangeName}>
</lightning-input>
<lightning-input type="text" label="AccountSite" class="accSite" onchange={handleChangeSite}>
</lightning-input>
<lightning-button label="Save" variant="brand" onclick={handleSave}>
</lightning-button>
</template>
<template if:true={isShow}>
<lightning-input type="text" label="Name" class="oppName" onchange={handleChangeOppName}>
</lightning-input>
<lightning-button label="Save" variant="brand" onclick={saveOppRecord}>
</lightning-button>
</template>
</template>
createRecords.Js
================
import { LightningElement, track, api } from 'lwc';
import createAccount from '@salesforce/apex/AccountController.createAccount';
import createOppty from '@salesforce/apex/AccountController.createOppty';
import Name_FIELD from '@salesforce/schema/Account.Name';
import Site_FIELD from '@salesforce/schema/Account.Site';
import OpptyName_FIELD from '@salesforce/schema/Opportunity.Name';
import { NavigationMixin } from 'lightning/navigation';
export default class CreateRecords extends NavigationMixin(LightningElement) {
@api accErrorMessage;
@api oppErrorMessage;
@api isShow=false;
@api accountId;
@api accRecord={
Name:Name_FIELD,
Site:Site_FIELD
}
@api oppRecord={
Name:OpptyName_FIELD
}
handleChangeName(){
this.accRecord.Name=this.template.querySelector(".accName").value;
}
handleChangeSite(){
this.accRecord.Site=this.template.querySelector(".accSite").value;
}
handleChangeOppName(){
this.oppRecord.Name=this.template.querySelector(".oppName").value;
}
handleSave(){
createAccount({acc:this.accRecord})
.then(account => {
this.accountId=account.Id;
this.accRecord={};
console.log('@@@AccountId@@@'+this.accountId);
if(this.accountId!=''){
this.isShow=true;
}
})
.catch(error=>{
this.accErrorMessage=error;
console.log('unable to insert the record due to '+JSON.stringify(this.accErrorMessage));
});
}
saveOppRecord(){
createOppty({
opp:this.oppRecord,
accId:this.accountId,
opptyStageName:'Closed Won'
})
.then(()=>{
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId:this.accountId,
objectApiName: 'Account',
actionName: 'view'
},
});
})
.catch((error)=>{
this.oppErrorMessage=error;
console.log('unable to insert the record due to '+JSON.stringify(this.oppErrorMessage));
})
}
}
LightningApplication
====================
<aura:application extends="force:slds">
<c:createRecords>
</c:createRecords>
</aura:application>
createRecords.js-meta.xml
=========================
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
<apiVersion>47.0</apiVersion>
<isExposed>false</isExposed>
</LightningComponentBundle>
Comments
Post a Comment