Friday 30 July 2021

Display Messages in ODATA Header Response while success and update and usage of ALL Query operations.

Introduction

This article explains the gradual creation of OData services, mapping messages on the OData response header, and simple operations($filter, $orderby, $inline count, $skip and $top).

Overview

1) Creation of OData service

Go to transaction code – SEGW.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Click on Icon Create. A pop window will appear, Fill the details as per below mention in screen shot and click on check icon or enter.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Below screen will appear where you can see below folder in project. In Folder data model, we can see three sub folders. Entity Type – it acts as work area, Entity Sets -It act as internal table and associations.
Now we are going to define structure of work area and internal table, Right click on Data model select import and select DDIC structure.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Here give the details of structure and structure name as per below screen below.
Select radio button Entity type and click on check box entity set. fill ABAP structure as required and click on next.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Now you will get the popup screen with provided table fields name. Select fields for your structure and click on next.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Other window will appear where we have to select the key field. Please select the key field
and click on finish.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Let’s generate Runtime Artifacts. Click on generate runtime objects button. It will display a popup and it will ask for a package, provide it. Keep the default class names as-is and click on enter button.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

On successful generation, you will see this kind of message log and generated artifacts. 4 classes will get generated. 2 for Data provider and 2 for Model provider class.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

2) Registering the Service


Go to transaction code /IWFND/MAINT_SERVICE and Click on Add services button.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Below screen will appear. Provide system Alias and click on Get services button.

Now select your service and click on Add selected Services.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

The below screen will appear, enter the package details and click on tick icon. An Information message will be shown where it will confirm about the service is created and metadata loaded successfully.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Now click back and go to main screen of transaction /IWFND/MAINT_SERVICE and find your service. Click on SAP Gateway Client and actually test your service.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Click on execute button and you can see your first created OData in action.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

If you want to see your output in JSON, just change $format=xml to $format=Json. You can see these options in ADD URI OPTION button.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Now we will write code to get data.

3) Redefine methods to write the code


Go to transaction code SEGW and open Runtime artifacts folder and right click on Class ZCL_ZODATA_SERVICE_DPC_EXT and select Go to ABAP Workbench option. Select edit mode and redefine method SOHEADERSET_GET_ENTITYSET. Write code in the methods. Save and activate all related objects for classes.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

The corresponding Methods displayed like

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Again Go to The Transaction /IWFND/MAINT_SERVICE and find your service.
Click on SAP Gateway Client and select the Entity set from the entity sets button and execute, you will get the header data in response.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

4) Simple operations of OData


$count,$filter,$orderby,$inlinecount,$top and $skip operations using OData services

SOURCE CODE :

SELECT * FROM zsodetails into CORRESPONDING FIELDS OF TABLE ET_ENTITYSET UP TO  3 ROWS.


***The below method is for Filter conditions
       CALL METHOD /IWBEP/CL_MGW_DATA_UTIL=>FILTERING
       EXPORTING
       IT_SELECT_OPTIONS = IT_FILTER_SELECT_OPTIONS
       CHANGING
       CT_DATA = ET_ENTITYSET.


*** The below method is for Skip and Top Functions
       CALL METHOD /IWBEP/CL_MGW_DATA_UTIL=>PAGING
       EXPORTING
       IS_PAGING = IS_PAGING
       CHANGING
       CT_DATA = ET_ENTITYSET.


***The below method is for Ascending and Descending order
        CALL METHOD /IWBEP/CL_MGW_DATA_UTIL=>orderby
        EXPORTING
        IT_ORDER = IT_ORDER
        CHANGING
        CT_DATA = ET_ENTITYSET.

***The below logic is for inline count

        IF io_tech_request_context->has_inlinecount( ) = abap_true.
        DESCRIBE TABLE et_entityset LINES es_response_context-inlinecount.
        ELSE.
        CLEAR es_response_context-inlinecount.

        ENDIF.

$Filter:-

Filter option is used to limit the results.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

$Inline count:-

Inline count means it displays the data with count as below

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

$Top:-

top=1 means first 1 record will be displayed in the data

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

$Skip:-

Skip1 means it skips one record while displaying.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

$Skip and Top:-

Top 2 means it will display the first two records and skip 1 means only one record skip in the data.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

$Count:-

It gets all the records count of the data

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

$orderby:-

Displays data either Ascending or Descending order.

Descending Order:-

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Ascending order:-

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

5) Add Success/ Error Message


Success/Error messages on ODATA Header response.

SOURCE CODE:

DATA lo_message_container TYPE REF TO /iwbep/if_message_container.

lo_message_container = me->mo_context->get_message_container( ).

"only to display message text.

CALL METHOD lo_message_container->add_message_text_only
EXPORTING
iv_msg_type = 'S'
iv_msg_text = 'SUCCESS'
iv_add_to_response_header = abap_true.

"To display msg id in combination with message text.

lo_message_container = me->mo_context->get_message_container( ).
lo_message_container->add_message(
EXPORTING
iv_msg_type = 'S'
iv_msg_id = 'ZMSG1'
iv_msg_number = '1'
iv_msg_v1 = 'Success'
iv_is_leading_message = abap_true
iv_add_to_response_header = abap_true
).

"To display msg id in combination with message text by using  add_message_from_bapi “
 method SOHEADERSET_UPDATE_ENTITY.

    DATA(lt_headers) = /iwbep/if_mgw_conv_srv_runtime~get_dp_facade( )->get_request_header( ).
    DATA(lv_request_uri) = lt_headers[ name = '~request_uri' ]-value.
    DATA(lv_context_path) = match( val = lv_request_uri regex = `/([^\/]+)\/?$` ).
    DATA(lv_message_target) = lv_context_path && |/Field1|. "/MyEntity('Key')/Field1

DATA lt_msg_types TYPE TABLE OF char1.

  io_data_provider->read_entry_data( IMPORTING es_data = ls_request_input_data ).

** Update fields of table Zsodetails

    UPDATE zsodetails SET
                          time     = ls_request_input_data-time
                          ernam     = ls_request_input_data-ernam
                          name1   = ls_request_input_data-name1
                          WHERE Vbeln  = lv_Vbeln.
    IF sy-subrc = 0.
      er_entity = ls_request_input_data. "Fill exporting parameter ER_ENTITY
*     er_entity-show_warning = 'X'.
*     er_entity-show_Info = 'X'.
      er_entity-show_success = 'X'.
*     er_entity-show_error = 'X'.
    ENDIF.

*** determine which messages to show

    IF er_entity-show_warning = abap_true.
      APPEND /iwbep/if_message_container=>gcs_message_type-warning TO lt_msg_types.
    ENDIF.
    IF er_entity-show_info EQ abap_true.
      APPEND /iwbep/if_message_container=>gcs_message_type-info TO lt_msg_types.
    ENDIF.
    IF er_entity-show_success EQ abap_true.
      APPEND /iwbep/if_message_container=>gcs_message_type-success TO lt_msg_types.
    ENDIF.
    IF er_entity-show_error EQ abap_true.
      APPEND /iwbep/if_message_container=>gcs_message_type-error TO lt_msg_types.
    ENDIF.

    DATA lt_return TYPE STANDARD TABLE OF bapiret2.
  LOOP AT lt_msg_types ASSIGNING FIELD-SYMBOL(<fs_msg_type>).

 " create bapi message
      APPEND INITIAL LINE TO lt_return ASSIGNING FIELD-SYMBOL(<fs_return>).

      MESSAGE ID '00' TYPE <fs_msg_type> NUMBER 001
         INTO <fs_return>-message
*        WITH |message { sy-tabix } type { <fs_msg_type> } value { er_entity-Vbeln }|.
         WITH |Error { <fs_msg_type> } value { er_entity-Vbeln }|.

      CALL FUNCTION 'BALW_BAPIRETURN_GET2'
        EXPORTING
          type   = sy-msgty
          cl     = sy-msgid
          number = sy-msgno
          par1   = sy-msgv1
        IMPORTING
          return = <fs_return>.

*--- only link Errors and Warning to the target field

 <fs_return>-field=COND#(WHEN<fs_msg_type>=/iwbep/if_message_container=>gcs_message_type-warning THEN
                   lv_message_target
                   WHEN <fs_msg_type> = /iwbep/if_message_container=>gcs_message_type-error THEN 
                   lv_message_target ELSE || ).
    ENDLOOP.

  LOOP AT lt_return ASSIGNING <fs_return>.
      " add message from bapi structure
      mo_context->get_message_container( )->add_message_from_bapi(
      EXPORTING is_bapi_message   = <fs_return>
        iv_entity_type = iv_entity_set_name
        it_key_tab = VALUE /iwbep/t_mgw_name_value_pair( ( name = 'Vbeln' value = er_entity-Vbeln ) )
        iv_add_to_response_header = abap_true
        iv_message_target = CONV string( <fs_return>-field ) ).
   ENDLOOP.
     MODIFY zsodetails FROM er_entity.
  endmethod.

Displaying messages by using add_messages method.

Success:-

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Displaying messages by using add_message_from_bapi Method.

Success:-

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Warning:-

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Error:-

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Note:- If the below error is encountered while executing the ODATA service please follow the below steps to resolve.

6) Configuration settings to resolve HTTP proxy error


SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Go to transaction SICF (HTTP service Hierarchy maintenance)

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Go to client -> Proxy settings

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Please uncheck “Proxy setting is active” and it will resolve the issue.

SAP OData, SAP ABAP, ABAP Environment, SAP ABAP Career, SAP ABAP Tutorial and Material, SAP ABAP Exam Prep, SAP ABAP Guides

Source: sap.com

No comments:

Post a Comment