I'm trying to set a default value on a lightning:inputField component. The fieldName references a User value. This is the code:

<lightning:recordEditForm aura:id="recordEditForm" objectApiName="Time__c" >
  <lightning:messages />
  <lightning:inputField aura:id="opportunity" fieldName="Opportunity__c" />
   <lightning:inputField aura:id="user" fieldName="User__c" />
   <lightning:inputField aura:id="description" fieldName="Description__c" />
   <lightning:button class="slds-m-top_small" type="button" label="Cancel" onclick="{!c.close}" />
   <lightning:button class="slds-m-top_small" type="submit" label="Save" />

In my controller, on the init function, I set the value of the user with the current user:

init : function(component, event, helper) {
   var userId = $A.get("$SObjectType.CurrentUser.Id");
   component.find("user").set("v.value", userId);

The problem is that the user in the user lightning:inputField is not displayed (the field is empty) even if the user is set when the object is created. I don't experiment this issue with the other lookup field (I've succeeded to set and display the opportunity field).

Could someone tell me what's wrong with this code?


Add an attribute in your cmp

<aura:attribute name="UserId" type="Id" />

Then add "value" field to your input field

<lightning:inputField aura:id="user" value="{!v.UserId}" fieldName="User__c" />

In your onInit function add

var a= component.get(v.UserId); var userId = $A.get("$SObjectType.CurrentUser.Id"); component.set(a,userId);

  • Thanks for ur answer. Unfortunately, that does not work. Still having an empty field displayed for user. – Sandrine Zecler Apr 18 '18 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.