Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
alrikx
Participant
1,846

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...😉

 

Problem

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😬

 

Resolution

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.

 

 

Final Remarks

  • You can use the custom type also in case you don’t use expand, e. g. when you have mixed scenarios with and without expand. In case you don’t request expand, the respective nested table is empty.
  • You can expand multiple associated entities in one call by extending your custom type with more nested tables.
  • In the latest version of SAP BTP, ABAP environment (Release 2311), when creating a service consumption model ABAP types in a generated class instead of abstract CDS entities are provided. They are flat structures, and you can also apply this trick when you read with expand option. To do so, use the local types instead of the CDS entities. The class also provides a table type definition for every entity, that you can reuse.

if you like this post or it helped you, don't hesistate to give kudos.

1 Comment
Labels in this area