Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Tisha_Haria
Participant
3,920

Introduction:

ABAP Restful Application Programming is an efficient and cloud-compatible development model that enables rapid creation of Fiori apps.

As a part of clean core guidelines, it is a best practice to used SAP released APIs to fetch the data within S4HANA system instead of directly querying on unreleased CDS views. 

Implementation

  • Create a local proxy instance to consume OData Version 2 'API_MATERIAL_STOCK_SRV' service.

 

 

DATA(lo_client_proxy)  = 
/iwbep/cl_cp_client_proxy_fact=>create_v2_local_proxy( VALUE #( service_id = 'API_MATERIAL_STOCK_SRV' service_version = '0001'  ) ) .

 

 

  • Create an instance of the entity list resource for entity set 'A_MatlStkInAcctMod'.

 

 

DATA(lo_entity_list_resource) = lo_client_proxy->create_resource_for_entity_set( 'A_MatlStkInAcctMod' ).

 

 

  • Create the read list request instance.

 

 

DATA(lo_read_list_request) = lo_entity_list_resource->create_request_for_read( ).

 

 

  • Call the method set_orderby of read list request to sort in the requested order.

 

 

          DATA(lt_sort_order) = CORRESPONDING /iwbep/if_cp_runtime_types=>ty_t_sort_order( it_sort_order MAPPING property_path = element_name ).
          lo_read_list_request->set_orderby( lt_sort_order  ).

 

 

  • Create the filter factory instance and pass the filter object to request instance to fetch the data based on filters requested. 

 

 

 DATA(lo_filter_factory) = lo_read_list_request->create_filter_factory( ).
    DATA: lo_filter_node_final TYPE REF TO /iwbep/if_cp_filter_node.
    LOOP AT it_filter ASSIGNING FIELD-SYMBOL(<lv_filter>).
      TRY.
          DATA(lo_filter_node) = io_filter_factory->create_by_range( iv_property_path = <lv_filter>-name it_range = <lv_filter>-range ).
          IF lo_filter_node_final IS BOUND .
            lo_filter_node_final = lo_filter_node_final->and( lo_filter_node ).
          ELSE .
            lo_filter_node_final = lo_filter_node .
          ENDIF. .
        CATCH /iwbep/cx_gateway.
          "handle exception
      ENDTRY.
    ENDLOOP.
    data(lo_filter) = lo_filter_node_final .
lo_read_list_request->set_filter( lo_filter ) .​

 

 

  • Implement the paging by setting the top and skip to request instance.

 

 

      IF lv_top > 0 .
          lo_read_list_request->set_skip( lv_skip ) .
          lo_read_list_request->set_top( lv_top ) .
        ENDIF.

 

 

  • Execute the read list request and gain the read list response instance:

 

 

     DATA(lo_read_list_response) = lo_read_list_request->execute( ).

 

 

  • Fetch the business data from the response object.

 

 

  lo_read_list_response->get_business_data( IMPORTING et_business_data = et_results ).

 

In this way all the other CRUD operations can be handled using local instance and HTTP client destination configuration can be avoided.

 

2 Comments