2

Your Guide to Calling S/4HANA APIs from SAP Build Apps

 11 months ago
source link: https://blogs.sap.com/2023/06/11/your-guide-to-calling-s-4hana-apis-from-sap-build-apps/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
June 11, 2023 7 minute read

Your Guide to Calling S/4HANA APIs from SAP Build Apps

0 2 175

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. (Later I can explain how I did that, but that is beyond the scope of this blog post – this tutorial will give you a start.)

2023-06-11_09-58-34.png

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:

2023-06-11_10-08-37.png

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.

2023-06-11_12-15-44.png

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.

2023-06-11_13-15-03.png

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.

2023-06-11_10-41-51.png

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.

2023-06-11_10-46-24.png

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.
    2023-06-11_10-50-32.png
  • I then retrieved the data for the selected business partner, but I saved the expanded address field in the variable above.
    2023-06-11_10-53-17.png
    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.

2023-06-11_10-56-34.png

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.

2023-06-11_11-04-49.png

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).

2023-06-11_11-07-00.png

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.

2023-06-11_11-22-35.png

On this page I created a “new data record” variable for both BusinessPartner and BusinessPartnerAddress.

2023-06-11_11-27-51.png

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.

2023-06-11_11-31-44.png

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] })

2023-06-11_11-34-59.png

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.

2023-06-11_11-51-21.png

Just so you know it’s real, here are my business partners in my S/4HANA system:

2023-06-11_11-55-52-1.png

If I click on my Wroblewski & Son business partner, I get the details including the addresses I got from expanding the entity.

2023-06-11_11-52-36.png

I can add an address (and the display gets updated).

2023-06-11_11-53-34.png

And the data is in my S/4HANA system.

2023-06-11_12-02-49.png

And finally, I can create a new business partner and the associated address in one API call.

2023-06-11_12-11-29.png

And here it is in S/4HANA.

2023-06-11_12-13-09.png

Additional Comments

In no particular order:

  • I did not try updating addresses with A_BusinessPartner PATCH
  • I did not try updating addresses with A_BusinessPartner(ID)/to_BusinessPartnerAddress POST – I’m not sure this is even possible.
  • I wish I could control when to expand the associated entity. I didn’t want it when I was getting the list of all business partners, but I did want it when I got a single business partner.
  • If I have to update many associated entities this might become cumbersome.
  • You must still be aware of the details of the S/4HANA Cloud APIs, including what fields are mandatory and what constraints fields have – otherwise you’ll get a lot of 400 errors.

But all in all, it was amazing!!! The S/4HANA system got updated, the destinations handled the CSRF tokens, the data displayed perfectly, and it was all simple to do, just a few short formulas.

Good luck with your extensions and let me know how it works and if you have any questions.

Download Project

To see the details of what I describe above, you can download the project. You’ll have to create your own destination to your own S/4HANA system.

Note that on my first page, I use search term DBW to bring back the records I wanted from my S/4HANA system, and I set this search term on the last page when creating new business partners. You’ll have to adjust this.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK