cancel
Showing results for 
Search instead for 
Did you mean: 

How to call other entity services

fjcv
Explorer

Hi,

I had a simple requirement. I created 3 api services with their corresponding mta in the same SCP account and deployed them.

https://xxxx.eu10.hana.ondemand.com/srv/testOne

testOne : cds

namespace ntest;
using {   cuid } from '@sap/cds/common';
entity testOne : cuid{
   quantity: Integer;
}

srv.cds

using {ntest} from '../db/testOne';
service srv {
    entity testOne as projection on ntest.testOne;
}

testTwo -> hast the same cds and srv that testOne, but I replaced name testOne for testTwo

https://xxxx.eu10.hana.ondemand.com/srv/testTwo

testThree -> same cds that service testOne and testTwo

https://xxxx.eu10.hana.ondemand.com/srv/testThree

I would like to know how can call in service testThree to services testOne and testTwo.

testthree-service.js

module.exports = cds.service.impl(( srv ) => {
   srv.before('CREATE','TestThree', req => {
     /** First Call  **/ 
       let quantityOne = // I want to get the quantity of the first services
      /** Second Call **/ 
       let quantityTwo = // I want to get the quantity of the second services
	 
	  let Total = quantityOne +  quantityTwo
	 
    })
   
});

Should I add the dependencies services in my mta and how to do it?

Can someone please advise on? Any help would be appreciated.

Thank you,

F.

Accepted Solutions (0)

Answers (3)

Answers (3)

david_kunz2
Advisor
Advisor

Dear F.,

For inter-service communication you need to create a destination for each service you want to call. For this you can use the destination service on SAP Cloud Foundry: https://help.sap.com/viewer/cca91383641e40ffbe03bdc78f00f681/Cloud/en-US/7e306250e08340f89d6c103e288...

Once this is done, you can use either the SAP Cloud SDK or CAP to trigger synchronous requests to those destinations.

In CAP you have the possibility to import external cds models and create queries using the well-known query APIs. More information can be found here: https://cap.cloud.sap/docs/guides/consuming-services

Best regards,
David

fjcv
Explorer
0 Kudos

Hi David,

I could not test untill today and I can not find the way to imported the CDS models.

I want to include the field quantity from the first service into the second services with I call the second service like: testSecond?&$expand=XXXX

Could you please help me with this example?

This is the external service in my package.json

"externalService": {
        "kind": "rest/odata",
        "model": "api/testOne",
        "credentials": {
          "destination": "DEST_testOne",
          "requestTimeout": 30000
      },

External First Service: schema.cds :

namespace my.test;
using {   cuid } from '@sap/cds/common';
entity testOne : cuid{
   quantity: Integer;
}


External Second Service: schema.cds.

namespace ntest;
using {   cuid } from '@sap/cds/common';
entity testSecond : cuid{
   quantity: Integer;
   testOneId: Association to testOne;
}


Thank you very much.

fjcv
Explorer
0 Kudos

Hi David,

Thank you very much for you info.

After import external cds models, it is also possible to do Association to one of the import services like that?

namespace my.test;
using {   cuid } from '@sap/cds/common';
entity testOne : cuid{
   quantity: Integer;
}
namespace ntest;
using {   cuid } from '@sap/cds/common';
using { testOne } from 'my.test';// import del modelo
entity testThree : cuid{
   quantity: Integer;
   testOneId: Association to testOne;
}

It should be works the association?

Best

F.

david_kunz2
Advisor
Advisor
0 Kudos

Hi F.,

Yes, it is possible. But keep in mind that imported CDS models are automatically annotated with `@cds.persistence.skip` (since it's a remote service).

To retrieve data you need to write custom logic (here you can then use the CDS query APIs).


Best regards,
David