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?
Request clarification before answering.
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.
Fetch the Current ETag:
Use the ETag in Your Update Request:
Batch Request for Updates:
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ":
Then paste it to the header If-Match
Best Regards,
Aliaksandra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 18 | |
| 13 | |
| 9 | |
| 8 | |
| 6 | |
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.