`addField` gets called with empty records

Hey @kll,

After some back and forth with the team (and reproducing easily the behaviour you’ve described) I do have an answer.

This is an expected behavior. :sweat_smile:

If the relation you are depending on is null, you still need to return a value for that record (That’s not because you don’t have a relation that you don’t want to still compute that field).
Computed fields must be sent back with their original order of computation, so sending nothing would have caused issue to detect which computed belong to which record.

We could have sent null, but we didn’t just to prevent having multiple xx?.yy to check for dependencies existence before computing.

Typings may not be correct though.

So the good answer for the resolution is to handle the case. :confused:

agent.customizeCollection('user', collection => {
  collection.addField('displayName', {
    columnType: 'String',
    dependencies: ['firstName', 'lastName'],
    getValues: (records, context) => {
      return records.map(r => `${r.firstName} ${r.lastName?.toUpperCase()}`),
    }
  });
});

Let me know, if you understand this global behavior. I do understand that this is weird in the one to one relations with a reference field.

Kind regards,
Morgan

1 Like