0

I'm trying to set the default value of lookup for

I'm getting the error when I try to set value:

var value = [{
type: 'Account',
id: "0014343234543",
label: "Related To"
}];
component.find("lookupaccount").get("v.body")[0].set("v.values",value);

My force:inputField is inside aura:if and on click of a button if condition sets to true and lookup component renders in the component.

Component:

<aura:if isTrue="{!v.isTrue}">
   <div class="slds-show slds-p-around--medium">
      <div class="slds-grid slds-wrap">
         <force:inputField aura:id="lookupaccount" value="{v.obj.AccountId}" />
</div>
</div>
</aura:if>

Controller:

buttonclick : function(component,event,helper){
   component.set("v.isTrue",true);
   var value = [{
    type: 'Account',
    id: "0014343234543",
    label: "Related To"
    }];
    component.find("lookupaccount").get("v.body")[0].set("v.values",value);

}
0

you have an aura:if and then force:inputField; hence your code is not working. Please try below.

<aura:if isTrue="{!v.isTrue}">
   <div class="slds-show slds-p-around--medium">
      <div class="slds-grid slds-wrap">
         <force:inputField aura:id="lookupaccount" value="{v.obj.AccountId}" />
      </div>
   </div>
</aura:if> 

Controller Code:

buttonclick : function(component,event,helper){
   component.set("v.isTrue",true);
   var populateAction = component.get('c.populateLookUp');
   $A.enqueueAction(populateAction); 
},
populateLookUp :function(component,event,helper){
var value = [{
    type: 'Account',
    id: "0014343234543",
    label: "Related To"
    }];
    component.find("lookupaccount").get("v.body")[0].set("v.values",value);
}

Please check if this works for you. You are trying to access something for which DOM is not ready yet. So, you need to call it asynchronously.

2
  • I’m still seeing undefined value – salesforce Developer Jan 20 '20 at 10:04
  • Instead of aura:if use css to show hide then the DOM will be defined. – Avijit Chakraborty Jan 20 '20 at 14:16

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.