I'm new to Lightning Web Components development.
I have 3 fields to populate values, out of which 2 are picklist fields which requires user input, to which i use lightning-record-form
with "edit" mode and the other field should not be displayed to end user but should be populated automatically.
My sample code (adding only the necessary parts):
Deal.html
<template>
<lightning-quick-action-panel header="Finance">
<div class="slds-p-around_large">
<lightning-record-form record-id={recordId}
object-api-name={apiName}
columns=1
mode="edit"
fields={fields}
onsubmit={handleSubmit}>
</lightning-record-form>
</div>
</lightning-quick-action-panel>
</template>
Deal.js
/* eslint-disable no-console */
import { LightningElement , api} from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { CloseActionScreenEvent } from 'lightning/actions';
import DEAL_OBJECT from '@salesforce/schema/Deal__c';
import DEAL_A from '@salesforce/schema/Deal__c.Picklist_A__c'
import DEAL_B from '@salesforce/schema/Deal__c.Picklist_B__c'
import DEAL_FINANCE from '@salesforce/schema/Deal__c.Finance__c'
export default class Deal extends LightningElement {
@api recordId;
apiName = DEAL_OBJECT.objectApiName;
fields = [DEAL_A, DEAL_B];
handleSubmit(event){
// Stops the submit action
event.stopPropagation();
event.preventDefault();
let updated_fields = event.detail.fields;
console.log(JSON.stringify(updated_fields));
// Do validation checks
if(!updated_fields.Picklist_A__c){
this.myToastEvent("Error !!","Please enter a value","error");
}
else if(!updated_fields.Picklist_B__c){
this.myToastEvent("Error !!","Please enter a value","error");
}
else{
// If validation passes, I need to add the 3rd field - Checkbox to true and Save
/* I tried this code , but it did not work
//event.detail.fields.push({"Finance__c":true});
/* I tried this code , but it did not work
//this.fields[DEAL_FINANCE] = true;
/* I want to add this DEAL_FINANCE field without displaying it in lightning-record-form
this.template.querySelector('lightning-record-form').submit(updated_fields);
this.myToastEvent("Success !!","Deal Records updated successfully","success");
this.dispatchEvent(new CloseActionScreenEvent());
}
}
myToastEvent(mytitle,mymessage,myvariant){
this.dispatchEvent(
new ShowToastEvent({
title: mytitle,
message: mymessage,
variant: myvariant
})
)
}
}
META XML uses lightning__RecordAction
as target and ScreenAction
as targetconfig