cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to call Update API using http request

07-02-2025 1:53 PM
JastinC1 Participant
1494 views 4 comments Go to solution
0 Likes
SAP Managed Tags
Labels
'API'ABAP RAP(RESTful Application Programming)s4 hana public cloud
Subscribe

I'm trying to call the FixedAsset API to update an asset from S/4 Public Cloud using RAP.

However, I am getting an error from the API - "State of entity CDS~A_FIXEDASSET with key XXXXX was already changed (stale If-Match)"

in before, I am getting an error about missing etag. Now, I'm taking the system date and time to be used as etag. I search that doing a batch request can do this but have no idea how to code it and I am using http client request and not service consumption model.

 DATA(lo_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
comm_scenario = 'XXX'
service_id = 'XX'
comm_system_id = 'XXX'
).
DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_destination ). 

DATA(lo_request) = lo_http_client->get_http_request( ).

DATA(lv_uri) = |/sap/opu/odata4/sap/api_fixedasset/srvd_a2x/sap/fixedasset/0001/FixedAsset/&1/&2/&3/SAP__self.Change|.

DATA(lv_date) = cl_abap_context_info=>get_system_date( ).
DATA(lv_time) = cl_abap_context_info=>get_system_time( ).
DATA(lv_etag) = | { lv_date+0(4) }-{ lv_date+4(2) }-{ lv_date+6(2) }T{ lv_time+0(2) }:{ lv_time+2(2) }:{ lv_time+4(2) }Z|.

lo_request->set_header_field(
EXPORTING
i_name = 'if-match'
i_value = |W/"datetimeoffset'{ lv_etag }'"|
).

lo_request->set_uri_path( lv_uri ).

DATA(lo_res) = lo_http_client->execute( if_web_http_client=>post ).
DATA(lv_text) = lo_res->get_text( ).

can you provide a solution so I can update asset using API?

0 Likes

Accepted Solutions (1)

Accepted Solutions (1)

HenrikeGroetecke
Product and Topic Expert
Product and Topic Expert

Hello,

Thank you very much for your question.

Can you please check whether the following information solves your issue?

To update an asset using the FixedAsset API in SAP S/4HANA Cloud, you need to handle ETags correctly for concurrency control. How to use etag for change operations in S/4HANA OData APIs The error message ‘State of entity CDS~A_FIXEDASSET with key XXXXX was already changed (stale If-Match)’ indicates that the ETag you are using is outdated.

Steps to Resolve:

  1. Fetch the Current ETag:

    • Before making an update, perform a GET request to retrieve the current ETag of the asset. This ensures you have the latest version.
  2. Use the ETag in Your Update Request:

    • Include the ETag in the ‘If-Match’ header of your update request. This tells the server that you are working with the latest version.
  3. Batch Request for Updates:

    • You can use a batch request to handle multiple operations APIs for Sales - ETag Handling in one call. This involves creating a changeset with the ETag and the update payload.

Example Code for Batch Request:

DATA(lo_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
    comm_scenario = 'XXX'
    service_id = 'XX'
    comm_system_id = 'XXX'
).
DATA(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination(lo_destination).

DATA(lo_request) = lo_http_client->get_http_request().

" Fetch the current ETag
DATA(lv_uri_get) = '/sap/opu/odata4/sap/api_fixedasset/srvd_a2x/sap/fixedasset/0001/FixedAsset/&1/&2/&3'.
lo_request->set_uri_path(lv_uri_get).
DATA(lo_res_get) = lo_http_client->execute(if_web_http_client=>get).
DATA(lv_etag) = lo_res_get->get_header_field('etag').

" Prepare the batch request
DATA(lv_uri_post) = '/sap/opu/odata4/sap/api_fixedasset/srvd_a2x/sap/fixedasset/0001/FixedAsset/&1/&2/&3/SAP__self.Change'.
lo_request->set_uri_path(lv_uri_post).
lo_request->set_header_field(
    EXPORTING
    i_name = 'If-Match'
    i_value = lv_etag
).

" Execute the update
DATA(lo_res_post) = lo_http_client->execute(if_web_http_client=>post).
DATA(lv_text) = lo_res_post->get_text().

Kind regards,

Henrike

[1] How to use etag for change operations in S/4HANA OData APIs

Answers (1)

Answers (1)

AlexLukashevich
Product and Topic Expert
Product and Topic Expert

Hello @JastinC1 ,

There is an example how to get the correct value for Etag from the request in Postman.

Use the next url path /sap/opu/odata4/sap/api_fixedasset/srvd_a2x/sap/fixedasset/0001/FixedAsset/X/X/X to get value of "@odata.etag ":

AlexLukashevich_0-1769519650687.png
Then paste it to the header If-Match

AlexLukashevich_1-1769519824154.png

Best Regards,

Aliaksandra