The LWC app that I am currently working on has a feature that lets users fetch information for a record whose record ID is entered. I am looking forward to implementing a feature that proactively lists all the possible record ID(s) that the user is trying to enter as they start typing part of the ID. Autocomplete as we have it in say search engines is more or less what I am trying to implement.

The first approach that comes to my mind is to prefetch record IDs of all records in the org and then match on the frontend as the user types. However, that I believe might be a rather expensive query. What's the best way to go about implementing something like this, with efficiency in mind? Does Salesforce come with a feature out of the box that helps achieve the said task efficiently?

To put things more clearly, once I start typing an ID that starts with say 01234 it should list 0123456789, 0123476589, 0123490876, etc assuming the aforementioned strings are valid Salesforce IDs.

  • 3
    Fetch them based on the input from your user, with a four digit minimum. That last bit is crucial, otherwise you will match every Id in the system.
    – Adrian Larson
    Commented Jun 4, 2022 at 21:43
  • Does that mean if the user backtracks and starts typing a different id, related records need to be fetched again? Wouldn't this lead to too many SOQL queries? This is an interesting approach regardless. I'll go this way if further optimisation isn't possible. Commented Jun 4, 2022 at 22:39
  • 1
    Each query is a round trip from the server to the back end, returning a single query run per transaction. Also highly recommend using a timeout, so you only actually query after the user stops typing. There are lots of examples out there for this kind of implementation.
    – Adrian Larson
    Commented Jun 5, 2022 at 1:30


You must log in to answer this question.

Browse other questions tagged .