Sub topic 14
Reading and creating business partners through the API and tables
This chapter assumes you know the business partner model, roles, groupings and how CVI links partners to customers and suppliers. It also assumes you can read a basic OData request and an ABAP SELECT. You will read partner data through the released API and the standard tables, then create a partner through the API.
Two ways in
SAP supports two access paths, and they have different purposes.
| Path | Use it for | Caution |
|---|---|---|
| `API_BUSINESS_PARTNER` OData service | Reading and writing from outside the system or from extensions | Released and stable. Applies the same checks as transaction BP. |
| Tables such as `BUT000` and `CVI_CUST_LINK` | Reading inside the system, in reports and analysis | Read only. Never write to these tables directly. |
Writing directly to the tables skips validation and CVI synchronization, and it leaves the partner inconsistent with its customer or supplier records.
Reading through the API
The request below reads five organization partners. Category 2 means organization. The $select option returns Customer and Supplier, the keys that CVI assigns to the partner.
GET /sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner?$filter=BusinessPartnerCategory eq '2'&$select=BusinessPartner,BusinessPartnerFullName,BusinessPartnerGrouping,Customer,Supplier&$top=5
Accept: application/jsonAn empty Customer means the partner has no customer role or CVI has not yet created the customer. An empty Supplier means the same for the supplier side.
Reading through the tables
BUT000 is the partner header table. CVI_CUST_LINK connects each partner, through PARTNER_GUID, to a customer number. This query lists the customer number for each organization partner.
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 join is an inner join, so partners without a customer record do not appear. The supplier side works the same way through the supplier link table.
Creating through the API
A create call is a POST that needs a CSRF token. You fetch the token first with a GET that sends x-csrf-token: fetch. The request below creates an organization with the FI customer role in one call.
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" }
]
}Common errors
- 403 on POST. The CSRF token is missing or expired. Fetch a new one.
- Grouping not found or number range errors. The grouping value does not exist in this system, or its number range is exhausted or has the wrong assignment type.
- No customer after creation. The role is missing, or CVI synchronization failed. Check the role and the synchronization messages.
- Missing rows in the SQL join. The partner has no customer link. Use a left outer join when you need all partners.
Exercise
A report joins BUT000 to CVICUSTLINK and misses a partner that exists in transaction BP. Give one likely cause and one way to confirm it.
Show answer
The partner has no customer role, so CVI never created a customer and no link row exists. Confirm it by checking the partner's roles in transaction BP, or by reading the partner through the API and checking whether the Customer field is empty.