16

I'm using lightning:recordForm to display create view for a custom sObject. It works as expected, but I cannot find a way to pre-populate some of the fields for create view, e.g. lookups. Documentation says that I can use onload action to modify loading behaviour, but it doesn't specify how.

I would really like to use this component, instead of creating a custom component based on the lightning:recordEditForm, with dynamically created and populate input fields.

So basically the question is: is there a way to pre-set some of the field values upon object creation through lightning:recordForm?

4
  • Is there a reason you are using lightning:recordForm? you can easily pre-populate fields values using quick actions.
    – Ricky
    Jun 27, 2018 at 12:51
  • Ricky, I'm using lightning:recordForm, because I need users to create records within a custom records creation process. If the user is creating a record, which is a child to another record, I want to pre-populate the lookup value on the child record. Jun 27, 2018 at 13:05
  • The onLoad event refers to when the recordForm loads an existing record from the database, not when the component itself loads. Aug 20, 2018 at 4:48
  • @AlexFisher Have you referred force:createRecord? Looking at your question, it looks like you can use this event to create custom records with pre-populated fields.
    – Jayant Das
    Aug 20, 2018 at 16:50

5 Answers 5

7
+50

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

0
2

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.

4
  • Lol, the question is not about js, but you could write record instead of record !== undefined. And I am sorry even if it works what you provided above is really very dangerous. I think we should wait for someone from the Salesforce team to address the issue. Thank you for the attempt anyway.
    – iloveseven
    Aug 16, 2018 at 17:47
  • And by the way. How should I go about with your approach if I want to set a master-detail field? I tried to use id value of the master object and the value like: {Id: idOfTheMasterObjectGoesHere}. And it did not work, the master field just do not get displayed at all and after I hit the Save button the record is not saved.
    – iloveseven
    Aug 16, 2018 at 18:14
  • @iloveseven You're not wrong, I acknowledge that it's a bad solution and I would be wary about using it in a production environment. That said, I haven't tested this in that scenario, more specifically, setting lookups/master-detail relationships so I can't provide much direction there. Aug 16, 2018 at 19:01
  • Nice hack! It may break in the future, but I like the out of the box thinking here.
    – willard
    Dec 5, 2018 at 21:35
1

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.

1
  • 5
    but what action should I invoke, or what attribute should I set to pre-popualte the fields? I mean the documentation has zero info about inner lightning:recordForm attribute, that actually holds the record with all the field values. Jun 28, 2018 at 16:21
1

I think I found some useful information here. There's an excellent blog by Peter Knolle that addressed Lightning recordEditForm, but a lot of it seems to be working with recordForm as well.

http://peterknolle.com/lightning-record-edit-form/

Basically, he shows you how to use event.preventDefault() during the onsubmit action. In this example he overrides the standard submit action and then populated/overwrites particular field values. I just did a quick test and it, at minimum, will let you populate field values before the record is saved. Might also be useful during the onload action, but I will have to do more testing and post updated. His code -->

handleOnSubmit : function(component, event, helper) {
event.preventDefault();
var fields = event.getParam("fields");
fields["AccountId"] = component.get("v.parentId");
component.find("form").submit(fields);
}
1
  • Note that this same sort of thing is already shown in the Salesforce documentation. My problem is that I want to populate a MasterDetail field using the context where I open the form and that being a required field would probably not let me get in during the onsubmit handling (though this is something I am going to test to verify).
    – Phil W
    Aug 7, 2019 at 12:27
0

You have to do custom form loading e.g using onload="{!c.yourFunction}". In your function you can populate field like that:

handleOnLoad : function(component, event, helper){
    var objectInfo = event.getParams().objectInfos;
    objectInfo["Object ApiName you are creating"].fields['Field api name of that object'] = 'Value of your field';

},

And Then you have to pass back these populated values to your recordForm. Couldn't find a way to do this. The documentation is bad.

4
  • unfortunately, this is not working. event.getParams().objectInfos contains info about the layout. There's indeed a parameter inside eventparams - record. This parameter seems to be responsible for system pre-populated fields. event.getParams().record.fields contains RecordTypeId and CurrencyIsoCode and other system pre-populated fields as well as it contains all the custom fields and record's values. However, changing them doesn't seem to be affecting what is displayed to the user. Jun 29, 2018 at 13:52
  • You are right. I have been trying to make it work for all day today. So far have not found the solution.
    – RSICK
    Jun 29, 2018 at 14:09
  • Thank you for your effort. Guess I'll just use lightning:recordEditForm and dynamic field creation, while Salesforce will update their documentation. Jun 29, 2018 at 14:13
  • Thats unfortunate yes. I think that there has to be a method to send back the modified data to lightning:recordForm. For example if you want to change field before submitting the form you do the same thing event.params('fields')[Field] = .. And after that you call form.submit().
    – RSICK
    Jun 29, 2018 at 14:23

You must log in to answer this question.

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