0% found this document useful (0 votes)
11 views

5 CRUD practical full

This document outlines the steps to create and manage an OData service using SAP Gateway, including creating a project, defining entity types, generating classes, and implementing CRUD operations. It provides detailed instructions for redefining methods, handling data retrieval and manipulation, and managing exceptions. The document also includes code snippets for implementing the service methods and examples for filtering and executing requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

5 CRUD practical full

This document outlines the steps to create and manage an OData service using SAP Gateway, including creating a project, defining entity types, generating classes, and implementing CRUD operations. It provides detailed instructions for redefining methods, handling data retrieval and manipulation, and managing exceptions. The document also includes code snippets for implementing the service methods and examples for filtering and executing requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

1.

​ Go to SEGW

2.​ Create Project

3.​ Below project get created


4.​ Create Entity type
Dbl clk on properties and append fields
Below entity type and entity set get generated

5.​ Generate project using this icon

This will generate MPC and DPC classes.


6.​ In Runtime artifacts you will get below classes
a.​ In service implementations you will get below operations
7.​ Go inside DPC_EXT class
a.​ Inside that class you get below methods. We have to redefine that methods.
Right click and redefine.
8.​ Redefine Get_entity
9.​ Put below code there

method FI01_BANKSET_GET_ENTITY.
READ TABLE it_key_tab INTO DATA(wa_key_tab)
WITH KEY name = 'COUNTRY'."read it_key_tab it_key_tab

if sy-subrc EQ 0.
DATA(lv_COUNTRY) = wa_key_tab-value.
ENDIF.

READ TABLE it_key_tab INTO wa_key_tab


WITH KEY name = 'KEY'."read it_key_tab it_key_tab

if sy-subrc EQ 0.
DATA(lv_KEY) = wa_key_tab-value.
ENDIF.

select SINGLE BANKS, BANKL, BANKA, PROVZ FROM BNKA


INTO @ER_ENTITY WHERE BANKS = @lv_COUNTRY and BANKL = @lv_KEY."read
from table and
"pass to er_entity

endmethod.

10.​Activate code
11.​Activate and maintain service - /n/iwfnd/maint_service
12.​Put initials of service and press get service. Double clk on your service name.
https://www.youtube.com/watch?v=K0Qu8SGnaT8&list=PLmajIBPiks11NU41T8G149D5hf-o1o
Bqs&index=5 17.47
13.​Dbl clk on service Below screen comes with details. Press next.
14.​Service get added
15.​Go back find your service and press SAP Gateway Client
16.​Below screen will popup
17.​Select GET radio button and press entity set

18.​See code and pass parameter accordingly and press Execute


a.​ (COUNTRY='DE',KEY='10000001')

19.​We receive below output


20.​For get entity set write below code after redefining it.

method FI01_BANKSET_GET_ENTITYSET.

select BANKS, BANKL, BANKA, PROVZ FROM BNKA


INTO TABLE @ET_ENTITYSET UP TO 2 ROWS.

Endmethod.

/sap/opu/odata/sap/ZJI_CRUD1_SRV/FI01_BANKSet
21.​To pass parameters to get_entityset method we use $filter method
a.​ It is a case sensitive method.
b.​ ?$filter=COUNTRY eq 'US' and KEY eq '011000390'
c.​ Write below code to accesses filter using select options
method FI01_BANKSET_GET_ENTITYSET.
DATA : LV_COUNTRY TYPE BANKS.
DATA : LV_KEY TYPE BANKL.

DATA : WA_FILTER_OPTIONS TYPE /IWBEP/S_MGW_SELECT_OPTION.


DATA : IT_FILTER_VALUES TYPE /IWBEP/T_COD_SELECT_OPTIONS.
DATA : IT_FILTER_VALUE TYPE /IWBEP/T_COD_SELECT_OPTIONS.
*In it_filter_select_options there is work area for you property name
READ TABLE it_filter_select_options INTO wa_filter_options WITH KEY property = 'COUNTRY'.
IF SY-subrc EQ 0.
*from that work area get select options table
it_filter_values = wa_filter_options-select_options[].
*put that select option table into work area as it have only one value
READ TABLE it_filter_valueS INTO DATA(wa_filter_VALUES) INDEX 1.
IF SY-subrc EQ 0.
*read the value in LOW
LV_COUNTRY = wa_filter_values-LOW.
ENDIF.
ENDIF.

READ TABLE it_filter_select_options INTO wa_filter_options WITH KEY property = 'KEY'.


IF SY-subrc EQ 0.
it_filter_values = wa_filter_options-select_options[].
READ TABLE it_filter_valueS INTO wa_filter_VALUES INDEX 1.
IF SY-subrc EQ 0.
LV_KEY = wa_filter_values-LOW.
ENDIF.
ENDIF.

select BANKS, BANKL, BANKA, PROVZ FROM BNKA


INTO TABLE @ET_ENTITYSET UP TO 2 ROWS WHERE banks = @lv_country AND BANKL = @LV_KEY.
*
* select BANKS, BANKL, BANKA, PROVZ FROM BNKA
* INTO TABLE @ET_ENTITYSET UP TO 2 ROWS.

endmethod.

22.​
23.​https://www.youtube.com/watch?v=K0Qu8SGnaT8&list=PLmajIBPiks11NU41T8G149D5
hf-o1oBqs&index=5 19
24.​CREATE
a.​ Redefine CREATE_ENTITY method

Here we get below data types for code


Code
method FI01_BANKSET_CREATE_ENTITY.
BREAK-POINT.
DATA BANK_CTRY TYPE BAPI1011_KEY-bank_ctry.
DATA BANK_KEY TYPE BAPI1011_KEY-bank_key.
DATA BANK_ADDRESS TYPE bapi1011_address.
DATA RETURN TYPE BAPIRET2.

*create internal table from entity_type you defined


*entity_type - ts_fi01_bank
*entity_set - TT_FI01_BANK
DATA INPUT_DATA TYPE zcl_zji_crud1_mpc=>ts_fi01_bank.

*get data from request into input_data


CALL METHOD io_data_provider->read_entry_data
IMPORTING
es_data = INPUT_DATA.

*after reciving data do what ever you want to do.


BANK_CTRY = input_data-banka.
BANK_KEY = input_data-bankl.
BANK_ADDRESS-bank_name = input_data-banka.
bank_address-region = input_data-provz.
DATA wa_BNKA TYPE BNKA.
wa_bnka-banka = BANK_CTRY.
wa_bnka-bankl = BANK_KEY.
*CODE 1
MODIFY BNKA FROM wa_bnka.
*CODE 2
CALL FUNCTION 'BAPI_BANK_CREATE'
EXPORTING
bank_ctry = BANK_CTRY
BANK_KEY = BANK_KEY
bank_address = BANK_ADDRESS
IMPORTING
RETURN = return .
IF return-type = 'E'.
RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
EXPORTING
textid = /iwbep/cx_mgw_busi_exception=>business_error
message = return-message .
ELSE.

ENDIF.
endmethod.

25.​(COUNTRY='DE',KEY='10000001')
26.​Exception
a.​ There are 2 exception class. As per you call BAPI or insert operation DB see its
output then raise exception
i.​ Business exception
ii.​ Technical exception
b.​
27.​UPDATE
a.​ Use filter options for filter data key to update
b.​ From workarea use data to direct modify
method FI01_BANKSET_UPDATE_ENTITY.
BREAK-POINT.

DATA BANK_CTRY TYPE BAPI1011_KEY-bank_ctry.


DATA BANK_KEY TYPE BAPI1011_KEY-bank_key.
DATA BANK_ADDRESS TYPE bapi1011_address.
DATA RETURN TYPE BAPIRET2.

*create internal table from entity_type you defined


*entity_type - ts_fi01_bank
*entity_set - TT_FI01_BANK
DATA INPUT_DATA TYPE zcl_zji_crud1_mpc=>ts_fi01_bank.

*get data from request into input_data


CALL METHOD io_data_provider->read_entry_data
IMPORTING
es_data = INPUT_DATA.

*after reciving data do what ever you want to do.


BANK_CTRY = input_data-banka.
BANK_KEY = input_data-bankl.
BANK_ADDRESS-bank_name = input_data-banka.
bank_address-region = input_data-provz.

DATA wa_BNKA TYPE BNKA.


wa_bnka-banka = BANK_CTRY.
wa_bnka-bankl = BANK_KEY.
*CODE 1
MODIFY BNKA FROM wa_bnka.
*CODE 2
*CALL FUNCTION 'BAPI_BANK_CREATE'
* EXPORTING
* bank_ctry = BANK_CTRY
* BANK_KEY = BANK_KEY
* bank_address = BANK_ADDRESS
* IMPORTING
* RETURN = return .
*IF return-type = 'E'.
* RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
* EXPORTING
* textid = /iwbep/cx_mgw_busi_exception=>business_error
* message = return-message .
*ELSE.
*
*ENDIF.
endmethod.

28.​Delete - same like update


29.​For now refer our old file
a.​ Use as request - button
b.​ In an HTTP request you have to edit the data.
30.​

You might also like