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
Venkat B 33Venkat B 33 

Custom lookup component in Lightning (How to validate this field)

Hi All,

I am new to lightning. I implemented a custom lookup component in lightning and i embed it into another component. Now my requirement is need to validate that lookup field. If the lookup field empty an error message should display reference code:   

<div class="slds-size_8-of-8" >
    <label class="slds-float_left"><span style="color:red;" align="left">*</span>Account Name: </label>
               <c:customLookup objectAPIName="contact" IconName="standard:contact" selectedRecord="{!v.selectedLookUpRecord}" label=""/>
</div>
                                                        
                                                       
Best Answer chosen by Venkat B 33
sfdcMonkey.comsfdcMonkey.com
Hi Venkat,
 when you click on your save button first validate the selectedLookUpRecord attribute id if it is undefined it's mean your lookup don't have any value and in this case you can show a alert msg to user :
here is the sample blueprint :
 
<aura:attribute name="selectedLookUpRecord" type="contact"/>
<div class="slds-size_8-of-8" >
    <label class="slds-float_left"><span style="color:red;" align="left">*</span>Account Name: </label>
               <c:customLookup objectAPIName="contact" IconName="standard:contact" selectedRecord="{!v.selectedLookUpRecord}" label=""/>
</div>

<lightning:button onclick="{!c.save}" label=" Save record">

javaScript controller :
 
save : function(component,event,helper){
   if(helper.validateFields(component,event)){
      //your save work goes here...
   }
},

javaScript helper method
validateFields: function(component,event){
  var isValid = true;
   // get lookup value 
   var lVal = component.get("v.selectedLookUpRecord");  
   if(lVal.Id == undefined){
      isValid = false ;
      alert('Lookup field is required...Please select contact');
   }
   return isValid;
},
Thanks, hopes it will helps you
 

All Answers

sfdcMonkey.comsfdcMonkey.com
Hi Venkat,
 when you click on your save button first validate the selectedLookUpRecord attribute id if it is undefined it's mean your lookup don't have any value and in this case you can show a alert msg to user :
here is the sample blueprint :
 
<aura:attribute name="selectedLookUpRecord" type="contact"/>
<div class="slds-size_8-of-8" >
    <label class="slds-float_left"><span style="color:red;" align="left">*</span>Account Name: </label>
               <c:customLookup objectAPIName="contact" IconName="standard:contact" selectedRecord="{!v.selectedLookUpRecord}" label=""/>
</div>

<lightning:button onclick="{!c.save}" label=" Save record">

javaScript controller :
 
save : function(component,event,helper){
   if(helper.validateFields(component,event)){
      //your save work goes here...
   }
},

javaScript helper method
validateFields: function(component,event){
  var isValid = true;
   // get lookup value 
   var lVal = component.get("v.selectedLookUpRecord");  
   if(lVal.Id == undefined){
      isValid = false ;
      alert('Lookup field is required...Please select contact');
   }
   return isValid;
},
Thanks, hopes it will helps you
 
This was selected as the best answer
Venkat B 33Venkat B 33
Hi Piyush,

It's working thanks for your quick responce.
simmi kumarisimmi kumari
Hi Venkat B 33,

Could you please share your code(.cmp, .js, .apxs) for the custom lookup lightning component on this thread. I am new to Salesforce development too and really need to create a contact detail page with a custom lookup search for Accounts.

Thanks in advance.
Surya PSurya P
Hi,

I'm also working on the same requirement. I have called the lookup component in another component, but saving the record function is not working. Kindly share me the code for saving the record (with lookup as a field in parent component).

Thanks & Regards
Surya