Skip to Content

How to call other entity services

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.

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • Posted on Feb 25 at 11:26 AM

    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/7e306250e08340f89d6c103e28840f30.html

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 26 at 09:20 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

  • Posted on Mar 11 at 01:20 PM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.