How to set value for "single type" Smart Action's field referenced to another table

I have a Smart Action as mentioned below:

collection('microTopics', {
    actions: [{
    type: "single",
    name: 'Edit MicroTopic',
    fields: [{
      field: 'Name',
      type: 'String',
      isRequired: true
    }, {
      field: 'Subject',
      reference: "subjects.id",
      isRequired: true,
      widget: 'belongsto select'
    }],
      values: async (context) => {

      let microTopic = await models.microTopics.findOne({ where: {id: parseInt(context.id)}})
      let subject = await microTopic.getSubject();

      console.log(`subject.name ===> `)
      console.log(subject.name)
      console.log(subject)

      // subject data is getting logged here correctly.

      return {
        Name: microTopic.name, // Name field is getting populated in the UI correctly
        Subject: subject.name // Subject field is **NOT** getting populated in UI.
      }
    }
    }]

Also we have this association in the microTopics model:

MicroTopics.belongsTo(models.subjects)

Expected behavior

The Subject field should get populated with the subject data associated with the microTopic.

Actual behavior

The Subject field (referenced to the Subjects table) in the Edit MicroTopic action is not getting populated at all.

Failure Logs

None.

Context:

  • Package Version: 6.2.1
  • Express Version: 4.16.4
  • Sequelize Version: 5.15.2
  • Database Version: MySQL 5.7
  • Project Name: xs-library

Hi @Shriram_Balakrishnan,

If I understand well, Subject is a reference and not a simple String attribute input in your Smart Action form.

It seems logic that Subject: subject.name does not work, as the the name of the subject is not what is expected here. Did you try: Subject: subject or maybe Subject: subject.id?

Hi @arnaud

I tried both suggestion of yours - Subject: subject and Subject: subject.id.
But it did not work. Any other way to make this work?

If I understand well, Subject is a reference and not a simple String attribute input in your Smart Action form.

Yes. That statement is correct.

Ok,

So I am afraid the prefilled values do not work for references.
Let me create an insight in our Product Board.

@arnaud

By Insight, do you mean a feature request?

But this is a critical requirement for us. We have a create form but we do not have a Edit form because of this.

Hi @Shriram_Balakrishnan,

What Arnaud meant by insight is indeed a feature request.

As that’s something you need (and that some other might need in the future), we logged it for future prioritization.

However, from what I can see here you’re just trying to re-create an edit form. Which already exists when you click on a record.

@lamatt_v - Thanks for the clarification.

Regarding you following question:

However, from what I can see here you’re just trying to re-create an edit form. Which already exists when you click on a record.

I need to add custom fields (that is, extra fields in addition to the ones that exist in the table). I am processing the value of these fields in the Smart Action’s routes.

I see that it is possible to override the routes of existing CRUD actions.

But Is it possible to have extra fields in the create/edit forms? If yes, how can I add it?

cc: @arnaud

@lamatt_v @arnaud - Any idea on this?

Hi @Shriram_Balakrishnan

I need to add custom fields (that is, extra fields in addition to the ones that exist in the table)

You could consider creating smart fields and configure their visibility and behavior. Then you could see them in the default edit/create form of Forest Admin without having to create your own form (smart field can be hidden in table view). Still, I’m not totally sure it would fit your need, but you could give it a try :slight_smile:

1 Like