• Tyler Kauffman 9
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

I'm creating a lightning component that has input fields(name firstname,lastname, phone, email etc.) 

I have it so that it runs a sosl query on change for any of those fields. (runs a sosl on change of the input field) 

My question is - What do you consider the best practice on this? It seems to me that running a query on every keystroke can be very expensive in terms of api calls. I looked at google.com and saw they have a time delay. What do you guys suggest? I'll put a few code snippets below:

 

Component

<lightning:input onchange="{!c.inputChange}" aura:id="fname"  label="First Name" value="{!v.SelectedCustomer.FirstName}" />

JsController:
inputChange: function(component, event, helper) {
   var action = component.get('c.suggestAccountsContacts');
   
   action.setParams({
    'name' : component.get('v.SelectedCustomer').Name,
    'fName': component.get('v.SelectedCustomer').FirstName,
    'lName': component.get('v.SelectedCustomer').LastName,
    'phone': component.get('v.SelectedCustomer').Phone,
    'email': component.get('v.SelectedCustomer').PersonEmail
});
   
   action.setCallback(this, function(response) {

    var state = response.getState();
    if (component.isValid() && state === 'SUCCESS') {
        component.set('v.Customers', response.getReturnValue());
    } else {
        console.log('Failed with state: ' + state);
    }
});
   $A.enqueueAction(action);
},
Apex:
public static Object ... {
...
//Build filter string 

Return [FIND :filter  IN ALL FIELDS RETURNING Contact(Id, RecordTypeId, IsPersonAccount, AccountId, Name, Firstname, LastName, Email, Phone), ACCOUNT(ID, IsPersonAccount, RecordTypeId, Name, FirstName, LastName, PersonEmail ,PHONE)];

}