Sub topic 14
Reading and creating business partners through the API and tables
This chapter assumes you know the business partner (BP) data model, roles, groupings and the role of Customer/Vendor Integration (CVI) from the earlier chapters. You should also be comfortable reading basic HTTP requests and ABAP Open SQL. The goal is to read and create business partners in two ways: through the released OData service, and through the standard tables.
Choosing between the API and direct table access
SAP S/4HANA gives you two ways to get at business partner data. They suit different jobs.
| Aspect | API_BUSINESS_PARTNER (OData) | Direct table reads (BUT000, CVI_CUST_LINK, and so on) |
|---|---|---|
| Stability | Released and versioned by SAP | Table structures can change between releases |
| Typical caller | External systems, side-by-side apps, cloud extensions | Custom ABAP reports and analysis inside the system |
| Writes | Supported, with validation and CVI synchronization | Not supported. Never write to these tables directly |
| Authorization | Checked by the service and underlying BP logic | Your code must check authorizations itself |
Use the API for anything that crosses a system boundary and for every write. Use tables for read-only reporting inside the system, and accept that you own the maintenance if a table changes.
The released OData service
The service API_BUSINESS_PARTNER is the released interface for business partner data. Its path is /sap/opu/odata/sap/API_BUSINESS_PARTNER. The main entity set is A_BusinessPartner. It holds the header data, and related entities hang off it through navigation properties.
| Entity set | Content |
|---|---|
| A_BusinessPartner | General data: category, grouping, name, and the Customer and Supplier keys |
| A_BusinessPartnerRole | Roles assigned to a partner |
| A_BusinessPartnerAddress | Addresses |
| A_Customer | Customer general data, with company code and sales area data below it |
| A_Supplier | Supplier general data, with company code and purchasing organization data below it |
Exact entity names and fields depend on the service version in your system. Check the service metadata at /sap/opu/odata/sap/API_BUSINESS_PARTNER/$metadata, or the SAP Business Accelerator Hub entry, before you rely on a field.
Reading partners
The following request reads five organization partners. It returns the key fields that CVI assigns:
GET /sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner?$filter=BusinessPartnerCategory eq '2'&$select=BusinessPartner,BusinessPartnerFullName,BusinessPartnerGrouping,Customer,Supplier&$top=5
Accept: application/jsonPoints to note:
BusinessPartnerCategory eq '2'selects organizations. Category 1 is person and category 3 is group.CustomerandSupplierare filled only when the partner has a customer or supplier record. For a partner with no such role, both are empty.$selectkeeps the response small. Without it, the service returns every header field.- Always set
$top. Unbounded reads of a large partner base are slow and may hit server limits.
To read the roles or addresses of a single partner, use $expand. Use the partner number as the key:
GET /sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('1000123')?$expand=to_BusinessPartnerRole,to_BusinessPartnerAddress
Accept: application/jsonThe number 1000123 is only an example. Partner numbers are strings, so keep leading zeros if your number range uses them.
Creating a partner
A write to the service goes through the same business logic as transaction BP. The call below creates an organization with the FI customer role in one request:
POST /sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner
Content-Type: application/json
x-csrf-token: <token fetched with a prior GET using x-csrf-token: fetch>
{
"BusinessPartnerCategory": "2",
"BusinessPartnerGrouping": "BP02",
"OrganizationBPName1": "Example Trading GmbH",
"to_BusinessPartnerRole": [
{ "BusinessPartnerRole": "FLCU00" }
]
}The request has these parts:
- CSRF token. SAP Gateway rejects modifying requests without a valid cross-site request forgery token. Send a
GETwith the headerx-csrf-token: fetch, then reuse the token returned in the response header for thePOST. Use the same session cookies for both calls. - Category.
2makes this an organization, so the name goes intoOrganizationBPName1. A person would use1and fields such asFirstNameandLastName. - Grouping.
BP02is an example. Groupings and their number ranges are defined in each system's customizing, so look up the valid values in yours. The grouping decides whether the number is assigned internally or supplied by the caller. - Roles.
FLCU00is the FI customer role. Supplying it in the deep structure lets CVI create the customer record in the same call. UseFLVN00for the FI supplier role, and the sales or purchasing roles for those data levels.
If the grouping uses external number assignment, add "BusinessPartner": "..." to the payload. For internal assignment, leave it out and read the new number from the response.
Common API errors
- 403 CSRF token validation failed. The token is missing, expired or from a different session. Fetch it again and resend the request with the same cookies.
- Grouping not found or number out of range. The grouping does not exist in this system, or an external number does not fit the number range. Check the grouping configuration.
- Role not allowed. The role cannot be combined with the category, or no mapping exists for CVI. Check the role customizing.
- Message from CVI. The partner is rejected because the customer or supplier cannot be created, often because a required customer or supplier field is missing. Read the message in the response body.
Because a creation with a CVI role is one logical unit, a failure normally leaves no partner behind. Still, read back the partner after a failed call before you retry. A retry after a timeout can otherwise create a duplicate.
Reading through the tables
For read-only analysis inside the system, the standard tables are enough. The central ones are:
| Table | Content |
|---|---|
| BUT000 | Business partner general data (number, GUID, category, grouping, names) |
| BUT100 | Roles assigned to a partner |
| ADRC, BUT020 | Addresses and the link from partner to address |
| CVI_CUST_LINK | Link between partner GUID and customer number |
| CVI_VEND_LINK | Link between partner GUID and supplier number |
| KNA1, KNB1, KNVV | Customer general, company code and sales area data |
| LFA1, LFB1, LFM1 | Supplier general, company code and purchasing organization data |
The partner number is in BUT000-PARTNER, but the link tables use the partner GUID (PARTNER_GUID) as the key. This is the join you need most often:
SELECT b~partner, c~customer
FROM but000 AS b
INNER JOIN cvi_cust_link AS c
ON c~partner_guid = b~partner_guid
WHERE b~type = '2'
INTO TABLE @DATA(lt_customers)
UP TO 10 ROWS.The query lists organization partners (TYPE = '2') together with their customer numbers. A partner without a customer record does not appear, because the join is an inner join. Use a left outer join if you want all partners and an empty customer number where none exists. For suppliers, replace cvi_cust_link with cvi_vend_link and select the vendor field.
Edge cases to handle
- Archived or blocked partners. BUT000 carries central block and archiving flags. Decide whether your report should include them, and filter explicitly.
- Partners without roles. A partner can exist with only the general role. It has no customer or supplier, so inner joins to the link tables drop it.
- Dual role partners. A partner that is both customer and supplier appears in both link tables with the same
PARTNER_GUID. Join both tables if you want to find these. - Authorizations. An ABAP
SELECTdoes not check authorizations. Add anAUTHORITY-CHECKwhere the data is sensitive, or read through a released API that applies the checks.
Summary of the working rules
- Read through the OData service when the caller is outside the system, and write only through it or through other released interfaces.
- Fetch a CSRF token before every modifying call and reuse the session.
- Look up groupings and role codes in your own system. Do not copy example values.
- Join the business partner to customers and suppliers through the CVI link tables, using the GUID.
- Use
$selectand$topon every read.
Exercise
You need a report in the same system that lists every business partner with its customer number and supplier number, including partners that have only one of the two. Which tables do you join, and which join type do you use for the link tables?
Show answer
Start from BUT000 and join CVICUSTLINK and CVIVENDLINK on PARTNER_GUID. Use left outer joins for both link tables, so partners with only a customer record or only a supplier record still appear. An inner join would drop every partner that lacks one of the two records.
Exercise
A POST to A_BusinessPartner fails with a 403 status and the message "CSRF token validation failed". The payload is valid. What do you do?
Show answer
Send a GET to the service with the header "x-csrf-token: fetch", read the token from the response header, and repeat the POST with that token in the "x-csrf-token" header. Keep the same session cookies for both requests, because the token is tied to the session.