cancel
Showing results for 
Search instead for 
Did you mean: 

How can I return a draft Entity using CAP Node.JS?

gregorw
Active Contributor

Hello CAP Friends,

the SAPUI5 documentation at Configuring Custom Actions that Create New Objects describes this behaviour of the UI:

"If a user triggers the action, note the following behavior in a draft application: if the back-end system returns a draft object as the result of the action, the front-end system automatically navigates to the object page where the new draft object is opened in edit mode."

This is exactly the behaviour I want to achieve. I've implemented an example in my bookshop-demo which can be tested via: http://localhost:4004/webapp/fiori-latest.html#V4Role-manageUI5latest. When you click there the action button "createDraftRole" you will be asked to enter the role name and description. When you then click the createDraftRole button of the pop-up the Entity is created using this code:

srv.on(["createDraftRole"], async (req) => {
  const adminService = await cds.connect.to("AdminService");
  const { Roles } = adminService.entities;
  console.log("createDraftRole - Request Parameters:", req.data);
  const insertRes = await cds.run(
    INSERT.into(Roles).entries([
      {
        ID: cds.utils.uuid(),
        rolename: req.data.rolename,
        description: req.data.description,
      },
    ])
  );
  const keyFields = [...insertRes][0];
  delete keyFields.IsActiveEntity;
  const roles = await cds.run(SELECT.from(Roles).where(keyFields));
  const role = roles[0];
  return role;
});

The UI navigates also the the created entity. But unfortuantely not in Edit mode. That due to the fact that I haven't found any documentation how to create a draft Entity in the CAP Node.JS documentation about Built-in Draft Support. It seems that for CAP Java this is already possible: Consuming Draft Services.

Would be great to know what is planned in this direction.

Best Regards
Gregor

Accepted Solutions (1)

Accepted Solutions (1)

OlenaT
Advisor
Advisor
0 Kudos

Hi gregorw,

In Node.js stack we are currently have no plans to provide Draft API similar to the one impemented by Java.

In your case you can try to register a handler not for a function but for a 'NEW' event, where a new draft is created. In the custom code you can access a draft entity as follows:

await SELECT.from(Books.drafts)

Also I'm not sure about cds.run. Most of our draft handling is implemented on service layer, so calling a handler on db layer will skip all of it. Maybe try srv.run (send)? If it not helps, please ask UI colleagues, if one can get a pop-up for some data in edit mode with some UI annotations.

Sorry for such a vague answer, building a custom draft implementation is really tricky.

Best regards,

Olena

gregorw
Active Contributor

Hi Olena,

thanks to your answer to Cannot create draft for Node.js CAP Service in custom handler I was able to update my bookshop-demo with this code for my action implementation:

  srv.on(["createDraftRole"], async (req) => {
    const adminService = await cds.connect.to("AdminService");
    const { Roles } = adminService.entities;
    console.log("createDraftRole - Request Parameters:", req.data);
    const role = await srv.send({
      query: INSERT.into(Roles).entries([
        {
          ID: cds.utils.uuid(),
          rolename: req.data.rolename,
          description: req.data.description,
        },
      ]),
      event: "NEW",
    });
    return role;
  });

When I trigger this action via the Fiori UI the new entity is returned as a Draft and so the behaviour documented in Configuring Custom Actions that Create New Objects works and the entity is shown in edit mode.

Best Regards
Gregor

Answers (1)

Answers (1)

sergei-u-niq
Active Contributor

Thanks gregorw !