A year ago, people got busy building S/4HANA extensions with SAP Build Apps, and demanded a feature that would make their OData calls easily – by using $expand to get more data with fewer calls. That feature was recently delivered.
But people are still were confused.
When you create a connection to an OData service, SAP Build Apps automatically provides you with the schema so you can reference all the fields. But even when you expand the associations, the schema for the associated objects is not (readily) available.
So I decided to explain how you could make S/4HANA API calls simpler – and take advantage of the recent features, and still get the schema you want. Here’s all you need to know about calling S/4HANA from SAP Build Apps, for both GET and POST requests. Strap in!
The S/4HANA API
For this example, I am using the Business Partner API. I already created a destination with a communication user with permission to call the Business Partner API.
The Business Partner API has many entities, but I focused on just 2:
◉ BusinessPartner (the main entity, which has many associated entities, including one for addresses)
◉ BusinessPartnerAddress
I used these entities to show 3 things:
◉ How to expand associated entities when retrieving the main entity
◉ How to create a new associated entity (address) for an existing main entity (business partner) – calling BusinessPartnerAddress
◉ How to create a new main entity (business partner) that includes an associated entity (address (calling) – calling BusinessPartner with no additional call
And all this is done while providing the schema you want for the associated entity.
Setting Up the Data Resources
In my SAP Build Apps project, I enabled SAP BTP authentication, which allows me than to easily set up data resources for SAP backends that are connecting with SAP BTP destinations (like the one I created above).
I enabled the 2 entities I needed:
The BusinessPartnerAddress entity was used to make calls, but ALSO to easily get the schema for the address for use when calling BusinessPartner.
For BusinessPartner, I expanded the associated address entity.
Page 1: List of Business Partners
The first page was quite easy, as it was a List Item component repeated to show all the business partners.
In the secondary label I show the business partner ID, but we could have shown the number of addresses or or the country of the main address.
I created a data variable from the BusinessPartner entity, filtering it for only the business partners I wanted (search term = DBW) and paging to only 20 items per page.
I added logic that when you clicked on one of the business partners, it opened the detail page, sending the ID, which is in the BusnessPartner field.
Page 2: Business Partners Details
I created a page to display the business partner full name and ID, as well as one of the associated entities: addresses.
Here’s the trick:
◉ I first created a variable based on the BusinessPartnerAddress entity (collection of data records) but I removed the logic for retrieving the data – it is just a variable with the proper schema.
◉ I then retrieved the data for the selected business partner, but I saved the expanded address field in the variable above.
And this was the formula:
outputs["Get record"].record.to_BusinessPartnerAddress.results
So now I had the address data for the selected business partner in its own variable, and I could use it to repeat the list item to display all the address. And better still, I had the schema so it was easy to bind the fields to my UI.
Adding an Address
I added a form at the top of the page to add an address to the current business partner. I created another variable but this one was a “new data record” variable so it did not retrieve data and only had a “set data variable”. I used this to set some basic data I needed for adding any address – for example, the business partner ID.
Then I added some input fields for a new address, bound them to the this variable, and then on button click I created the record, using the BusinessPartnerAddress API. (You could probably also use the PATCH call on the BusinessPartner API.)
After creating the record I retrieved the business partner with all its addresses to update the screen (I could have just added this record to the variable manually).
Page 3: Create New Business Partner (w/ address)
OK, this part is what people were asking about: How to create a new business partner but with a new associated address when there is no schema for the address within the business partner schema?
I created a new page with input fields for business partner header data and for associated address data.
On this page I created a “new data record” variable for both BusinessPartner and BusinessPartnerAddress.
The only thing I set in the data variable logic was the search term and the category for the BusinessPartner variable.
On the page, I bound the input fields to the appropriate variable, and the appropriate fields in those variables.
Now the Magic!
When I click the button to add a business partner, all I do is add a “Create record” flow function to the BusinessPartner entity.
But the value is a formula, where I append the address variable onto the main business partner variable:
SET_KEY(data.A_BusinessPartner1, "to_BusinessPartnerAddress", {results: [data.A_BusinessPartnerAddress1] })
If you had other associated entities in the main entity, you just add another flow function.
Recap
I retrieve my list of business partners labeled with my special search term.
Just so you know it’s real, here are my business partners in my S/4HANA system:
If I click on my Wroblewski & Son business partner, I get the details including the addresses I got from expanding the entity.
I can add an address (and the display gets updated).
And the data is in my S/4HANA system.
And finally, I can create a new business partner and the associated address in one API call.
And here it is in S/4HANA.
No comments:
Post a Comment