Skip to Content
0
Mar 10 at 06:40 PM

BTP Cloud Application Programming - trigger on DELETE exit of associated entities

267 Views Last edit Mar 10 at 06:42 PM 2 rev

I have a Database cds with 2 entities Header and Item . I have a service MyService in Service.cds as Headers and Items.

image.png

image.png

I wrote Service.js to handle DELETE operation on both the entities. I do not let the system actually delete the records from the DB , but just set the DELETED = true

const cds = require("@sap/cds");

module.exports = cds.service.impl(async function(srv) {
this.on("DELETE", "Headers", async (req) => {
let id = req.data.ID; console.log("Deletig header " + id);
await UPDATE("atom.test.db.Header").set({ DELETED: true }).where({ ID: id });
});
this.on("DELETE", "Items", async (req) => {
let id = req.data.ID; let lineId = req.data.LineItemId;
await UPDATE("atom.test.db.Items").set({ DELETED: true }).where({ ID: id, LineItemId: lineId });
});
});

Now the question :

When a DELETE on Headers is triggered , I want all the Items also to be marked as DELETED = true. So I do like this

await DELETE.from('atom.test.srv.MyService.Items').where({ ID : 1 });

I expected that the DELETE exit of Items would be fired and will be handled there and that did not happen -Entries were deleted from the tables. is it expected ? Or am I doing something wrong ?

is there an option to code my way ? My real scenario had 15 tables with a linear hierarchy where there user can delete records at all level. Such a delete must mark all the subsequent level records with DELETED = true.

Sree

Attachments

image.png (30.7 kB)
image.png (19.4 kB)