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
Bryan Leaman 6Bryan Leaman 6 

Unexplained error with Lightning RecordEditForm lookup field

I have a record edit form that works for me, but "rarely" works for another user (I'm logging in as that user to test this) -- but here's the kicker -- *until* they go do other stuff and come back later.

The lightning component consists of a lightning:recordEditForm that has several fields including about 4 lookup fields (Account, Contact, managed obj Vehicle Inventory and managed obj Location). One of the lookups is to a custom object, and gets a default value based on the user's settings. When creating a new record, the default value shows up properly. The record is saved and that location lookup value is in the record -- it's visible in the "Recent items" related list.  But when this user re-opens the record that value isn't there anymore. 

When the value will NOT display on-screen I'm seeing an error 4 times in my Chrome consols: "Cannot read property 'color' of undefined.'  This error occurs *between* the completion of my "init" handler and the start of the RecordEditForm's onload method. The "setDefaultLocation" routine runs correctly when initiating a new record, but does not run (and shouldn't) when displaying an existing record with the value specified.

I've verified that the user has read & edit authority on all fields in the object and can read the target object of the lookup field. And again, if I log off as this user, then log in as this user again (or just go on to other things and come back later), then the lookup field is properly displayed.

I think the full code is too much for the forum, but here are some snippets:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:appHostable,lightning:actionOverride"
    controller="RentalAgreementLex">
...
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
...
<lightning:recordEditForm recordId="{!v.recordId}" objectApiName="dealer__Rental_Agreements__c"
    aura:id="newrecordform" onsuccess="{!c.recordSaved}" onerror="{!c.recordSaveError}"
    onload="{!c.pageload}">
...
            <lightning:inputField fieldName="Location__c" disabled="{!v.alreadyposted}" aura:id="location"
                required="true" readonly="{!v.alreadyposted}" class="mw_required"/>


Some of the controller code:
/* general page initializations */
    doInit : function(component, event, helper) {
        console.log('doInit');
        // initialize that we're not in the process of posting
        component.set("v.posting", false);
...
        console.log('doInit complete');
    },
    /* initializations when recordeditform is loaded */
    pageload: function(component, event, helper) {
        console.log('pageload');
        let id = component.get("v.recordId");
        console.log(id);
        let sts = (id==null) ? null : component.find("agreement_status").get("v.value");
        if (id==null) {
            sts = 'Open';   // default value
            let stscmp = component.find("agreement_status");
            if (stscmp!=null) stscmp.set("v.value", sts);
            component.set("v.status", sts); // also init record data status
            component.find("deposit_amount").set("v.value",0.00);   // default value
            component.find("excess_miles_charge").set("v.value",0.00);  // default value
            component.set("v.title","New Rental Agreement");
            component.find("totalperdiemtax").set("v.value",0.00);  // default value
            component.find("sales_tax").set("v.value",0.00);    // default value
            component.find("countysalestax").set("v.value",0.00);   // default value
            component.find("citysalestax").set("v.value",0.00); // default value
            component.find("thirdtierrentaltax").set("v.value",0.00);   // default value
        }
        // in doInit: component.set("v.title", name);
        let isopen = sts!='Paid';
        let posting = component.get("v.posting");
        console.log('isopen = ' + isopen);
        // enable/disable buttons based on rental status
        if (!posting && isopen) component.find("submitbutton").set("v.disabled", false);
        if (!posting && id!=null && isopen) component.find("postbutton").set("v.disabled",false);
        let fromaccount = component.get("v.fromaccount");
        if (fromaccount!==null) {
            component.find("account").set("v.value",fromaccount);
            helper.rtvPersonContactId(component, event);
        }
        let fromcontact = component.get("v.fromcontact");
        if (fromcontact!==null) {
            component.find("contact").set("v.value",fromcontact);
            helper.rtvContactsAccountId(component, event);
        }
        if (isopen) component.set("v.alreadyposted", false);
        else component.set("v.alreadyposted", true);
        // set default location
        let c_location = component.find("location");
        let location = null;
        if (c_location!=null) location = c_location.get("v.value");
        if (id==null && location==null) helper.rtvUserDefaultLocation(component, event);
        // initialize running totals
        helper.recalcTotals(component, event);
        console.log('pageLoad complete');
    },
 
Best Answer chosen by Bryan Leaman 6
Bryan Leaman 6Bryan Leaman 6
Okay, for anyone else who might stumble on this post with a similar issue ... It appears that adding the lookup field to the object's page layout fixes this issue. (OMG! -- Why should this component rely on page layouts??? And why would the values start showing up later on???)  

Anyway, I was clued in to this as a possible solution from stackexchange:  https://salesforce.stackexchange.com/questions/215218/lightningrecordeditform-not-populating-lookup-fields  

--Bryan

All Answers

Bryan Leaman 6Bryan Leaman 6
Okay, for anyone else who might stumble on this post with a similar issue ... It appears that adding the lookup field to the object's page layout fixes this issue. (OMG! -- Why should this component rely on page layouts??? And why would the values start showing up later on???)  

Anyway, I was clued in to this as a possible solution from stackexchange:  https://salesforce.stackexchange.com/questions/215218/lightningrecordeditform-not-populating-lookup-fields  

--Bryan
This was selected as the best answer
Francesco Carmagnola 10Francesco Carmagnola 10
I still see the same issue, my lookup field isn't displayed properly. Adding the field to the standard page layout fixes it. I feel that Salesforce should work on this bug.
sumit kumar 49sumit kumar 49
I'm also same facing same isuue. i  created same component  on acse object (Recordeditform with  accountId and ContactId field) and deployed in my personal developer org and in my sandbox .. i see diffrence in behaviour  though these fields are present on  both places/pagalyouts.

1. in my personal org contactId is working fine   - it can search the contact in the lookup as std factionality.
2. In Sandbox.. Contact name doesnot display any value in the look up field , so no look up value is found for contact. 

tried searching many article but could not solve this issue.. dont know why its coming like this..

i also shecked the field accessibility - its editable for the contact name field
Account Id works fine at oth org. problem is with contactId.. 

could any1 help me on this ? thanks!!

 
Madhura KurdukarMadhura Kurdukar
Hi,
I am also facing issue with lookup for ContactId  on Case Edit Page. I am using lightning:recordEditForm to edit a Case record and lightning:inputField to display AccountId and ContactId fields. 
The lookup for AccountId works fine however ContactId does not show or search the contact.
I have checked the field accessibility and security settings. The field is accessible and editable. Still the ContactId lookup is not working.
Please suggest. 

Please let me know if there any workaround?

Thanks !!!
Bryan Leaman 6Bryan Leaman 6
Sumit and Madhura,
If the solution on this thread doesn't solve your issue you might be better off starting a new thread of your own.  Adding the lookup field to the page layout solved my issues, so I have nothing else to test with at the moment to see if there are other possible solutions to other issues.
--Bryan
Fred13Fred13
Bryan, thanks so much for posting your solution!  I spent a LONG time trying to figure out what is  going on..  Thanks!!!!