Sub topic 11
Consuming queries outside the launchpad
This chapter assumes you can build an analytical query view on a cube, as in the earlier chapters, and that you know the basics of HTTP and JSON. A query used in a Fiori app is also available to other tools. This chapter covers how those tools reach it and what stays the same on the way.
Why consume a query elsewhere
The launchpad is one front end. Teams often also need the same numbers in a spreadsheet, a dashboard tool, or a custom application. Copying the data out creates a second version of the logic. Reading the query directly keeps one definition of rows, columns, aggregation and authorizations.
The main paths
| Consumer | Typical access | Notes |
|---|---|---|
| Custom web or mobile app | OData service | You send HTTP requests and receive JSON. |
| Spreadsheet analysis (SAP Analysis for Microsoft Office) | Analytical protocol of the system | Users pick the query and drill down in Excel. |
| SAP Analytics Cloud | Live data connection | Data stays in S/4HANA and is not replicated. |
| Other tools | OData or SQL-style access | Depends on the tool and your system release. |
Which paths exist depends on your release and on whether the system is on-premise or in the cloud. Check the documentation for your version before you plan an integration.
Exposing the query as an OData service
For a custom application, the usual step is a service definition. It lists which views the service exposes. In the ABAP RESTful programming model you then create a service binding for this definition in ADT. The binding type decides the protocol (OData V2 or V4) and generates the service URL.
Using the query from the earlier chapter:
@EndUserText.label: 'Net amount by customer service'
define service ZUI_NETAMOUNT_SRV {
expose ZC_NetAmountByCustomer;
}After you publish the binding, open its $metadata document. It shows the real entity set name and the fields. Do not guess the name, because it can differ from the view name.
Reading the data from a client
The sketch below calls the service from Python. The host, service path and entity set name are placeholders. Replace them with the values from your service binding and $metadata. Credentials come from environment variables so they never appear in code.
import os
import requests
base = os.environ["S4_BASE_URL"] # for example https://myhost:44300
entity_set = os.environ["S4_ENTITY_SET"] # name taken from $metadata
url = f"{base}/sap/opu/odata/sap/ZUI_NETAMOUNT_SRV/{entity_set}"
params = {
"$select": "CustomerId,NetAmount",
"$top": "20",
"$format": "json",
}
response = requests.get(
url,
params=params,
auth=(os.environ["S4_USER"], os.environ["S4_PASSWORD"]),
timeout=30,
)
response.raise_for_status()
for row in response.json()["d"]["results"]:
print(row["CustomerId"], row["NetAmount"])The response layout d.results is specific to OData V2. A V4 service returns rows under value.
What stays the same, and what changes
- Aggregation. The server aggregates the data. If you select only
CustomerIdandNetAmount, it returns one row per customer with the summed amount. You do not group in the client. - Authorizations. Access control from the DCL applies to the user the request runs as. Two users calling the same URL can receive different rows.
- Currency and unit. An amount only makes sense with its currency. Include the currency field in your selection, or add it to the query if it is missing. Otherwise a client may add up values in different currencies.
- Filtering. Use
$filterso the database does the work. Fetching everything and filtering in the client wastes time and memory.
Common errors
- Using a technical user for everyone. A shared user with broad authorizations shows people data they should not see. Prefer logon as the real user, for example with single sign-on, so the access control checks the right person.
- Wrong entity set name. A 404 usually means the name or path is wrong. Re-read
$metadata. - Empty results with no error. The user probably has no authorization for any row. Test the same query as that user in the system.
- Expecting every query field to work in every tool. Some tools support only part of the query features, such as certain restricted measures or variables. Test with your tool early.
Exercise
A colleague reads your query through OData with a shared technical user that has full data access. Sales managers then see all customers in the custom app, although they see only their own region in Fiori. What is the cause, and what is the fix?
Show answer
The access control runs for the user making the request. The shared technical user passes every check, so every caller of the app sees all rows. The fix is to let the app call the service as the real end user, for example with single sign-on or principal propagation, so the DCL restricts rows by region for each person.