Create extra entities after create one entry

System Information
  • Strapi Version: v4.11.1
  • Operating System: Run strapi inside docker container
  • Database: postgres:14
  • Node Version: v16.11.1

Hi,
I have two separate models. Let’s say, one is Book and the other is Page. One book has one or multiple pages. When I create one new Book entity in Content Manager UI, I want to automatically create X number of Page entities and they are linked to the Book that I created. Like, after I click the default “Save” button, a book is created as usual and X pages are automatically created.

Is it possible? Which document should I look at? Thank you all very much.

Apply your logic after referring the above doc. If need any further information, update here.

Happy to help :slight_smile:

Thanks, Shekhar. You are talking about Lifecycle Hooks, right? Looks promising. I will read through the documents.

Yes about the lifecycle hooks.
You can easily apply your logic for Multiple pages creation inside afterCreate hook.

Thanks @Shekhar for pointing me in the right direction!

I added a new file under api/{name}/content-types/{type-name}/lifecycles.ts

export default {
  async afterCreate(event) {
    const { result, params } = event;
    await strapi.entityService.create(
      "api::another-type.another-type",
      {
        data : { 
          Relation: [result["id"]] // This is how I link the sub-entries with the main entry.
        }
      });
  }
}
1 Like