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.
We can see that 4 different request from 4 different user or system wants to update planned order no 1234 which has current etag value “E01” (Figure 1)
Figure 1
Request 1 comes a millisecond ahead from other request so that it will update the document and etag value of the document would also be changed to E02, now other requests would get a response that “412: precondition is failed” because they have sent data with etag value “E01” which is now became E02, that means etag value is not latest or correct (Figure 2)
Figure 2
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)
Sample payload
Figure 3
Content modifier 1 will read planned order number and store it in a property called “porder” which will be used in API call(Figure 4)
Figure 4
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)
API : get: https://<you s/4hana tenant host name>/sap/opu/odata/sap/API_PLANNED_ORDERS/A_PlannedOrder(PlannedOrder=’${property.porder}’)
Figure 5
After getting the etag value from first call, let’s set that value in “if-match” header (Figure 6)
Figure 6
Call the same API again but this time with delete operation and if-match header.(Figure 7)
API : delete:https://<your s/4hana tenant hostname>/sap/opu/odata/sap/API_PLANNED_ORDERS/A_PlannedOrder(PlannedOrder=’${property.porder}’)
Figure 7
Save and deploy the iflow.
Let’s send the request from postman. (Figure 8)
Figure 8
Check the trace logs in CPI
After get call, etag value is coming as a header(Figure 9)
Figure 9
Same etag value will be passed in if-match header in delete call(Figure 10)
Figure10
Planned order delete 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”.