Smart collection hooks load record

This topic follows Smart collection hooks routes

Today I updated forest-express-sequelize to v7.6.3

As for now, this is my smart collection forest/ file :

const { collection } = require('forest-express-sequelize');

collection('categories', {
  actions: [{
    name: 'Ajouter un nouveau contrat',
    type: 'single',
    fields: [{
      field: 'Clef de repartition',
      type: 'Enum',
      enums: []
    }, {
      field: 'Référence',
      type: 'String'
    }, {
      field: 'Date de début',
      type: 'Date'
    }, {
      field: 'Date de fin',
      type: 'Date'
    }, {
      field: 'Préavis en jours',
      type: 'Number'
    }, {
      field: 'Tacite reconduction',
      type: 'Boolean'
    }, {
      field: 'Montant annuel',
      type: 'Number'
    }, {
      field: 'comment',
      type: 'String'
    }, {
      field: 'Échéance du contrat assurance',
      type: 'Enum',
      enums: [
        'Mensuelle',
        'Trimestrielle',
        'Annuelle'
      ]
    }, {
      field: 'Type de contrat',
      type: 'Enum',
      enums: [
        'Maintenance',
        'Assurance'
      ]
      // }, {
      // "provider_matricule": "TYMATE_12"
    }],
    hooks: {
      load: ({ fields, record }) => {
        console.log(record);
        return fields;
      }
    }
  }, {
    name: 'Mise à jour (avenant) d\'un contrat',
    type: 'single',
    fields: [{
      field: 'Date de fin',
      type: 'String'
    }, {
      field: 'Préavis en jours',
      type: 'String'
    }, {
      field: 'Tacite reconduction',
      type: 'Boolean'
    }, {
      field: 'Montant',
      type: 'Number'
    }, {
      field: 'PJ du contrat',
      type: 'File'
    }, {
      field: 'Échéances',
      type: 'String'
    }],
  }],
  fields: [{
    field: 'id',
    type: 'String'
  }, {
    field: 'display_name',
    type: 'String'
  }, {
    field: 'created_at',
    type: 'Date'
  }, {
    field: 'updated_at',
    type: 'Date'
  }, {
    field: 'sergic_slug',
    type: 'String'
  }, {
    field: 'sergic_update_time',
    type: 'Date'
  }, {
    field: 'mandatory',
    type: 'Boolean'
  }, {
    field: 'default',
    type: 'Boolean'
  }]
});

As you can see, the whole point of upgrading forest-express-sequelize was in order to use hooks load method in the smart action of a smart collection. I’m about to write the load method in order to init the dropdowns in my form.

But I’m really puzzled about one thing :

 load: ({ fields, record }) => {
        console.log(record);
        return fields;
      }

This console.log simply displays :

{ id: '101' }

but this is FAR from what I need in my load method :
I should have the display_name, mandatory and all additional fields that might come in the future (the API providing me data for this collection will soon be enriched).
If the “record” contains the id only, it might be a major issue for me.

I should specify that i’m calling the smart action from the table view and I do not have a GET method for a unique record (hense no summary view).

Hi @JeremyV,

The behaviour you describe is the one that was designed.
In the context of a Smart Collection, so far, there is no easy way for the agent to retrieve the full Smart Collection record (ie you implement the controllers logic manually)

As a developer, you know the logic to build your Smart Collection record.
And I think the id is sufficient to compute the whole record in the hook before using it.
Is it wrong to say so in your context?

I’m afraid so. My API provider is providing me additional info that is not existing in the DB. These are computed business logic. This is why the smart collection has been created in the first place.

For instance : Clef de repartition - this field is a dropdown list which is different from one place to another ; so my API provider will add an additional attribute in the GET all route which is the place_id so I can query the DB and construct the appropriate list.

This is one example ; before posting my form in the smart action route, i may have to set this place_id field back to my API provider, so I need to have kept the information all along.

Hello @JeremyV,

From the way smart collection work, we are not able to fetch the record.
The only information we get is the id.
You will need to fetch the record from the id inside the hook.

I hope it can help you

Thank you for your input, we’ll figure out another way to proceed.