0

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

1 Answer 1

-1

From what I see, the 3rd field is not yet added to the array. So the first thing you can try to do is this.fields.push(DEAL_FINANCE); After this I would suggest setting the value property to true for that field.

6
  • No its not working. I added as you suggested and tried to get the list of fields using console.log(JSON.stringify(updated_fields)); and it displays only the first 2 picklist fields. not able to add the 3rd field dynamically in {handleSubmit} method
    – AjaySFDC
    Commented Jan 25, 2022 at 9:54
  • Of course you don't see them. The objects you retrieve using the event.detail.fields are different than the ones you retrieve using this.fields. The diference is that the first has the values taken from what the user has populated. The second on is only the metadata without any value. Maybe try event.detail.fields.push(DEAL_FINANCE), but i'm not sure it will work Commented Jan 25, 2022 at 9:59
  • As per my requirement, i should not display the 3rd field to the end user and populate its value behind the scenes. The ways that you suggested and I had tried , neither is working.
    – AjaySFDC
    Commented Jan 25, 2022 at 10:06
  • ok, than can you post a snip of how the console.log(JSON.stringify(updated_fields)) is looking? Commented Jan 25, 2022 at 10:50
  • For the 2 fields ,( masking the finance information), it will look like this {"Picklist_A__c":"Green","Picklist_B__c":"JP Morgan"}
    – AjaySFDC
    Commented Jan 25, 2022 at 11:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .