cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to invoke event handlers from handler of another entity?

KM11
Participant

Hi CAP experts david.kunz2 gregorwolf iobert mioyasutake thomas.jung

Below is my schema and service

test_Service.cds-> Entities: Entity1(Draft-Enabled), Entity2(Draft-Enabled), Upload( Singleton entity with one field for excel data and no persistence in DB).

In test-service.js, I implemented an Upload handler srv.on("PUT", Upload), where I read excel data and insert it into Entity1 or Entity2( I passed entity name dynamically from separate UI modules/apps developed for both entities). For inserting I use INSERT.into(entity).entries(data).

Problem Statement: I want to carry out some validations on data before inserting into the respective entities. Is there a way I can invoke srv.before or srv.on event handlers of Entity1 or Entity2 on calling the statement INSERT.into(entity).entries(data) which is written inside handler of Upload srv.on("PUT", Upload)?

Thanks

Kanika

View Entire Topic
martin-kl
Explorer

Hello Kanika,

we achieved this functionality by running the query object not on the db or cds but on the service in question:

const query = UPDATE.entity('<entity-name>').byKey(<identifier>).set({
status: 100
});
let srv = await cds.connect.to('<srv-name>');
return await srv.run(query);

On a side note: in our setting only queries using the String name of the entity triggered the correct event, using the reflected entity instead of the string did not work!

Best,

Martin

KM11
Participant
0 Kudos

Hello Martin

This was great, worked like a charm.

Thanks a lot.

Also I would like to highlight this point because that does the trick. It only works when passed the entity name as String(without any namespace)
On a side note: in our setting only queries using the String name of the entity triggered the correct event, using the reflected entity instead of the string did not work!

Regards

Kanika