on 11-27-2022 11:48 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Thanks gregorw !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
82 | |
11 | |
8 | |
8 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.