5

For example, my component has the following section that displays a record form:

<template if:true={showRecordForm}>
    <lightning-record-form
        object-api-name={sObjectApiName}
        onsuccess={handleFormSuccess}
        record-type-id={recordTypeId}
        layout-type="Full"
        columns="2"
        oncancel={handleFormCancel}
        onload={handleFormLoad}>
    </lightning-record-form>
</template>

It will load the form for the object I specify in sObjectApiName, and with the specified record type as well. The problem is that I want to assign some values to specific fields.

Let us say that it is a Case form, and I want to assign an Account ID to the AccountId field, before the case is submitted to the platform.

I tried to query the lightning-input-fields, but with no success:

const inputFields = this.template.querySelectorAll(
    'lightning-input-field'
);
if (inputFields) {
    inputFields.forEach(field => {
        console.log(field)
    });
}

This is a snippet from the documentation of lightning-record-edit-form.

I'm starting to think that this might not be viable, or too much trouble. Is there a way to do this?

  • I think this can be done inside handlesubmit function. There is a example on the documentation. – javanoob Jun 22 at 0:42
  • But I don’t want to change the values on submit. I want fields to be set before the form is submitted. – Renato Oliveira Jun 22 at 1:07
1

You are trying to use edit form, why dont use - record-edit and try passing values

@track recordInfo = {
    fields: [
        { name: "AccountId", value: "001000000000000AAA" },
        { name: "Description", value: "Description of this record." }
    ]
}

get recordInfoDefined () {
    return this.recordInfo !== undefined && this.recordInfo.fields !== undefined
}
<lightning-record-edit-form
    object-api-name="Case">
    <template if:true={recordInfoDefined} for:each={recordInfo.fields} for:each="field">
        <lightning-input-field
            key={field.name}
            field-name={field.name}
            value={field.value}>
        </lightning-input-field>
    </template>
</lightning-record-edit-form>
  • I did end up using something like that yesterday. But instead of assigning the value directly to an attribute, I assigned it to an object that is the attribute. That way I can override/preload multiple fields without creating multiple attributes. Thanks for your answer. – Renato Oliveira Jun 25 at 14:16
  • Thank you. Can you add a snippet please. I wanted to understand how did you managed that - 'I assigned it to an object that is the attribute' – Anshul Agrawal Jun 25 at 14:40
  • Do you mind if I add the snippet to your post, so it will be in the actual answer instead of the question? :) – Renato Oliveira Jun 25 at 14:41
  • It's fine. Thanks – Anshul Agrawal Jun 25 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.