Pre-populate field values in lightning:recordForm onload

After an extensive discussion with Salesforce support:

recordForm may not work with your use case, and the better approach here would be to use recordEditForm. This is only after discussing this with my team in our internal forum.

You had a concern that why it doesn't work with recordForm- to which I have mentioned that it will not work with recordForm, and would be better to use recordEditForm.

Currently, the doc does not mention anything about pre-populating fields. Like any other salesforce doc, it outlines facts, and customizations depend on the person developing the solution. Your use case is a customization.

I am absolutely not questioning your right to put forward this question. In fact, I understand why do you have this question in the first place. However, what matters here is you having a solution - and recordEditForm would be your best bet here. R&D will not consider changing the doc, as there is nothing mentioned about pre-populating fields there. Pre-populating fields would be a custom use case, and our doc gives general facts of how a recordEditForm works.

In a nutshell -->

This is not supported and you will want to use lightning:recordEditForm

FYI, my use case was pre-populating field values when laoding a recordForm as well. (more specifically, a lookup field, however text fields were also raised in the context of the case).

Case Number for Reference Purposes #19884436


You should be able to do this with an action.

However, let me answer your question. Yes, I have done this.

I set an aura:id and then used the javascript controller to set the value. of that field.

The issue that can happen is timing. Sometimes the onload script is ran before the component is created. I have seen this from time to time. My solution has been to call create the component in the javascript controller dynamically and set the value that way. A bit more cumbersome, but more likely to work consistently.


I was able to do this, albeit is extremely hack-y and I'm not sure how long it'll work but here's a quick example.

Component:

<aura:if isTrue='{!v.booleanVal}'>
    <lightning:recordForm aura:id='form' objectApiName='Lead' layoutType='Full' mode='edit' onload='{!c.onLoad}'/>
</aura:if>

Controller:

onLoad: function(component, event, helper){
    var record = event.getParam('record');

    // I've noticed this will fire multiple times. If you don't set some criteria 
    // to protect against it, this workaround will result in an infinite loop
    if(record !== undefined && !record.fields.FirstName.value){

        // I did not test if setting both value and displayValue attributes are required
        record.fields.FirstName.value = 'test';
        record.fields.LastName.displayValue = 'lastName';

        event.setParam('record', record);

        component.set('v.booleanVal', false);

        setTimeout($A.getCallback(function(){
            component.set('v.booleanVal', true);
        }), 1);
    }
}

Basically, this method seems to work only when you 'refresh' the form so-to-speak. I had this working previously with the e.force:refreshView event but that no longer appears to be working (which is why I've resorted to showing/hiding the form). I really don't like this solution but it does work. If there's a better way to do this, I'd love to hear from others.