Pre-requisites
1.SAP user id with developer access
2.abap skill 3.Odata service created using SEGW.(If you dont know how to Please refer my previous post step-by-step-gateway-odata-service)
4.GET_ENTITYSET implemented already.(If you dont know how to Please refer my previous post http://www.techippo.com/2015/10/odata-service-implementation-for.html)
5.GET_ENTITY method implemented already(Refer OData Service Implementation for GET_ENTITY)
The Update operation is used when we want to update data on the backend server.The related HTTP method is PUT. If the Update operation is successful, you receive the HTTP 204 (no content) response code. Via the metadata, you can specify whether an entity set is updatable or not.In update operation you check the key and update the related resource.
In this tutorial we will use our flight service which was created in earlier tutorials.We will update a record in our SCARR table via OData service.We have seen how to create data in SAP Backend using OData Service in our previous tutorial.
Steps
- Open
the service and click on edit button.Expand Data Model->Entity Sets
and select our FlightSet.Then tick mark Updatable checkbox .Meaning is Updates of entries of the related entity type are supported.Dont forget to hit Generate Button.
- Expand Service Implementation node.Right click on Update and click on Go to Abap Workbench.
- You will get an information message that method is not implemented. Click OK.
- You will now land on class builder with ****_DPC_EXT opened.We write our code in this class.Expand Methods node.expand Inherited Methods node.Right click on FLIGHTSET_UPDATE_ENTITY and click Redefine.
- Paste the code to access the flight details provided and update record in SCARR table inside method(FLIGHTSET_UPDATE_ENTITY) and activate it.Using the read_entry_data method of the import object reference io_data_provider, you can retrieve the data that was passed along the PUT request in the HTTP body. The entry is retrieved in the format of the entity type definition.Now you can update related record in SCARR table.
- Update operation implemented.Now you can test it.For testing open tcode /IWFND/GW_CLIENT.Paste url(/sap/opu/odata/sap/ZTEST_ODATA_SRV_01/FlightSet('AI')) and select GET http method then click execute button. You will get details of flight with carrid AI.
- Now Click on Use as Request button in the right side.This is for copying the format of our entry data.Edit the data Select PUT http method.Do not change the url and press execute button.You will get a 204(No Content) response.
data: ls_flight like er_entity. io_data_provider->read_entry_data( IMPORTING es_data = ls_flight ). UPDATE SCARR FROM LS_FLIGHT.
If you enjoyed this post, Please Share!!
Thanks for the interesting post. What I don't get is why we have to write the id in the URL. In your case 'AI'. I have noticed that it has no effect what you write there, even if you write complete nonsense. Only the id in the payload seems to count. Can you please help out?
ReplyDeleteWhy we need to write the id? because then only it will invoke the update_entity method. If we use only 'FlightSet' in the url then update_entity method wont be invoked. Check it putting an external breakpoint. We should use meaningful id only. We need to edit the record with id AI. Thats why we have used AI id. For update operation we are first fetching the csrf token also.
DeleteThanks for the guide. However I got an error "The type of the database table and work area (or internal table) "LS_FLIGHT" are not Unicode convertible". What should I do now? Thanks in advance!
ReplyDeleteLS_FLIGHT is of type work area. May be you are treating it as an internal table?
Delete