Extending Your SAP NetWeaver Gateway OData Service – Some ABAP Tips
So – you’ve decided to expose some SAP Business Suite data to outside applications through SAP NetWeaver Gateway. You’ve seen the ...Contact us
We’ll make use of these classes in the below example code. Note that *_DPC classes are the base, generated classes, while *_EXT classes are subclasses that can be extended for custom purposes.
Now, on to the information!
Know Your BAPIs
(The parameter $top tells you how big the page is, and $skip tells you where you are in the list. In this example the consuming application has pages of 3 contracts apiece, and is on the first page.)
Knowing the page size and skip values tells you how many rows you need to pass to your max_rows parameter. Even if you had the system generate your data access classes through the screens, Gateway’s generated *_DPC classes can be extended via the *_DPC_EXT classes which are also generated at service creation. For our purposes, the CONTRACTCOLLECTI_GET_ENTITYSET method is where we extend the base class to ensure we pull limited data back.
Here’s some simple sample code:
CHECK it_order IS INITIAL. “See note below
DATA: lv_max_rows TYPE bapimaxrow,
lv_lower_bound TYPE i,
lv_upper_bound TYPE i.
“is_paging is the structure that holds the $top and $skip parameters
lv_max_rows = is_paging-top + is_paging-skip.
“this is a contrived FM – substitute your own!
“note that *_DPC class generates the instance attribute t_contract
“based on the service implementation in SEGW
CALL FUNCTION ‘BAPI_CONTRACTLIST_GET’
max_rows = lv_max_rows
“your other parameters may need to be here
t_contract = t_contract.
“Now if $skip is a nonzero number, you can remove that many
“rows from the top of the table and return the result
lv_lower_bound = is_paging-skip.
IF lines( t_contract ) > is_paging-skip + is_paging-top.
lv_upper_bound = is_paging-skip + is_paging-top.
lv_upper_bound = lines( t_contract ).
LOOP AT t_contract INTO ls_contract
FROM lv_lower_bound TO lv_upper_bound.
“Logic goes here to move fields to parameter table et_entityset
Two things to note:
- Even if you implement your own data population logic without a call to an RFC, if you use ABAP Open SQL to retrieve records you can use the clause “UP TO lv_max_rows ROWS” in your SQL and keep the rest of the logic exactly the same.
- If the $orderby option is filled, you CANNOT use this logic. See the very first CHECK in the sample method.
$filter and $select
“First, retrieve the $select fields
“This is relatively easy, provided by a helper method.
“After invocation, lt_select contains a list of all
“fields needed for the corresponding SELECT statement,
“and to define which fields should be mapped back into
“the et_entityset exporting parameter.
DATA: lt_select TYPE /IWBEP/T_MGW_TECH_FIELD_NAMES.
lt_select = io_tech_request_context->get_select( ).
“Now the $filter data.
“This comes into the method as importing parameter
“it_filter_select_options. If we were filtering by
“contract ID, we could use code as follows:
DATA: ls_filter_select_options TYPE /iwbep/s_mgw_select_option,
lt_contract_range TYPE RANGE OF contract_id,
ls_contract_range LIKE LINE OF lt_contract_range,
ls_select_option TYPE /iwbep/s_cod_select_option.
“Since any number of select options can be in the table,
“we have to examine it and pull out the relevant information
“for the contract_id range.
LOOP AT it_filter_select_options INTO ls_filter_select_options.
IF ls_filter_select_options-property = ‘ContractID’.
LOOP AT ls_filter_select_options-select_options
MOVE-CORRESPONDING ls_select_option TO ls_contract_range.
APPEND ls_contract_range TO lt_contract_range.
Customizing Calls to Standard RFCs
iv_entity_name = iv_entity_name
iv_entity_set_name = iv_entity_set_name
iv_source_name = iv_source_name
it_filter_select_options = lt_filter_select_options
is_paging = is_paging
it_key_tab = it_key_tab
it_navigation_path = it_navigation_path
it_order = it_order
iv_filter_string = iv_filter_string
iv_search_string = iv_search_string
io_tech_request_context = io_tech_request_context
et_entityset = et_entityset
es_response_context = es_response_context.
- Tips for making sure that your NetWeaver Gateway service is configured for performance
- Tips for making sure that your SAPUI5 application makes good use of your Gateway services
- Writing ABAP code to consume an external RESTful web service and expose it to Gateway
Let’s make your SAP better, together.
We use the lab to bring ideas to life.
Being plugged into the local ecosystem is a true competitive advantage, so each Lab seeks out partners, universities, and startups to build relationships as a trusted peer and thought leader on topics of strategic importance to SAP.