lightning:recordEditForm – Set or modify field values onsubmit

I have created a lightning component which allows the creation of a record in a custom object. I’ve used lightning:recordEditForm to capture the data, and to help the user it automatically populates some fields on the custom object in the background when they submit the form.

<lightning:recordEditForm objectApiName="Object__c" onsubmit="{!c.onRecordSubmit}" onsuccess="{!c.onRecordSuccess}">
    <lightning:inputField fieldName="Name" />
    <lightning:button variant="brand" label="Save" type="submit" />
</lightning:recordEditForm>

When the user clicks submit, my logic sets a field value in the background (Field__c in this case), and then on a successful save redirects to the new record.

onRecordSubmit: function(component, event, helper) {

    var eventFields = event.getParam("fields");
    eventFields["Field__c"] = "Test Value";
    event.setParam("fields", eventFields);
},
onRecordSuccess: function(component, event, helper) {
    //navigate to new record on successful save
    var navEvt = $A.get("e.force:navigateToSObject");
    navEvt.setParams({
        "recordId": event.getParam("response").id,
        "slideDevName": "detail"
    });
    navEvt.fire();
}

This was working fine until this week (I think today 19/07), when we noticed that the field values being set in javascript were no longer being persisted in the newly created record. Is this an issue with Salesforce?

Answer

This is not the correct api for setting form values via javascript. It happened to work before, but that was not the supported api. The correct way to do this is this:

<lightning:recordEditForm
     aura:id="myform"
     objectApiName="Object__c" 
     onsubmit="{!c.onRecordSubmit}" 
     onsuccess="{!c.onRecordSuccess}">
  <lightning:inputField fieldName="Name" />
  <lightning:button variant="brand" label="Save" type="submit" />
</lightning:recordEditForm>

controller:

onRecordSubmit: function(component, event, helper) {
  event.preventDefault(); // stop form submission
  var eventFields = event.getParam("fields");
  eventFields["Field__c"] = "Test Value";
  component.find('myform').submit(eventFields);
},

Attribution
Source : Link , Question Author : James , Answer Author : James

Leave a Comment