Looking up an id in an autocomplete form sometimes returns many more unrelated results

Feature(s) impacted

In a smart action, I’m using a Collection field for users to lookup an id in an other table. But some ids returns so many results that the id being searched does not appear.
Here is the setup of the field :
{
label: “catalogueItemIdKey”,
description: “ID Parcours par apprenant”,
type: “Collection”,
collectionName: “catalogueItem”,
isRequired: true,
},

Observed behavior

I can see this network call being made :
curl ‘https://forest-backend-edumiam.herokuapp.com/forest/catalogueItem?timezone=Europe%2FParis&context[relationship]=BelongsTo&context[field]=catalogueItemIdKey&fields[catalogueItem]=catalogueItemId&page[number]=1&page[size]=100&search=50709&searchExtended=0&searchToEdit=true
-H ‘Accept: application/json’
-H ‘Accept-Language: en-GB,en-US;q=0.9,en;q=0.8’
-H ‘Authorization: Bearer TOKEN’
-H ‘Connection: keep-alive’
-H ‘Content-Type: application/json’
-H ‘Origin: https://app.forestadmin.com
-H ‘Sec-Fetch-Dest: empty’
-H ‘Sec-Fetch-Mode: cors’
-H ‘Sec-Fetch-Site: cross-site’
-H ‘Sec-Fetch-Storage-Access: active’
-H ‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36’
-H ‘sec-ch-ua: “Not;A=Brand”;v=“99”, “Google Chrome”;v=“139”, “Chromium”;v=“139”’
-H ‘sec-ch-ua-mobile: ?0’
-H ‘sec-ch-ua-platform: “macOS”’

Expected behavior

It should return exactly this id

Failure Logs

Context

  • Project name: forest_backend
  • Team name: All teams
  • Environment name: All environments
  • Database type: Supabase
  • Recent changes made on your end if any: none

And, if you are self-hosting your agent:

  • Agent technology: nodejs
  • Agent (forest package) name & version (from your .lock file): 1.60.0

Hi,

This autocompletion is based on the basic list search feature.
It’s not an exact match; it targets all fields in the collection using contains.

Instead of this, you can customize the search for this specific case (doc link).
The request context contains the query param “searchToEdit“, which is true only when the search comes from an action form.
Take care to create a “id===term” search in your case, and to return the default search filter otherwise.

Best Regards

Thanks for your answer.
How can I set up this field in this action form to use this custom search with an id===term like you suggest?
Thanks again

Hi,

Can you please confirm your use case is the following:
I want to lookup an id from the user collection, when I type “1234”, I want the user collection to be searched against “id===1234” so the 1234 id is displayed in the autocomplete. This is to confirm the id is correct. No other field from the user collection is used.

After discussion with the tech team, a better approach is the following:

collection.addAction('select user', {
      scope: 'Single',
      form: [{
        type: 'Number',
        label: 'User',
        widget: 'Dropdown',
        search: 'dynamic',
        options: async (context, searchValue) => {
          const ops = await context.dataSource.getCollection('user').list({ conditionTree: { field: 'id', operator: 'Equal', value: searchValue }}, ['id']);
          return ops.map(op => op.id);
        }
      }],
      execute: (context, resultBuilder) => {
        return resultBuilder.success(`select user ${context.formValues.User}`);
      }
    })

Please validate it in staging or dev.

Best regards