Technical Articles
How to Call an Unbound Function Through an Unbound Action in SAP CAPM and Pass Function Parameters to a Destination within SAP BTP
Introduction:
In this blog, we will explore how to call an Unbound Function from an Unbound Action in the SAP Cloud Application Programming Model (CAPM), which subsequently triggers a call to a Destination defined within the SAP Business Technology Platform.
Let’s begin by understanding the distinction between Actions and Functions, specifically Custom Actions and Functions(Custom Actions and Functions)
-
- Actions are meant for operations, which add or modify data in the server; they are called through POST request with the arguments passed in application/json bodies.
-
- Functions are meant for operations, which only retrieve data from the server; they are called through GET requests with the arguments passed in the URL path.
Before we delve into implementation, it’s essential to grasp the difference between “Bound Functions/Actions” and “Unbound Functions/Actions.”
Bound Functions or Bound Actions are tied to a specific entity and are defined within that entity’s service definition. Unbound Functions or Unbound Actions, on the other hand, are defined within the service definition as depicted below:
- How to Call an Unbound function by an Unbound Action:
Step 1: Service Definition: Service needs to be defined in the srv folder within SAP CAPM framework using .cds file.
Step 2: Define the Unbound Action within the service definition (GBConsentSrv)
Step 3: Unbound function is defined that we want to call from an action and then pass parameters to this unbound function from an unbound action.
Step 4: A critical step is to establish the Data Type for the incoming parameters in the unbound function. This definition should be in the .cds file where the service definition, along with Action and Function definitions, are maintained.
- Implementing Action and Function:
Step 1: Implement the Action within the consent-srv.js (service implementation file), adhering to the relevant business logic (which we won’t cover in this blog). Refer to SAP blogs for further details.
Based on incoming parameters, we have crafted custom logic for the action modifyExtendEndDate_014. Our objective is to subsequently send header parameters to the unbound function based on this action’s outcome
Based on the incoming parameters from the service, we have written the custom logic for the action modifyExtendEndDate_014, now we want to send header parameters to our unbound function based on the outcome of this action.
Step 2: To call an Unbound Function from an Unbound Action, initially connect to the service defined in the service definition file (GBConsentSrv, in our case).
Step 3: Prepare the parameters to be sent to the unbound function and then invoke the function using the reference of the calling service
Step 4: Extract the parameters in the calling function and craft custom logic to align with business requirements.
- Destination Configuration:
To call a specific destination defined within the SAP BTP and pass the Payload to the destination, follow these steps:
Step 1: In the project’s root folder, navigate to package.json – cds->requires section file.
Step 3: Call the destination via reference
const DEST = await cds.connect.to(‘sendemail_Oauth’);
Here we are just passing the header parameters to the destination
Reference Links:
https://cap.cloud.sap/docs/guides/providing-services#custom-actions-functions
https://cap.cloud.sap/docs/cds/cdl#actions
Through a systematic walkthrough of steps, I have tried to explain the service definition, define the Unbound Action and Function, implement custom logic, and interact with a designated destination.
These techniques enable us to harness the full potential of the SAP CAPM framework, bringing flexibility and efficiency to our application development journey.
For more information please visit https://cap.cloud.sap/docs/about/
I hope everyone enjoyed this blog. Please provide your feedback and let me know if you have any questions by leaving your comments below.
Regards,
Shiromani Soni