You are on page 1of 61

SAP NetWeaver Gateway : A Step-by-Step Guide for Beginners

In three-tier architecture, SAP Gateways make up the middle or application tier. This middle layer is
essential for communication between the frontend and the backend. The use of multi-tier architecture to
implement a data processing system adds a valuable level of modularity and flexibility to the system by
being able to develop and maintain each tier individually. The purpose of this tutorial is to create a
Gateway that will extract data from the flight carrier database table, which will be accessed from the
Gateway Client. The Gateway will be able to retrieve a list of carriers as well as individual carriers. Lets
get started

Creating a New Project

First, navigate to the Gateway Service Builder with the T-Code SEGW
Click the Create Project button

Give the Gateway project a name and a description


Save the project and assign it to the appropriate package. For the purpose of this demo the project will be
saved as a local object ($TMP or click the button Local Object). The Gateway project has now been
successfully created

Creating an Entity & Entity Set

What is an Entity and Entity Set?


An Entity is a structure that can be defined by the user or defined as an ABAP Data Dictionary structure. An
Entity Set is simply a collection or table of Entities.
Right click Data Model and select Import -> DDIC Structure to use an ABAP Data Dictionary structure

Type SCARR for ABAP Structure and press enter. A list of properties should appear
Make Carrier the object name for the entity
Change the Usage of the property MANDT to Ignore and click the check mark at the bottom right

Double click the folder Entity Sets


Click Insert Row and name the Entity Set

The naming convention is to either make the Entity Set name the plural form of the name of the entity or
append _Set to the Entity name. For training purposes, name the entity set Carriers or Carrier_Set.
Carriers will be used for the remainder of this tutorial
Use the Entity name, Carrier, for Entity Type Name. Make sure to save and the Entity and corresponding
Entity Set have successfully created

How to Generate ABAP Classes

Click on the Generate Runtime Objects button towards the top left of the IDE
How to Activate Gateway Service

Navigate to the Activate and Maintain Services page, /iwfnd/maint_service, and click Add Service

Set System Alias to LOCAL and Technical Service Name to the name of the Gateway

Click Local Object and then the check button to save


Go back to the Activate and Maintain Services page, click on the service name, and click on Gateway
Client

To test the service, verify the HTTP Method is set to GET and then click Execute. There should now be
some auto-generated XML

In order to view the entity and its properties add a URI option to the end of the URI. Click Add URI Option
and use $metadata and sap-ds-debug=true
Now we can see the Entity Type as well as its properties

Congratulations! You have made a usable Gateway Service. Now the backend functionality
of the Gateway must be coded in order to make it useful.
Implementing GetEntitySet

Navigate back to the gateway service builder, expand the Service Implementation folder, and expand the
entity set. There will be a few auto-generated methods
Right click GetEntitySet, click Go to ABAP Workbench, and ignore the popup that follows. This will take
bring up the Class Builder

In the left menu, expand the Methods folder, right click on the GET_ENTITYSET method, and select
Redefine
Under Signature, what the method is exporting to the frontend service can be seen, ET_ENTITYSET. This
exporting table needs to be populated with data from the backend database
It is generally bad practice to select all the records from a database table because it can be extremely inefficient
and redundant so instead of using SELECT *, only select the first 100 records from the database using the
following statement

SELECT * FROM scarr INTO CORRESPONDING FIELDS OF TABLE et_entityset UP TO 100 ROWS.

Activate the method


To debug this code, set an external breakpoint. Session breakpoints will not work using the Gateway Client.
Now the method needs to be tested

Testing GetEntitySet

Reenter the Activate and Maintain Services or if it is already in a window click Refresh Catalog
Open the service again using the Gateway Client
Append the name of the Entity Set to the end of the URI, verify HTTP Method is set to GET, and execute.
There should now be multiple Carrier entries
Implementing GetEntity

To get an individual Carrier, the Get_Entity method must be implemented


In the Class Builder, right click CARRIERS_GET_ENTITY and select Redefine
Add the following code,

DATA: ls_key_tab LIKE LINE OF it_key_tab.


READ TABLE it_key_tab INTO ls_key_tab WITH KEY name = 'Carrid'.
SELECT SINGLE carrid carrname currcode url FROM scarr
INTO CORRESPONDING FIELDS OF er_entity
WHERE carrid = ls_key_tab-value.

The above code will select a Carrier using the Carrid that will be passed into the URI
Activate this method and open the Gateway Client one more time
Make sure HTTP Method is GET, type /sap/opu/odata/sap/Z_GATEWAY_DEMO_SRV/Carriers(AF)
for the URI, and press execute
There should now be an individual Carrier using the Carrid that was just passed in the URI

Congratulations! You have made your first Gateway Service.

2]
In this first part of tutorial, I am going to show how to prepare a data model using SEGW transaction. If you
got stuck, you can try to look at this official document that describes the process in more details.

Steps needed to create your first oData service

1. You need to prepare a database table that will be used for storing data. Below you can see the specific
database table which is used in this example.
2. You need to run the transaction (t-code) SEGW and create your first project.

3. Click on Data Model > Import from DDIC structure. Follow all the steps using your created database
table. At the end you should be able to see following table. Then select relevant keys and finish this process.
4. Creatable, updatable, deletable and addressable parameters need to be checked for your entity.

5. Now, you need to generate a runtime object. Select a property panel on your project by right click and
choose Generate runtime action.

6. In Service implementation > UserSet > GetEntitySet click by right and choose Go to ABAP Workbench.

7. The main goal now is to redefine relevant methods in USERS_DPC_EXT class. See content of redefined
methods below.

Implementation of redefined methods

USERSET_CREATE_ENTITY CREATE odata method

1 METHOD userset_create_entity.

2 DATA: ls_user TYPE zsa_users.

4 io_data_provider->read_entry_data( IMPORTING es_data = ls_user ).

5 INSERT INTO zsa_users VALUES ls_user.

7 er_entity = ls_user.

8 ENDMETHOD.

USERSET_GET_ENTITY GET odata method

1 METHOD userset_get_entity.
2 DATA: lt_keys TYPE /iwbep/t_mgw_tech_pairs,

3 ls_key TYPE /iwbep/s_mgw_tech_pair,

4 lv_email TYPE char100,

5 ls_user TYPE zsa_users.

7 lt_keys = io_tech_request_context->get_keys( ).

8 READ TABLE lt_keys WITH KEY name = 'EMAIL' INTO ls_key.

9 lv_email = ls_key-value.

10

11 SELECT SINGLE * FROM zsa_users INTO CORRESPONDING FIELDS OF ls_user WHERE email = lv_email.

12

13 er_entity = ls_user.

14 ENDMETHOD.

USERSET_GET_ENTITYSET GET odata method

1 METHOD userset_get_entityset.

2 DATA lt_users TYPE TABLE OF zsa_users.

4 SELECT * FROM zsa_users INTO TABLE lt_users.

5 et_entityset = lt_users.

6 ENDMETHOD.

USERSET_UPDATE_ENTITY PUT odata method

1 METHOD userset_update_entity.

2 DATA: ls_user TYPE zsa_users.


3

4 io_data_provider->read_entry_data( IMPORTING es_data = ls_user ).

5 UPDATE zsa_users SET

6 firstname = ls_user-firstname

7 lastname = ls_user-lastname

8 age = ls_user-age

9 address = ls_user-address

10 WHERE email = ls_user-email.

11

12 er_entity = ls_user.

13 ENDMETHOD.

USERSET_DELETE_ENTITY DELETE odata method

1 METHOD userset_delete_entity.

2 DATA: lt_keys TYPE /iwbep/t_mgw_tech_pairs,

3 ls_key TYPE /iwbep/s_mgw_tech_pair,

4 lv_email TYPE char100.

6 lt_keys = io_tech_request_context->get_keys( ).

7 READ TABLE lt_keys WITH KEY name = 'EMAIL' INTO ls_key.

8 lv_email = ls_key-value.

10 DELETE FROM zsa_users WHERE email = lv_email.

11 ENDMETHOD.

Testing your oData service

There is a gateway client which can be used for testing running services. Just run transaction(t-
code) /IWFND/GW_CLIENT and you can find out, if your service runs correctly.
Your oData service is running, now you can start to consume your data!

3]
step1) Open TCode SEGW and create a project as shown in below screen shot.
Provide the following details

Then the components like


1. 1. Data Model
2. 2. Service Implementation
3. 3. Runtime Artifacts
4. 4. Service Maintenance
Gets displayed automatically.

step2. Create an entity type as follows


Provide as following.

Click on properties as shown below.


Add the following values for header as shown below

Same way create entity type PR_Item for the item also and give the following values

step3. Create an entityset as shown below.

Give the following details


Then Header Entityset is created.

Same way create for Item Entityset is created.

step 4. Create a association as shown below.


And association set is automatically created.
step 5. Now Navigation is automatically created.

step 6.After completion of data model in Odata service in Service Implementation is filled automatically as shown in
below screen shot.
step 7.Now we need to generate runtime artifacts ,for that you need to select Runtime Artifacts and click on

Click OK and save it.

We get the following in Runtime Artifacts .


step 8. We need write in ZCL_Z_PURCHASE_REQUISI_DPC_EXT so double click on it .

step 9 A.Then we need to right click on the methods and redefine required methods in following process.

And write following code to get entity .


Code for Get Entity

method PRHEADERCOLLECTI_GET_ENTITY.

DATA: LS_KEY_TAB LIKE LINE OF IT_KEY_TAB.

READ TABLE it_key_tab INTO ls_key_tab WITH KEY name ='PRNumber'.

ER_ENTITY-prnumber = LS_KEY_TAB-VALUE.
* lv_pR_item = ls_key_tab-value.
**TRY.
*CALL METHOD SUPER->PRHEADERCOLLECTI_GET_ENTITY
* EXPORTING
* IV_ENTITY_NAME =
* IV_ENTITY_SET_NAME =
* IV_SOURCE_NAME =
* IT_KEY_TAB =
** io_request_object =
** io_tech_request_context =
* IT_NAVIGATION_PATH =
** IMPORTING
** er_entity =
** es_response_context =
* .
** CATCH /iwbep/cx_mgw_busi_exception .
** CATCH /iwbep/cx_mgw_tech_exception .
**ENDTRY.
endmethod.

9 B. Likewise redefine other required methods


PRITEMCOLLECTION_GET_ENTITYSET and deep insert also in same
way .

Code for GET_ENTITYSET


*** inactive new ***
METHOD PRITEMCOLLECTION_GET_ENTITYSET.
DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
lv_pr_number TYPE BANFN,
lv_pr_item TYPE BNFPO,
lt_pr_items_bapi TYPE TABLE OF BAPIEBAN,
ls_pr_item_bapi TYPE BAPIEBAN,
IT_RETURN TYPE STANDARD TABLE OF BAPIRETURN.
TYPES:
BEGIN OF ts_pr_item,
PRITEM type C length 5,
PURGROUP type C length 3,
MATERIAL type C length 18,
SHORTTEXT type C length 40,
PLANT type C length 4,
MATERIALGROUP type C length 9,
QUANTITY type P length 7 decimals 3,
UNIT type C length 3,
DOCUMENTTYPE type C length 4,
DELIVERYDATE type TIMESTAMP,
ITEMCATEGORY type C length 1,
ACCTASSIGNCATEGORY type C length 1,
PRNUMBER type C length 10,
END OF ts_pr_item.
*
DATA: es_entityset LIKE LINE OF ET_ENTITYSET.
*
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name ='PRNumber'.
lv_pR_number = ls_key_tab-value.
*
READ TABLE it_key_tab INTO ls_key_tab WITH KEY name ='PRItem'.
lv_pR_item = ls_key_tab-value.

CALL FUNCTION 'BAPI_REQUISITION_GETDETAIL'


EXPORTING
number = lv_pr_number
tables
requisition_items = lt_pr_items_bapi
RETURN = IT_RETURN
.
*DELETE lt_pr_items_bapi WHERE preq_item NE lv_pr_item.
*
READ TABLE lt_pr_items_bapi INTO ls_pr_item_bapi INDEX 1.
*
LOOP AT lt_pr_items_bapi INTO ls_pr_item_bapi.
*
es_entityset-PRITEM = ls_pr_item_bapi-PReq_ITEM.
es_entityset-PURGROUP = ls_pr_item_bapi-PUR_GROUP.
es_entityset-material = ls_pr_item_bapi-material.
es_entityset-SHORTTEXT = ls_pr_item_bapi-short_text.
es_entityset-plant = ls_pr_item_bapi-plant.
es_entityset-MATERIALGROUP = ls_pr_item_bapi-mat_grp.
es_entityset-quantity = ls_pr_item_bapi-quantity .
es_entityset-UNIT = ls_pr_item_bapi-unit .
es_entityset-DOCUMENTTYPE = ls_pr_item_bapi-doc_type .
es_entityset-ITEMCATEGORY = ls_pr_item_bapi-item_cat .
es_entityset-ACCTASSIGNCATEGORY = ls_pr_item_bapi-acctasscat .
es_entityset-PRNUMBER = ls_pr_item_bapi-preq_no .
es_entityset-DeliveryDate = ls_pr_item_bapi-deliv_date .

APPEND es_entityset TO et_entityset.


CLEAR: es_entityset.
*
ENDLOOP.
A. ENDMETHOD.

CODE FOR CREATE_DEEP_ENTITY

method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY.
**TRY.
*CALL METHOD SUPER->/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY
* EXPORTING
** iv_entity_name =
** iv_entity_set_name =
** iv_source_name =
* IO_DATA_PROVIDER =
** it_key_tab =
** it_navigation_path =
* IO_EXPAND =
** io_tech_request_context =
** IMPORTING
** er_deep_entity =
* .
** CATCH /iwbep/cx_mgw_busi_exception .
** CATCH /iwbep/cx_mgw_tech_exception .
**ENDTRY.

DATA: lv_new_pr_no TYPE BAPIEBANC-PREQ_NO.


* ls_new_pr_header TYPE BAPIMEREQHEADER.

DATA: ls_bapi_item TYPE bapiebanc,


lt_bapi_item TYPE TABLE OF bapiebanc,
lt_return TYPE TABLE OF bapiret2.

TYPES: ty_t_pr_items TYPE TABLE OF zcl_z_purchase_requisi_mpc=>ts_pr_item WITH DEFAULT


KEY.

TYPES: BEGIN OF ts_pr_items.


INCLUDE TYPE zcl_z_purchase_requisi_mpc=>ts_pr_header.
TYPES: PrItemCollection TYPE ty_t_pr_items,
END OF ts_pr_items.

DATA: lt_items TYPE zcl_z_purchase_requisi_mpc=>tt_pr_header,


ls_item TYPE zcl_z_purchase_requisi_mpc=>ts_pr_item,
lt1_items type ty_t_pr_items,
ls_pritems TYPE ts_pr_items.

DATA: ls_data TYPE ts_pr_items.

CALL METHOD io_data_provider->read_entry_data( IMPORTING es_data = ls_data ).


* ls_item-PRItemCollection = ls_data-PrItemCollection.
lt1_items = ls_data-PrItemCollection.
*append ls_item to it_items.
*clear ls_item.

* LOOP AT lt_items INTO ls_item.


LOOP AT lt1_items INTO ls_item.

ls_bapi_item-material = ls_item-material.
ls_bapi_item-plant = ls_item-plant.
ls_bapi_item-quantity = ls_item-quantity.
ls_bapi_item-doc_type = ls_item-DocumentType.
ls_bapi_item-DELIv_DATE = ls_item-DeliveryDate.
ls_bapi_item-PUR_GROUP = ls_item-PURGROUP.
ls_bapi_item-PREQ_ITEM = ls_item-PRITEM.
ls_bapi_item-SHORT_TEXT = ls_item-SHORTTEXT.
ls_bapi_item-MAT_GRP = ls_item-MATERIALGROUP.
ls_bapi_item-UNIT = ls_item-UNIT.
ls_bapi_item-ITEM_CAT = ls_item-ITEMCATEGORY.
ls_bapi_item-ACCTASSCAT = ls_item-ACCTASSIGNCATEGORY.
ls_bapi_item-PREQ_NO = ls_item-PRNUMBER.
* ls_itemx-po_item = ls_item-item.
APPEND ls_bapi_item TO lt_bapi_item.
* APPEND ls_itemx TO lt_itemx.
CLEAR ls_item.
ENDLOOP.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'


EXPORTING
INPUT = lv_new_pr_no
IMPORTING
OUTPUT = lv_new_pr_no
.
CALL FUNCTION 'BAPI_REQUISITION_CREATE'
* EXPORTING
* SKIP_ITEMS_WITH_ERROR =
* AUTOMATIC_SOURCE = 'X'
IMPORTING
NUMBER = lv_new_pr_no
TABLES
requisition_items = lt_bapi_item
RETURN = lt_return
.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'


EXPORTING
wait = 'X'.
* MOVE-CORRESPONDING ls_headerdata TO ls_pritems.
ls_pritems-prnumber = lv_new_pr_no.

copy_data_to_ref(
EXPORTING
is_data = ls_pritems
CHANGING
cr_data = er_deep_entity ).
endmethod.

step10: Now Service Maintenance is automatically created but we need to register the service. So select the system i.e.
EC7 and click on register.
Give the system Alias LOCAL_GW and click OK. Then Maintain The register
step 11.Test the service

Provide the following query


/sap/opu/odata/SAP/Z_PURCHASE_REQUISITION_TEST_SRV/PRHeaderCollection('0010003245')?$expand=PRItem
collection

Click on Use as Request.


Then you will get the response as request on left side
For Purchase Requisition creation you need to remove the Purchase Requisition number from left
side.

/sap/opu/odata/SAP/Z_PURCHASE_REQUISITION_TEST_SRV/PRHeaderCollection() in
gateway client and click on post
Purchase Requisition is created 0010017270 as shown in below screen shot.

Check in Table level entries in EBAN we can find the Purchase Requisition '0010017270'
4]
Implementing Expand Entity/Entity Set
Posted by Srikanth Gajula in SAP Gateway on Jul 18, 2014 7:21:30 AM

inShare1
Hi Folks,

Finally got some time to write a Blog on something which was a common question from a lot of audience here, so doing my bit
of help here .. hope this helps a lot of beginners and not for experts

SAP Documentation for Expand Understanding

https://help.sap.com/saphelp_gateway20sp08/helpdata/en/ca/c683e803494b77a2e1290b987556e2/content.htm

Some of the Blogs already posted in SCN relevant to Expand and Deep Entity .

Improved Inside-Out Modelling

Step by Step development for CREATE_DEEP_ENTITY operation

Requirement

Considering a basic scenario where i am using BAPI_PO_GETDETAIL which has multiple output tables and input is PO number

Now we shall start with SAP Gateway

Create Project in SEGW

Create three entity types and Entity Sets

Entity Type-1- Header


Entity Type-2- Item
Entity Type-3- Schedule
Entity Set-1- HeaderSet
Entity Set-2- ItemSet
Entity Set-3- ScheduleSet

Create Association

Association-1 - AssItem (Without key fields mapping)


Association-2 - AssSchedule (Without key fields mapping)

Create Navigation

Navigation-1 - NavItem
Navigation-2 - NavSchedule
Lets generate runtime artifacts. Click on generate runtime objects button. It will display
popup . Keep the default class names as-is and click on enter button.
Once generation is successful, you will get 4 classes. 2 for Data provider and 2 for Model provider.

we have to Redefine the method/IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET

Code Snippet

METHOD /iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.

*-------------------------------------------------------------------------*
* Deep Structure
*-------------------------------------------------------------------------*
DATA: BEGIN OF ls_order_items.
INCLUDE TYPE zcl_zproj_982_mpc=>ts_header.
DATA: navitem TYPE STANDARD TABLE OF zcl_zproj_982_mpc=>ts_item WITH DEFAULT KEY.
DATA: navschedule TYPE STANDARD TABLE OF zcl_zproj_982_mpc=>ts_schedule WITH DEFAULT KEY,
END OF ls_order_items,
ls_item1 TYPE zcl_zproj_982_mpc=>ts_item,
ls_schedle1 TYPE zcl_zproj_982_mpc=>ts_schedule.

*-------------------------------------------------------------------------*
* Data Declarations
*-------------------------------------------------------------------------*
DATA : ls_item TYPE zcl_zproj_982_mpc_ext=>ts_item,
lt_item TYPE TABLE OF zcl_zproj_982_mpc_ext=>ts_item,
ls_sch TYPE zcl_zproj_982_mpc_ext=>ts_schedule,
lt_sch TYPE TABLE OF zcl_zproj_982_mpc_ext=>ts_schedule,
ls_header TYPE zcl_zproj_982_mpc_ext=>ty_header,
lthead TYPE STANDARD TABLE OF bapiekkol,
lshead TYPE bapiekkol,
lsitem TYPE bapiekpo,
ltitem TYPE STANDARD TABLE OF bapiekpo,
lv_filter_str TYPE string,
lt_filter_select_options TYPE /iwbep/t_mgw_select_option,
ls_filter TYPE /iwbep/s_mgw_select_option,
ls_filter_range TYPE /iwbep/s_cod_select_option,
ls_expanded_clause1 LIKE LINE OF et_expanded_tech_clauses,
ls_expanded_clause2 LIKE LINE OF et_expanded_tech_clauses,
lv_ebeln TYPE ebeln,
lt_order_items LIKE TABLE OF ls_order_items,
ltsch TYPE STANDARD TABLE OF bapieket,
lssch TYPE bapieket.

*-------------------------------------------------------------------------*
* Entity Set - HeaderSet
*-------------------------------------------------------------------------*
CASE iv_entity_set_name.
WHEN 'HeaderSet'.
LOOP AT it_filter_select_options INTO ls_filter.

LOOP AT ls_filter-select_options INTO ls_filter_range.


TRANSLATE ls_filter-property TO UPPER CASE.
CASE ls_filter-property.
WHEN 'PONUMBER'.
lv_ebeln = ls_filter_range-low.
WHEN OTHERS.
" Log message in the application log
me->/iwbep/if_sb_dpc_comm_services~log_message(
EXPORTING
iv_msg_type = 'E'
iv_msg_id = '/IWBEP/MC_SB_DPC_ADM'
iv_msg_number = 020
iv_msg_v1 = ls_filter-property ).
" Raise Exception
RAISE EXCEPTION TYPE /iwbep/cx_mgw_tech_exception
EXPORTING
textid = /iwbep/cx_mgw_tech_exception=>internal_error.
ENDCASE.
ENDLOOP.

ENDLOOP.
*-------------------------------------------------------------------------*
* Call Method-BAPI_PO_GETDETAIL
*-------------------------------------------------------------------------*

CALL FUNCTION 'BAPI_PO_GETDETAIL'


EXPORTING
purchaseorder = lv_ebeln
items = 'X'
schedules = 'X'
IMPORTING
po_header = lshead
* PO_ADDRESS =
TABLES
* PO_HEADER_TEXTS =
po_items = ltitem
po_item_schedules = ltsch.
*-------------------------------------------------------------------------*
* Fill Header Values to Deep Structure
*-------------------------------------------------------------------------*
ls_order_items-ponumber = lshead-po_number.
ls_order_items-ccode = lshead-co_code.
ls_order_items-doctype = lshead-doc_type.
*-------------------------------------------------------------------------*
* Fill Item values to Deep Structure
*-------------------------------------------------------------------------*
LOOP AT ltitem INTO lsitem.
CLEAR ls_item1.
ls_item1-ponumber = lsitem-po_number.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = ls_item1-ponumber
IMPORTING
output = ls_item1-ponumber.

ls_item1-poitem = lsitem-po_item.
ls_item1-material = lsitem-material.
APPEND ls_item1 TO ls_order_items-navitem.
ENDLOOP.
*-------------------------------------------------------------------------*
* Fill Schedule values to Deep Strcture
*-------------------------------------------------------------------------*
LOOP AT ltsch INTO lssch.
CLEAR ls_item1.
* ls_item1-ponumber = lsitem-po_number.
ls_schedle1-poitem = lssch-po_item.
ls_schedle1-serial = lssch-serial_no.
APPEND ls_schedle1 TO ls_order_items-navschedule.
ENDLOOP.
*-------------------------------------------------------------------------*
* Assign the Navigation Proprties name to Expanded Tech clauses
*-------------------------------------------------------------------------*
ls_expanded_clause1 = 'NAVITEM'.
ls_expanded_clause2 = 'NAVSCHEDULE'.
APPEND ls_expanded_clause1 TO et_expanded_tech_clauses.
APPEND ls_expanded_clause2 TO et_expanded_tech_clauses.
*-------------------------------------------------------------------------*
* Append Deep Strcture Values to Final Internal Table
*-------------------------------------------------------------------------*
APPEND ls_order_items TO lt_order_items.

*-------------------------------------------------------------------------*
* Send back Response to Consumer
*-------------------------------------------------------------------------*

copy_data_to_ref(
EXPORTING
is_data = lt_order_items
CHANGING
cr_data = er_entityset ).
WHEN OTHERS.
ENDCASE.
ENDMETHOD.

Coding Part Done....Lets move to Testing

Test Case 1:

URI : /sap/opu/odata/sap/ZPROJ_982_SRV/HeaderSet?$filter=PoNumber eq '4500000163'&$expand=NavItem

Test Case 2:

URI : /sap/opu/odata/sap/ZPROJ_982_SRV/HeaderSet?$filter=PoNumber eq '4500000163'&$expand=NavItem,NavSchedule


For Expand Entity :-

From the modelling point of view there wont be any changes


but in DPC we need to Redefine the method /iwbep/if_mgw_appl_srv_runtime~get_expanded_entity.
Also there would be a small change in Code , Like Below

*-------------------------------------------------------------------------*
* Deep Structure
*-------------------------------------------------------------------------*
DATA: BEGIN OF ls_order_items.
INCLUDE TYPE zcl_zproj_982_mpc=>ts_header.
DATA: navitem TYPE STANDARD TABLE OF zcl_zproj_982_mpc=>ts_item WITH DEFAULT KEY.
DATA: navschedule TYPE STANDARD TABLE OF zcl_zproj_982_mpc=>ts_schedule WITH DEFAULT KEY,
END OF ls_order_items,
ls_item1 TYPE zcl_zproj_982_mpc=>ts_item,
ls_schedle1 TYPE zcl_zproj_982_mpc=>ts_schedule.

copy_data_to_ref(
EXPORTING
is_data = ls_order_items
CHANGING
cr_data = er_entity ).

Hope this Blog now helps you to implement Expand Entity/Entityset

5]
Detailed step by step procedure for Creating Gateway
Service with all the CRUD Operations and testing them in
Service Explorer Part1
Posted by Nagesh A in SAP NetWeaver Gateway Developer Center on Dec 22, 2013 12:15:49 PM

inShare4

In this blog I will explain Creating Gateway service with all CRUD operations Before starting, I am expecting that you have
basic idea of gateway service and gateway service builder i.e SEGW

1. Create service in Gateway system with all the CRUD operations.(Create Read Update Delete)

Create project in SEGW (Gateway Service Builder).

Our Project looks as below.


Now we need to build our Data model, for that first Create the Entity type by importing an RFC
Interface.

Give the Entity type name, RFC destination and name of the RFC to be imported and click on next

Select the required properties for the entity type.


Set key property for the entity type, here we will make Material as key then click on Finish.

Our Entity type MATR and its properties look as below.


Create Entity set with necessary entity type.

Give appropriate Entity set name and select the necessary Entity type then click on continue.

Our Project looks as below.


After creating Entity set Service implementation also created with empty CRUD operations which needed to be implemented by
mapping with RFC/BOR interface.

Now we need save our changes within a request.

Now click on generate button to generate runtime artifacts.


Data provider and Model provider classes will be created. Click on ok.

Save the changes.


Runtime artifacts are generated and looks as below.
Now we need to implement all the CRUD operations in Service implementation.
Create
Read/Query
Update
Delete

CREATE: This is to create a new entry. Right click on create and choose Map to data source option.

Give the RFC destination name and select required RFC then click on ok.

Now click on propose mapping, the system will do mapping automatically or else we can do it manually for required fields. Here
no need to map all the properties of the entity type; we will map only the required properties. We will do the mapping by drag
drop from Function Module onto data source parameter of the Service operation.
Here all the required fields will have input mapping as below.

Here in our case we need to pass constant values for BASE_UOM, MATL_GROUP and BASIC_VIEW, we will do that as
follows. First append the rows and maintain values under constant value tab.

Once mapping is done save your changes. Now we will implement DELETE operation.
DELETE: Right click on Delete and choose Map to data source option.

Give the RFC destination name and select required RFC then click on ok. Here we are mapping
withBAPI_MATERIAL_SAVEDATA.

Do the required mapping as follows. Here we are passing Material, DelFlag, MatlGroup fields so do the mapping accordingly.

READ: Right click on Read and choose Map to data source option.

Give the required RFC and do the mapping as below. Here we are mapping Material, MatlDesc,
IndSector,
MatlType, MatlGroup, BaseUom fields. Except Material all are having output mapping.
Note: In READ operation all key fields should have input mapping, here in our example Material is key.

QUERY: Right click on Query and choose Map to data source option.
Note: In Query operation all key fields should have output mapping. Here we can also map ranges directly to pass range value for
selection.

Give the required RFC and do the mapping as below. Here we are mapping with BAPI_MATERIAL_GETLIST

Here we will map MatlDesc property with appropriate Function field and our key property Material with range table, for that
just drag drop that filed on to the data source field. Click on ok in the below screen.

Our mapping looks like below.


Here for display purpose we will again add Material property since all key fields should have output mapping in Query operation.
For this click on create button and choose the Material property and do the necessary mapping as shown below.
UPDATE: Right click on Update and choose Map to data source option.

Give the required RFC, Here we are mapping with BAPI_MATERIAL_SAVEDATA.Do the required mapping as follows. Here
we are going to update MatlGroup Field.

After completing all the implementations save the changes and check for syntax errors.

Now again click on Generate button to regenerate the runtime artifacts.

Check the success messages.

With this we have successfully created a gateway service with implementing all CRUD operations.
Detailed step by step procedure for Creating Gateway
Service with all the CRUD Operations and testing them in
Service Explorer Part2
Posted by Nagesh A in SAP NetWeaver Gateway Developer Center on Dec 22, 2013 12:16:30 PM

inShare8

In the first part Detailed step by step procedure for Creating Gateway Service with all the CRUD Operations and testing them in
Service Explorer Part1
I discussed the the below topic.

1.Create service in Gateway system with all the CRUD operations.(Create Read Update Delete).

Before starting, I am expecting that you are able to create gateway service or you have gone through my previous blog.In this
blog i will discuss about maintaining our service and testing it in ServiceExplorer.

Our Service Name: ZBPS_MATR_DEMO_SRV.


Now we need to Activate and Maintain our service, we will do that in transaction /IWFND/MAINT_SERVICE.
a. First we need to locate and add our service. Click on add service button.

Enter system alias and press enter. From the displayed list locate our service and click o it.
Save it in a package and click on Ok.
Select the request and click on Ok. Again click on ok. Come back and click on our service.

b. Now we need to add System alias to our service, i.e. systems with which we are going to interact.

Click on Create System alias button and enter the required system details and save the changes and
come back.
Save the changes in the request.
Now we can see our system alias details.

Now we will explore our service for that click on Explore service button.
Click on Execute to get Service Document .
Service Document: Describes the location and capabilities of one or more Collections.
Here we can check our Entity set name and we will get our Service URI.

Here our URI: http://<hostname>:<port>/sap/opu/odata/sap/ZBPS_MATR_DEMO_SRV/


Now come back and now choose Get Service Metadata option as shown below and click on execute to get the service mete data.

Now we will get our service Meta data.


Service Metadata Document (Metadata Document): Describes the data model (i.e. structure and organization of all the
resources) exposed as HTTP endpoints by an OData service.
Note the URI of our service and we will use it further.
URI: http:// <hostname>:<port>/sap/opu/odata/sap/ZBPS_MATR_DEMO_SRV/$metadata

Testing Our Service.

Now we will test our service in Gateway Client transaction for that is /IWFND/GW_CLIENT.
Paste our URI in Request URI field and click on Get HTTP Method.

Check the Service Meta data.


Now we will test all the CRUD operations.

READ:
Here in URI we can pass values for key fields only.
In our example we are trying to READ Material 000000000000000023 and we will do that in Gateway client.

URI: http:// <hostname>:<port>/sap/opu/odata/sap/ZBPS_MATR_DEMO_SRV/matrset('000000000000000023')

Paste this URI against Request URI field in Gateway client and select HTTP Method GET and click on execute.

Output:
QUERY:

Query is to get multiple entries and here we can pass ranges for selection.
In our example we will try to fetch Materials within the range 000000000000000023 to 000000000000000038.

URI:
http:// <hostname>:<port>/sap/opu/odata/sap/ZBPS_MATR_DEMO_SRV/matrset?$filter=Material ge '000000000000000023'
and Material le '000000000000000053'

Paste this URI @ Request URI in Gateway client and select HTTP Method GET and click on execute.

Output: We will get three materials in our output

CREATE:

Now we will try to create Material 000000000000000016. For this first we will READ an existing material and using that XML
we will CREATE desired material.
First try to READ Material 000000000000000023 by using the URI same as in our READ operation in Gateway client.

Now click on Use as request button as below.


We will get same XML in HTTP Request Body. Make require changes in that for Material creation. Here we need to select
HTTP Method POST for CREATE operation and we need to change URI as below.

URI:
http:// <hostname>:<port>/sap/opu/odata/sap/ZBPS_MATR_DEMO_SRV/matrset

OUTPUT:
UPDATE: Now we will try to UPDATE the Material just we have created through CREATE operation.

Do the same steps as in create operation, first read the Material 000000000000000016 through READ operation and by using
that xml as a Request we will try to UPDATE Material Group of that Material.

URI: http:// <hostname>:<port>/sap/opu/odata/sap/ZBPS_MATR_DEMO_SRV/matrset('000000000000000016')

Here MatlGroup is 00108, we will update that to 00107 and we will verify it through READ operation.
Click on Use as Request and make the necessary changes to the XML as below and select the HTTP Method PUT for Updating
and click on execute.
Xml:

<?xml version="1.0" encoding="utf-8"?>


<entry
xml:base="http:// <hostname>:<port>/sap/opu/odata/sap/ZBPS_MATR_DEMO_SRV/"xmlns="http://www.w3.
org/2005/Atom"xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"xmlns:d="http://sc
hemas.microsoft.com/ado/2007/08/dataservices">
<content type="application/xml">
<m:properties>
<d:BaseUom>EA</d:BaseUom>
<d:MatlGroup>00107</d:MatlGroup>
<d:BasicView>X</d:BasicView>
<d:MatlType>ROH</d:MatlType>
<d:IndSector>1</d:IndSector>
<d:Material>000000000000000016</d:Material>
<d:MatlDesc>Test material gateway</d:MatlDesc>
<d:LanguIso>EN</d:LanguIso>
<d:Langu>E</d:Langu>
</m:properties>
</content>
</entry>

OUTPUT:
Now we will check that whether that Material is updated or not by using READ operation. Select the HTTP Method GET and
check the output.

DELETE:
Now we will try to DELETE the Material which we have created, for this we use HTTP method DELETE.
Here to delete the Material we will just set the Delete Flag to X. First we will read that material and using that xml as a request
we will process Delete operation. Here we will select HTTP method DELETE and execute it after modifying xml.

XML:
<?xml version="1.0" encoding="utf-8"?>
<entry
xml:base="http:// <hostname>:<port>/sap/opu/odata/sap/ZBPS_MATR_DEMO_SRV/"xmlns="http://www.w3
.org/2005/Atom"xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"xmlns:d="http://s
chemas.microsoft.com/ado/2007/08/dataservices">
<content type="application/xml">
<m:properties>
<d:MatlGroup>00107</d:MatlGroup>
<d:Material>000000000000000019</d:Material>
<d:DelFlag>X</d:DelFlag>
</m:properties>
</content>
</entry>

OUTPUT:

With this we have completed all the CRUD operations.

You might also like