0

Is it possible to preset a few fields in lightning:recordForm component?

For example, I have an object which has a master-detail field. And now I would like to have my lightning:recordForm being opened with already set value in the master-detail field on that object.

I covered the documentation over here and nothing is said about it. So, may I conclude that it is impossible? Or are there any workarounds?

4
  • I don't think it is possible. You may have to use force:createRecord event or force:recordData in order to pre-populate values. You cannot use lightning:recordEditForm as well becs there's is bug preventing the creation of child records in master-detail relationship if the relationship is not reparantable.
    – Ranga
    Commented Aug 15, 2018 at 14:36
  • @Ranga, 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 in force:createRecord. Using force: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 with force:recordData as well.
    – iloveseven
    Commented Aug 15, 2018 at 14:40
  • Yes, agreed. If your design allows that master-detail relationship can be reparantable, you could use lightning:recordEditForm. In which you could use onsuccess 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.
    – Ranga
    Commented Aug 15, 2018 at 14:49
  • @Ranga, could you, please, elaborate the idea of using the reparantable master-detail relationship in lightning:recordEditForm? Or provide an example, or maybe give a few references to articles, documentation etc.
    – iloveseven
    Commented Aug 15, 2018 at 14:54

1 Answer 1

4

There are couple of ways to create records with pre-defined values in lightning.

  1. 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

  2. Using force:recordCreate event
    Main drawback: After saving the record, user is redirected to the record detail page.

  3. 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.)

  4. 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.

5
  • Is not quick action just for mobile experience?
    – iloveseven
    Commented Aug 16, 2018 at 11:39
  • Edit you answer, please, to reflect the following problem. It is impossible to use a quick action in custom lightning component according to this question. So, it is not reasonable to add quick actions in the list above. Correct me if I am wrong.
    – iloveseven
    Commented Aug 16, 2018 at 11:53
  • @iloveseven lightning object specific quick actions are available in lightning experience (LEX) they can also be enabled in mobile experience as well. They won't appear on classic Salesforce experience though.
    – Ranga
    Commented Aug 16, 2018 at 12:17
  • I can not find any example which would use quick actions to display any kind of UI. Could you, please, help with this?
    – iloveseven
    Commented Aug 16, 2018 at 12:20
  • @iloveseven this trailhead trailhead.salesforce.com/en/modules/lex_migration_customization/… provides good overview of using quick actions. Also, to set predefined values, you can checkout the link I've added in the answer.
    – Ranga
    Commented Aug 16, 2018 at 12:56

You must log in to answer this question.

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