cancel
Showing results for 
Search instead for 
Did you mean: 

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

sreehari_vpillai
Active Contributor
0 Kudos

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

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

Accepted Solutions (0)

Answers (1)

Answers (1)

sreehari_vpillai
Active Contributor
0 Kudos

I took the service instance and fired srv.delete , that worked.

req.data was empty , but pulling the where clause like below helped me to build the necessary UPDATE query

 let where = req.query.DELETE.where ;
return UPDATE("atom.test.db.Items").set({DELETED: true}).where(where);

atom.test.db.Items being the Database CDS entity .