I completed this mission on SAP Learning explaining how to connect Your SAP BTP, ABAP environment to an external OData Service. Here you import the $metadata file in Eclipse and it generates the stubs and artifacts needed to interact with this remote service. Pretty cool...😉
After I’ve integrated a remote OData service, the consumption worked fine, till the point, I wanted to use the OData Option "$Expand". This blogpost explains how to do it based on an example.
The generated CDS Entity ZA_MATERIAL contains an association to entity ZA_TEXT:
define root abstract entity ZA_MATERIAL {
[...]
_Text : association [0..*] to ZA_TEXT on 1 = 1;
}
I want to read the Material and the associated texts in one request. This way you create a read request for the entity Material with the expand option for entity Text:
DATA: odata_client_proxy TYPE REF TO /iwbep/if_cp_client_proxy,
read_list_request TYPE REF TO /iwbep/if_cp_request_read_list,
read_list_response TYPE REF TO /iwbep/if_cp_response_read_lst,
lt_business_data TYPE TABLE OF ZA_MATERIAL.
[...]
read_list_request = odata_client_proxy->create_resource_for_entity_set( 'MATERIAL' )->create_request_for_read( ).
DATA(lr_node) = read_list_request->create_expand_node( ).
lr_node->add_expand( iv_expand_property_path = '_TEXT' ).
read_list_response = read_list_request->execute( ).
read_list_response->get_business_data( IMPORTING et_business_data = lt_business_data ).
The problem is that in ABAP the associations are not available in lt_business_data and I was not able to get the expanded entities from method
read_list_response->get_business_data.
What a pitty...I thought first😬
I found out that the missing piece is the correct data type of the table, that I hand over to method get_business_data. I’ve created my own types with a slight deviation:
TYPES tt_sap_mat_txt TYPE STANDARD TABLE OF ZA_TEXT WITH NON-UNIQUE DEFAULT KEY.
TYPES BEGIN OF ty_sap_material.
INCLUDE TYPE ZA_MATERIAL.
TYPES: _Text TYPE tt_sap_mat_txt,
END OF ty_sap_material.
Now I replaced the direct use of the CDS entity with my custom type.
DATA: odata_client_proxy TYPE REF TO /iwbep/if_cp_client_proxy,
read_list_request TYPE REF TO /iwbep/if_cp_request_read_list,
read_list_response TYPE REF TO /iwbep/if_cp_response_read_lst,
"THIS WAS THE ORIGINAL LINE:
"lt_business_data TYPE TABLE OF ZA_MATERIAL.
"HERE THE CHANGED LINE WITH MY OWN TYPE:
lt_business_data TYPE TABLE OF ty_sap_material.
[...]
read_list_request = odata_client_proxy->create_resource_for_entity_set( 'MATERIAL' )->create_request_for_read( ).
DATA(lr_node) = read_list_request->create_expand_node( ).
lr_node->add_expand( iv_expand_property_path = '_TEXT' ).
read_list_response = read_list_request->execute( ).
read_list_response->get_business_data( IMPORTING et_business_data = lt_business_data ).
Finally I can access the expanded entity’s data, as a nested table, e. g.:
Data(lv_lang) = lt_business_data[ 1 ]-_text[ 1 ]-Language.
if you like this post or it helped you, don't hesistate to give kudos.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 | |
2 |