cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatically trigger draft events in CAP

10-23-2024 8:19 PM
Steven-Mason Explorer
2935 views 5 comments
0 Likes
SAP Managed Tags
Labels
DraftFIORI
Subscribe

We have a use case in CAP where we are programmatically triggering draft events (create, prepare, activate, discard).  We were able to do this for the "create" draft event via the following code:

 

const { draftEntity } = service.entities
const payload = {}
await service.send({query:INSERT.into(draftEntity).entries(payload), event: 'NEW'})

 

Unfortunately we have not been able to figure out how to trigger the prepare, activate, and discard events.  We have tried the following and various iterations with no success:  

 

const { draftEntity } = service.entities

//draftEntityID is the passed in ID for the draft entity we want to activate
await service.send({ event: 'draftActivate', entity: draftEntity, data: {}, params: [{ID: draftEntityID, IsActiveEntity: false}]})

 

Being able to do this is very important to our overall design.  Any suggestions on how to accomplish this would be greatly appreciated!

For reference, here are the CAP relevant versions we are at while trying this: 

 

-js/asyncapi: 1.0.2
@cap-js/db-service: 1.14.0
@cap-js/hana: 1.3.2
@cap-js/openapi: 1.0.6
@sap/cds: 8.3.1
@sap/cds-compiler: 5.3.2
@sap/cds-dk: 8.3.0
@sap/cds-dk (global): 8.2.2
@sap/cds-fiori: 1.2.3
@sap/cds-foss: 5.0.0
@sap/cds-mtxs: 2.2.0
@sap/eslint-plugin-cds: 3.1.0
Node.js: v18.20.3

 

 

0 Likes

Accepted Solutions (0)

Answers (1)

Answers (1)

Steven-Mason
Explorer
0 Likes

Thanks Peter,

I have tried sending the "standard" events coming through the system based.  For example, I have used "SAVE" instead of draftActivate.  Example of this being: 

const { draftEntity } = service.entities

//draftEntityID is the passed in ID for the draft entity we want to activate
await service.send({ event: 'SAVE', entity: draftEntity, data: {}, params: [{ID: draftEntityID, IsActiveEntity: false}]})

While the framework does not reject this, it does not successfully trigger the draftActivate behavior observed from calls to the OData service.

Anyone else have any ideas on what I am doing wrong and how I could do this programmatically?

catano
Active Participant
0 Likes

If creating a non-draft entity instance of the draft, then what I tried to suggest is do the following steps:

Taking the ID of the draft entity:

  • read draft entity data by ID
  • create non-draft entity with draft data
  • update draft data to reflect state after creating non-draft from draft, either delete draft or do any necessary status change of draft

This is just an idea, I don't know if it is feasible or not without knowing the complete scenario of yours.

catano
Active Participant
0 Likes

I just played around with the stuff, but still have no idea. I could craft the HTTP request to call the draft actions but cannot call them programatically, even when try to fully copy the request made by the framework itself..

http://localhost:4004/odata/v4/service/Entity(ID=c2b16e23-d129-4134-9558-1512c75ceb62,IsActiveEntity=false)/Service.draftPrepare
http://localhost:4004/odata/v4/service/Entity(ID=c2b16e23-d129-4134-9558-1512c75ceb62,IsActiveEntity=false)/Service.draftActivate

I would assume that the following should work:

srv.send('GET', `Entity(ID=${id},IsActiveEntity=false)/Service.draftPrepare`);

but I'm a bit confused because I cannot even make it work a basic query like:

srv.send('GET', 'Entity');

I'm still getting error message:

[cds] - Error: The request has no query and cannot be served generically.
    at module.exports (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/node_modules/@sap/cds/libx/_runtime/common/error/index.js:5:48)
    at ApplicationService.<anonymous> (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/node_modules/@sap/cds/libx/_runtime/common/generic/crud.js:17:13)
    at next (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/node_modules/@sap/cds/lib/srv/srv-dispatch.js:69:36)
    at ApplicationService.handle (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/node_modules/@sap/cds/lib/srv/srv-dispatch.js:73:6)
    at async ApplicationService.handle (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/node_modules/@sap/cds/libx/_runtime/common/Service.js:84:16)
    at async ApplicationService.<anonymous> (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/srv/service.js:124:18)
    at async next (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/node_modules/@sap/cds/lib/srv/srv-dispatch.js:69:17)
    at async ApplicationService.handle (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/node_modules/@sap/cds/lib/srv/srv-dispatch.js:67:10)
    at async ApplicationService.handle (/Users/petermanuel.catano/projects/sap-btp-cloud-full-stack-challange/node_modules/@sap/cds/libx/_runtime/common/Service.js:84:16) {