There are couple of ways to create records with pre-defined values in lightning.
Use a lightning quick action (Create Record action type)
Main drawback : Doesn't support complex logic or un-related objects to set defaults. For more info about quick actions and pre-defined values
Using force:recordCreate
event
Main drawback: After saving the record, user is redirected to the record detail page.
Use force:recordData
(LDS)
Main drawback: Fields in the form has to be updated if object's fields are changed. Also each field type has to be handled differently (eg. Dates, Text, Numbers etc.)
Use lightning:recordEditForm
Main drawback: Currently because of this issue, child records in a master-detail relationship (Unless "Allow reparenting" checkbox on the master-detail relationship is checked) cannot be populated.
If the design allows you to set the "Allow reparenting" to true (Consider these before making a decision), You should be able to do something like below.
<aura:attribute name="fields" type="LIST" description="List of input field api names to be rendered on the form"/>
<lightning:recordEditForm
onsuccess="{!c.handleSuccess}"
objectApiName="Account">
<lightning:messages />
<aura:iteration items="{v.fields}" var="field">
<lightning:inputField fieldName="{field}" />
</aura:iteration>
<div class="slds-m-top_medium">
<lightning:button disabled="{!v.disabled}" variant="brand" type="submit" name="save" label="Save" />
</div>
</lightning:recordEditForm>
In the js controller, during initialisation, you need to set list of api names for all the fields you need to display on the form. This can be done in few ways.
- Hard code it and update it when you want to change manually.
- Write some apex to get all editable fields from a given page layout.
- Store Field api names in a custom meta-data/custom setting
Given that you want to stay on the same page and you have implemented your component as a lightning quick action, you can close the quick action on handleSuccess
method if you want to stay on the same page.
force:createRecord
event orforce:recordData
in order to pre-populate values. You cannot uselightning:recordEditForm
as well becs there's is bug preventing the creation of child records in master-detail relationship if the relationship is not reparantable.force:createRecord
does not allow to stay at my custom tab(or component) and is redirecting me every time I create a record to a record detail page. So, no use inforce:createRecord
. Usingforce:recordData
is not a good idea from my point of view, because the maintenance will be very terrible: every time I will update an object I will have to update manually the component withforce:recordData
as well.lightning:recordEditForm
. In which you could useonsuccess
event to handle the navigation. It is frustrating that Salesforce still hasn't fixed these little issues so that lighting component development can be smoother.lightning:recordEditForm
? Or provide an example, or maybe give a few references to articles, documentation etc.