function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Julien SalensonJulien Salenson 

lightning:recordeditform is not populating lookup fields

Hi,

I have created this component :
<lightning:recordEditForm aura:id="edit" recordId="{!v.recordId}" 
                          objectApiName="Account" 
                          > 
    <lightning:inputField fieldName="Sales_DT__c" variant="label-hidden"/>
</lightning:recordEditForm>

Where Sales_DT__c is a lookup on User.
This field Sales_DT__c  has a value recorded : 00530000004FGcfAAG.

Before, it's was working with this old workaround : add lightning:recordViewForm in the same compoent, but it's doesn't work anymore :
<lightning:recordViewForm recordId="{!v.recordId}" 
                              objectApiName="Account" >
        <div class="slds-hidden">
            <lightning:outputField fieldName="Sales_DT__c" />
        </div>
    </lightning:recordViewForm>

I have already read this : https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=9060G0000005qTaQAI

But, I didn't found a good response.

Any idea on how to populating this lookup field when I load the component ?
 
{tushar-sharma}{tushar-sharma}
Can you try to populate field like this
component.find('LastName').set('v.value', 'Tushar Sharma');

You can find complete code here: https://newstechnologystuff.com/2018/10/06/lightningrecordeditform-save-data-without-apex/

If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
Julien SalensonJulien Salenson
Hi Tushar,

But how to populate it with his current value (instead of 'Tushar Sharma') ?

Thank you
Julien SalensonJulien Salenson
Hi,

I have contacted SF support and they gave me 2 workarounds : 
1)For the lookup field to be populated from lightning:recordEditform, the field MUST first be included in the page layout of the object.

2) If you don't want to add it in the page layout, well it's a know issue :
https://help.salesforce.com/articleView?id=000349125&language=en_US&type=1&mode=1

Below is the sample code i they gave me created and I tested it : 
Lightning Component : 
----------------------------------
<aura:component controller ="LookupBug" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="public" >
    
    <lightning:recordEditForm aura:id="edit"  recordId="{!v.recordId}"
                              objectApiName="Account" onload="{!c.handleLoad}" onsuccess = "{!c.showToast}">
        <lightning:inputField aura:id = "nameField" fieldName="Payments__c"  />
        <lightning:inputField fieldName="Name" />
        <lightning:inputField fieldName="Industry" />
        <lightning:inputField fieldName="Phone" />
        
       
        <div class="slds-align_absolute-center slds-m-top_medium"> 
            <lightning:button  variant="brand" type="submit" name="save" label="Save" />
        </div>
    </lightning:recordEditForm>   
   
</aura:component>

Controller : 
-----------------
({
  
    handleLoad: function (cmp, event, helper) {
         var action = cmp.get('c.getAccountResults');
        action.setParams(
            {
            "accountId" : cmp.get("v.recordId")
            }
        );
        action.setCallback(this, function(response) {
            cmp.find("nameField").set("v.value", response.getReturnValue().Payments__c);

        });
        $A.enqueueAction(action);
        
    },
    
     showToast : function(component, event, helper) {
    var toastEvent = $A.get("e.force:showToast");
    toastEvent.setParams({
        "title": "Success!",
        "message": "The record has been updated successfully."
    });
         toastEvent.fire();}
})



Apex controller :
------------------------
public class LookupBug {
    @AuraEnabled
    public static Account getAccountResults(String accountId)
    {
        return [Select Id,Payments__c from Account where Id =: accountId];
    }
}



Notes :  

>> I have created a lookup field "Payments__c"  on Account object ,  which refers to custom object "Payments__c".
>> On the lightning component you can see in the lightning:recordEditForm form you have an aura:id on payments field.
>> On the Client Controller  in method onLoad , we call an apex controller to get the values of the lookup fields and populate them to the respective fields using aura:id.

>> onLoad function is called from onload  attribute call of   lightning:recordEditForm (i.e handleLoad method).