Avoid Using Server side Call(Apex Class) using lightning/uiRecordApi.
For Toast in LWC use lightning/platformShowToastEvent.
ObjectAPIName can be Account/Contact/CustomObject__c
FieldAPIName can be Name (FieldAPIName).
Lwc_DemoComponent.js:
For Toast in LWC use lightning/platformShowToastEvent.
Example :
import OBJECT from '@salesforce/schema/ObjectAPIName';
import NAME_FIELD from '@salesforce/schema/ObjectAPIName.FieldAPIName';FieldAPIName can be Name (FieldAPIName).
Lwc_DemoComponent.js:
import { LightningElement, track} from 'lwc';
import { createRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import OBJECT from '@salesforce/schema/ObjectAPIName';
import NAME_FIELD from '@salesforce/schema/ObjectAPIName.FieldAPIName';
export default class Lwc_DemoComponent extends LightningElement {
isActive = true;
@track groupId;
name = '';
handleNameChange(event) {
this.groupId = undefined;
this.name = event.target.value;
}
createRecord() {
const fields = {};
fields[NAME_FIELD.fieldApiName] = this.name;
const recordInput = { apiName: OBJECT.objectApiName, fields };
createRecord(recordInput)
.then(taskTeam => {
this.groupId = taskTeam.id;
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Group created',
variant: 'success',
}),
);
})
.catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message: error.body.message,
variant: 'error',
}),
);
});
}
}
Lwc_DemoComponent.html:
<template>
<lightning-card title="GroupRecord" icon-name="standard:record">
<div class="slds-m-around_medium">
<lightning-input label="Id" disabled value={groupId}>
</lightning-input>
<lightning-input label="Name" onchange={handleNameChange}
class="slds-m-bottom_x-small"></lightning-input>
<lightning-button label="Create Group" variant="brand"
onclick={createRecord}></lightning-button>
</div>
</lightning-card>
</template>
Comments
Post a Comment