So I know that on mobile <lightning:inputField>
for lookup fields doesn't work. It's right there in the documentation (not clearly, but it's there if you know what to look for).
But if you check, you'll see that the standard Salesforce components do have a dropdown for selecting records in lookup field on a mobile device. (Try creating a new Contact on mobile and selecting an Account).
How do they do it?
Best Answer
Well, it looks like it's possible with
force:inputField
.But that requires a lot more effort on my part, client side and server side.
Markup
So what we're doing here is a few things:
lightning:recordEditForm>
based on whethereditRecordId
has a value. Tangential, but still important.default
for theforce:inputField
to refer to. A common mistake to forget that.force:input
field, so we can dynamically set the value inside theeditRecordForm
.display: none
) so that they will not disappear from the DOM and cause problems.Controller
So we have two functions there:
1.
doInit
which retrieves the values required if we are editing the form (Apex code to follow). Notice here that in the callback function we are doing a lot of work to make the value of the field from the database visible to the user (in the case of edit).2.
lookupChanged
which sets the value of the field inside thelightning:recordEditForm
so that thesubmit
button will work (this is why we are using CSS to hide thatinputField
, so that our code can set its value).Apex controller
EDIT:
After additional poking and prodding, it turns out that some of the code in the Javascript controller might be redundant. I commented it out, because in some cases you might need/want it.
Think carefully if you need it though,
component.find
is probably a very expensive function call, much more so thancomponent.get
.