#1 – OData CRUD Crash Course – Update
You should have done the following steps:
– Building Update operation
1. In SAP transaction SEGW, expand a project folder and open TravelAgencySet. Click on Update and choose Go to ABAP Workbench. As the first implementation, you’ll see the popup.
2. Click on the *_DPC_EXT folder. The right pane should show the Class Interface shown in the next step.You’ll see the list of the methods. Scroll down – click and focus on the “…_UPDATE_ENTITY” and press the Redefine icon.
3. Delete the commented out lines of code. You have an empty implementation. If you click “Signature” text, you’ll see the in & out parameters of this method. We’ll implement so that it returns “ER_ENTITY” parameter.
Naming convention: l - local scope t - table s - structure v - variable 01 method TRAVELAGENCYSET_UPDATE_ENTITY. 02 03 DATA: ls_entityset TYPE stravelag, 04 ls_key_tab TYPE /iwbep/s_mgw_name_value_pair, 05 lv_error_entity TYPE string. 06 07 io_data_provider->read_entry_data( IMPORTING es_data = ls_entityset ). 08 09 * key is TravelAgencySet(agency#) 10 READ TABLE it_key_tab INTO ls_key_tab INDEX 1. 11 12 * make sure the value matches with the one in OData payload 13 IF ls_key_tab-value EQ ls_entityset-agencynum. 14 15 * new data 16 UPDATE stravelag FROM ls_entityset. 17 18 IF ( sy-subrc = 0 ). 19 * entity found and updated 20 er_entity = ls_entityset. 21 ELSE. 22 * entity not found 23 CONCATENATE iv_entity_name 24 '(''' 25 ls_key_tab-value 26 ''')' 27 INTO lv_error_entity. 28 RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception 29 EXPORTING 30 textid = /iwbep/cx_mgw_busi_exception=>resource_not_found 31 entity_type = lv_error_entity. 32 ENDIF. 33 ENDIF. 34 35 endmethod.
#05 declares a variable which can have the error message if the Update operation failed. #07 picks up the new entity value which comes in from the OData client. #10 picks up the key value of the updating entity – such as ../TravelAgencySet(‘12345678‘). #13 checks if the key value in the URL and the agencynum value in the OData payload is the same value. #16 does a simple update command. If the command goes successful, the sy-subrc value should be 0. #23 – #32 sets the user-friendly error message saying the entity doesn’t exist in the TravelAgency table.
– Testing Update operation
1. Do the Query operation. In the query step, set the two HTTP Header parameters – these are required to do any data modification (either Create/Update/Delete) against OData services in SAP Gateway.
X-CSRF-Token = Fetch
Content-Type = application/atom+xml; charset=UTF-8
2. In a response header, you’ll find the token value in X-CSRF-Token. Copy it. (Note: the token will keep valid until the browser gets closed)
3. Replace the copied value with “Fetch” string. Now your REST client is ready for Update operation via HTTP PUT.
4. Choose one of the entities you want to update – in <id> tag.
5. Run the Read operation.
6. You should obtain the entity value in a response body. Copy it.
7. And paste it in the Body payload.
8. By making use of this body content, we’ll update the existing entity. For this case just change the <d:Name> value to a new one. You can change other values if you like – but make sure the XML tag is well formed by a set of opening and closing tags.
9. Let’s issue by HTTP PUT. The URL is /TravelAgencySet(‘agency#’).
10. You should receive HTTP 204 No Content. The Update operation executed successfully!
11. By the Read operation, you can make sure the value in the TravelAgency table has been really updated.
What’s next? You can choose either of: