How to use etag for change operations in S/4HANA OData APIs
Introduction: This document describes about why etag is required in update or delete operations of OData API in S/4HANA cloud and how to use it in CPI while making put/patch/delete OData API calls.
etag or entity tag is used in concurrency control of a data while changing it using OData service.
Let’s consider that multiple requests are trying to update same document in S/4HANA cloud via OData Service at the same time.
Now the request 1 will update the document and then request 2 will update the document which will overwrite the updates of request 1 later request 3 will overwrite the data updated by request 2, This will happen because there is no locking mechanism.
To solve this issue, etag is used for concurrency control, which makes sure that only one request will update the data and remaining requests will be notified that they are not updating latest document.
Let’s understand that how to do concurrency control using etag with an example.
All system or user must get the current etag value of the document and then send it along with change request.
If you don’t sent etag as a header then you will get following error
“com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occured: Precondition Required : 428 : HTTP/1.1 .”
So, in this way, only one request can change or update data
Now, lets see how to use etag in CPI for OData API call for an update or delete request
Let’s create an iflow where a planned order number will come in the request payload and after reading planned order number from payload, same planned order number will be deleted from S/4HANA cloud.(Figure 3)
Same API will be called two times in the iflow, first time it will be called with read(get) operation to get etag of that order “3000” (Figure 5)
Call the same API again but this time with delete operation and if-match header.(Figure 7)
Save and deploy the iflow.
Check the trace logs in CPI
Planned order deleted successfully from S/4HANA cloud!!!
Conclusion: After reading this blog, you have learnt that “what is etag, why it is required and how to use it in CPI”.