How to Create a Record in Salesforce using Lightning Web Components and Apex class
Use lightning-input to get the user input for Account Name, Phone, Rating and Industry. Add lightning-button to call the JS controller method to create the record. Add onchange handler for each lightning-input tag to get the updated value in the JS controller.
1. Create HTML Templete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<lightning-card title="Account Record Create Form" icon-name="standard:account"> | |
<div class="slds-var-p-around_x-small"> | |
<lightning-input value={recAccount.Name} label="Account Name" onchange={handelNamechange}></lightning-input> | |
<lightning-input value={recAccount.Phone} label="Phone" onchange={handelPhonechange}></lightning-input> | |
<lightning-input value={recAccount.Rating} label="Rating" onchange={handelRatingchange}></lightning-input> | |
<lightning-input value={recAccount.Industry} label="Industry" onchange={handelinduschange}></lightning-input> | |
<br/> | |
<lightning-button label="Save Account Rec" onclick={createAccRec} variant="brand"></lightning-button> | |
</div> | |
</lightning-card> | |
</template> |
2. Create Javascript File.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LightningElement, track } from 'lwc'; | |
import NAME_FIELD from '@salesforce/schema/Account.Name'; | |
import PHONE_FIELD from '@salesforce/schema/Account.Phone'; | |
import RATING_FIELD from '@salesforce/schema/Account.Rating'; | |
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry'; | |
import accountRecMethod from '@salesforce/apex/AccountRecHelper.accountRecMethod'; | |
import { ShowToastEvent } from 'lightning/platformShowToastEvent'; | |
export default class AccountRecCreation extends LightningElement { | |
@track name = NAME_FIELD; | |
@track phone = PHONE_FIELD; | |
@track rating = RATING_FIELD; | |
@track industry = INDUSTRY_FIELD; | |
recAccount = { | |
Name : this.name, | |
Phone : this.phone, | |
Rating : this.rating, | |
Industry: this.industry | |
} | |
handelNamechange(event){ | |
this.recAccount.Name = event.target.value; | |
//console.log("name",this.recAccount.Name); | |
} | |
handelPhonechange(event){ | |
this.recAccount.Phone = event.target.value; | |
// console.log("phone",this.recAccount.Phone); | |
} | |
handelRatingchange(event){ | |
this.recAccount.Rating = event.target.value; | |
// console.log("email",this.recAccount.Rating); | |
} | |
handelinduschange(event){ | |
this.recAccount.Industry = event.target.value; | |
//console.log("industry",this.recAccount.Industry); | |
} | |
createAccRec() { | |
debugger; | |
accountRecMethod({ accRec : this.recAccount }) | |
.then(result => { | |
this.message = result; | |
this.error = undefined; | |
if(this.message !== undefined) { | |
this.recAccount.Name = ''; | |
this.recAccount.Industry = ''; | |
this.recAccount.Phone = ''; | |
this.recAccount.Rating= ''; | |
this.dispatchEvent( | |
new ShowToastEvent({ | |
title: 'Success', | |
message: 'Account created', | |
variant: 'success', | |
}), | |
); | |
} | |
console.log(JSON.stringify(result)); | |
console.log("result", this.message); | |
}) | |
.catch(error => { | |
this.message = undefined; | |
this.error = error; | |
this.dispatchEvent( | |
new ShowToastEvent({ | |
title: 'Error creating record', | |
message: error.body.message, | |
variant: 'error', | |
}), | |
); | |
console.log("error", JSON.stringify(this.error)); | |
}); | |
} | |
} |
3. Create Apex Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class AccountRecHelper { | |
@AuraEnabled | |
public static Account accountRecMethod(Account accRec){ | |
try { | |
insert accRec; | |
return accRec; | |
} catch (Exception e) { | |
throw new AuraHandledException(e.getMessage()); | |
} | |
} | |
} |
Like our post & bookmark this blog for your future learning.
Any suggestions and improvements are most welcome, please comment your suggestions and improvement in the comment box.
Happy Coding Sharing Is caring. 😀😀😀
Hi i am getting error in Apex: DML requires SObject or SObject list type: Account
ReplyDeleteList
ReplyDeleteLovely blog you haave here
ReplyDeletePost a Comment