Thursday, February 13, 2020

ODATA Service UPDATE, DELETE & MODIFY Example in SAP ABAP.


Details about How to  record into table using GET_ENTITY method in OData service.
Table: SCARR
Step 1: Go to the T-Code: SEGW → Create New ODATA Object



Step 2: Created Project will appear Data model → Import → DDIC structure.



Provide the Entity name and Structure name and select the fields you want to use in oData service. At least one field of Entity should be Key field, selecting CARRID here as Key and click on Finish.





 Step 3: Project is created successfully after that click on below marked button to Generate Run time Artifacts.


 Step 4: After You’ll get following Success message after Generate Run time Artifacts.


 Step 5:After that go to Data Provider Extension (DPC_EXT) Class → Double click on DPC_EXT class ZCL_ZMTEST5_UMD_DPC_EXT.


 Step 6: After that go to GET_ENTITY method SCARRSET_GET_ENTITY → Click on the  Redefine button to Redefine GET_ENTITY method. After you  implementing this method to fetch records so that Updated / Modified & Deleted records can be validate.



 Code in GET_ENTITY  Example:

FIELD-SYMBOLS <fs_keytab> TYPE /iwbep/s_mgw_name_value_pair.
 Table IT_KEY_TAB contains the Input value from Frontend & through
ER_ENTITY parameter we can send the records back to Frontend.
UNASSIGN <fs_keytab>.
READ TABLE it_key_tab ASSIGNING <fs_keytab> INDEX 1.
  IF sysubrc IS INITIAL.
    SELECT SINGLE 
      FROM scarr
      INTO CORRESPONDING FIELDS OF er_entity
      WHERE carrid EQ <fs_keytab>value.
    UNASSIGN <fs_keytab>.
  ENDIF.

 Step 7: After that go to  UPDATE_ENTITY method SCARRSET_UPDATE_ENTITY → Click on Redefine button to Redefine UPDATE_ENTITY method.



Code in UPDATE_ENTITY method.

DATA TYPE zcl_zmtest5_umd_mpc=>.
* Capture input value from Frontend into workarea WA_SCARR
  CLEAR wa_scarr.
  io_data_provider->read_entry_dataIMPORTING es_data wa_scarr ).
* Here we can use UPDATE or MODIFY depending on the requirement 
  UPDATE scarr SET carrname wa_scarrcarrname
              url      wa_scarrurl
             WHERE carrid   wa_scarrcarrid.
  IF sysubrc IS INITIAL.
    COMMIT WORK.
  ENDIF.

 Step 8: Place the cursor on DELETE_ENTITY method SCARRSET_DELETE_ENTITY → Click on Redefine button to Redefine DELETE_ENTITY method.

Write below code in DELETE_ENTITY method:
FIELD-SYMBOLS <fs_keytab> TYPE /iwbep/s_mgw_name_value_pair.
* Parameter IT_KEY_TAB holds the input record from Frontend
  UNASSIGN <fs_keytab>.
  READ TABLE it_key_tab ASSIGNING <fs_keytab> INDEX 1.
  IF sysubrc IS INITIAL.
    DELETE FROM scarr WHERE carrid EQ <fs_keytab>value.
    IF sysubrc IS INITIAL.
      COMMIT WORK.
    ENDIF.
    UNASSIGN <fs_keytab>.
  ENDIF.

 Step 9Now add the Service in T Code: /IWFND/MAINT_SERVICE. Please give a look to my previous post oData service which consumes RFC about how to add a service. You can find the Service name in Run time Artifacts. Here service name is: ZMTEST5_UMD_SRV.



 Step 10: After adding the Service,  to test the Service Goto T Code: /IWFND/GW_CLIENT, You’ll get the below screen:

Select the Click on EntitySets and select your entity

 Filtering Record with CARRID = ‘ZZ’. Add filter in URL: /sap/opu/odata/sap/ZMTEST5_UMD_SRV/SCARRSet(Carrid=’ZZ’)

After executing record with CARRID = ‘ZZ’ will be displayed as:

 Step 11: Testing UPDATE functionality: Click on Use as Request button to copy the HTTP Response contents to HTTP Request, which works as Input.






Status code is 204, means executed successfully.




 Table has been updated.


 Step 12: Testing DELETE functionality: Select DELETE Radiobutton → provide the filter in URL → Execute.


 Record with CARRID = ‘ZZ’ has been deleted from SCARR table.


 Step 13: Display custom messages after Database operations. 

Deleting record from Table SCARR with CARRID = ‘ZX’. 

 Below code to display custom messages:

      DATA:ls_header TYPE ihttpnvp.
      ls_headername  ‘Custom Message’.
      ls_headervalue ‘{msg_typ:S, desc:Record deleted  }’.
      /iwbep/if_mgw_conv_srv_runtime~set_headerls_header ).

 If you deleting the record with CARRID = ‘ZX’ you are getting custom message:




No comments:

Post a Comment