A typical use case when creating records is to populate some fields in advance, and then let the user fill in the rest of required fields. Let’s say that we want to create account records, having a pre-populated description, like this:
If you want to do this in a Lightning Component while using lightning:recordEditForm, you could do it the next way:
<aura:component implements="flexipage:availableForAllPageTypes" access="global" > <lightning:card title="Add account"> <div class="slds-p-around_medium"> <lightning:recordEditForm objectApiName="Account" aura:id="createAccountForm" onsubmit="{!c.handleSubmit}"> <lightning:messages/> <lightning:inputField fieldName="Name"/> <lightning:inputField fieldName="Phone"/> <div class="slds-p-vertical_x-small"> <dt class="slds-item_label slds-text-color_weak slds-truncate">Description</dt> <dd class="slds-item_detail slds-truncate">This is a default description</dd> </div> <lightning:inputField fieldName="Website"/> <lightning:button type="submit" name="Submit" label="submit" class="slds-m-top_medium"/> </lightning:recordEditForm> </div> </lightning:card> </aura:component>
As you can see, I am using some SLDS styles to show a static text for Description field. You could use an attribute instead and set it from the controller with the value you want to pre-populate.
This is the controller:
({ handleSubmit : function(component, event, helper) { event.preventDefault(); // Prevent default submit var fields = event.getParam("fields"); fields["Description"] = 'This is a default description'; // Prepopulate Description field component.find('createAccountForm').submit(fields); // Submit form } })
What we are doing is to intercept the event, and set a value for the Description field, which by default was not included in the Javascript object.
Thanks to Stephen Woods for the support.
How you are calling handleSubmit function?
LikeLike
In the recordEditForm component there is an attribute to which you can pass an on submit handler function: onsubmit=”{!c.handleSubmit}”
LikeLike
I want to pre-populate the value before submit and make required field not editable
LikeLike
You can make fields non editable setting the “disabled” property on the input field: https://developer.salesforce.com/docs/component-library/bundle/lightning:inputField/specification.
If you want to set a value in advance, just use the “value” property too.
LikeLike