Reference field on type array of number doesn't seem to work

Hi,

Following this documentation:

In a smart action I got a field that is an array of ids ['Number']:

{
    field: 'supplier',
    type: ['Number'],
    reference: 'suppliers.id',
    isRequired: false
}

I added the reference field so that instead of showing ids it shows the name of the supplier.

Instead of replacing the ids it breaks that field.

Cheers

Hi @mathieuh,

Array of references are currently not supported in smart action forms.

However, as a workaround if you are using v8, I think you should be able to achieve a similar result without the reference using hooks (load & change).

Eg. If you have a small number of suppliers (So a dropdown is enough to search through), something like

{
  actions: [{
    name: 'SmartActionExample',
    type: 'bulk',
    fields: [{
      field: 'suppliers',
      type: ['Enum'],
      isRequired: false,
    }],
    hooks: {
      load: async ({ fields }) => {
        const allSuppliers = await users.findAll();
        fields.find(f => f.field === 'suppliers').enums = allSuppliers.map(
          u =>  `${u.name} | ${u.id}`
        );
        return fields;
      }
    }
  }

If you have a bigger number of suppliers, something like this maybe?

{
    name: 'SmartActionExample',
    type: 'bulk',
    fields: [{
      field: 'supplier 1',
      type: 'Number',
      reference: 'users.id',
      isRequired: false,
      hook: 'onSupplierChange',
    }],
    hooks: {
      change: {
        onSupplierChange: async ({ fields }) => {
          fields.push({
            field: `supplier ${fields.length + 1}`,
            type: 'Number',
            reference: 'users.id',
            isRequired: false,
            hook: 'onSupplierChange',
          });
          return fields;
        }
      }
    }
  }

This is just a workaround though (So it is far from perfect from what you might expect). To be more explicit, handling Array of references is currently not supported since we don’t have widgets that allow such selection.

Still, I’ll also push this thread as a feature request in our product board.

Hi @jeffladiray,

No worries, I thought it might be a bug instead of a feature.

The 1st workaround should work fine in my case.

Cheers,

1 Like