cancel
Showing results for 
Search instead for 
Did you mean: 

CAP: How to call a generic cds OData Delete handler from a custom OData action in the same service

achim_vogel2
Explorer
0 Kudos
1,085

Hi,

I have a cds entity

entity Object : managed {
key id : UUID ;
objectType : Association to mdm.ObjectType not null;
externalKey : String(200); . .
}

With OData I can delete an Object only by it's id:

DELETE /servicename/Object('4711')

I would like to delete an Object by its objectType and externalKey, which are together unique. I have a custom action for this:

Service definition:

action deleteObjectByObjectTypeAndExternalKey(objectType:String, externalKey:String);

Implementation:

const cds = require('@sap/cds')
module.exports = function (){
this.on('deleteObjectByObjectTypeAndExternalKey', async (req) => {
// Read the id by given objectType and externalKey
let ids = await cds.run (`SELECT ID FROM OBJECTI
WHERE OBJECTTYPE_ID = '${req.data.objectTypeId}' AND EXTERNALKEY = '${req.data.externalKey}'`);
if( ids.length > 0)
{ // Now delete by id
// => How can I call the generic cds Odata handler here ?
}
});

But I do not know how to call the generic OData DELETE handler with the id, after I have selected it from the DB. I would like to do all the generic work, for example delete depending composition entities, by the generic handler.

Can somebody help me here?

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

gregorw
Active Contributor

You can use cds.connect.to (name, options?) ⇢ service and then use the normal cds-ql DELETE syntax.

achim_vogel2
Explorer
0 Kudos

Hi Gregor,

thank you very much. It works as you described above.

Best regards

Achim

LuizGomes
Participant
0 Kudos
gregorw

Do you have any end-to-end example to perform a delete?

Answers (0)