• ABAP Debugging Script(调试器脚本)使用的一些实际例子


    例子1:Use ABAP debugger script to view BOL entity content in an efficient way

    In CRM, if we could like to review a BOL entity content in debugger, for example consider the following sample code which fetches line item product of a given one order document:

    DATA:
          lo_collection      TYPE REF TO if_bol_entity_col,
          lv_view_name       TYPE crmt_view_name,
          lv_query_name      TYPE crmt_ext_obj_name,
          ls_parameter       TYPE genilt_query_parameters,
          lt_query_parameter TYPE genilt_selection_parameter_tab,
          ls_query_parameter LIKE LINE OF lt_query_parameter.
    
        ls_query_parameter-attr_name = 'OBJECT_ID'.
        ls_query_parameter-low = iv_oppt_id.
        ls_query_parameter-option = 'EQ'.
        ls_query_parameter-sign = 'I'.
        APPEND ls_query_parameter TO lt_query_parameter.
    
        ls_query_parameter-attr_name = 'PROCESS_TYPE'.
        ls_query_parameter-low = iv_process_type.
        ls_query_parameter-option = 'EQ'.
        ls_query_parameter-sign = 'I'.
        APPEND ls_query_parameter TO lt_query_parameter.
    
        so_core = cl_crm_bol_core=>get_instance( ).
        so_core->load_component_set( 'BT' ).
        lv_query_name = 'BTQ1Order'.
    
        DATA(lo_result) = so_core->dquery(
            iv_query_name               = lv_query_name
            is_query_parameters         = ls_parameter
            it_selection_parameters             = lt_query_parameter
            iv_view_name                = lv_view_name ).
    
        CHECK lo_result->size( ) = 1.
        DATA(lo_order_result) = lo_result->get_first( ).
    
        DATA(lo_bt_order) = lo_order_result->get_related_entity( 'BTADVS1Ord' ).
        CHECK lo_bt_order IS NOT INITIAL.
    
        DATA(lo_header) = lo_bt_order->get_related_entity( 'BTOrderHeader' ).
    
        CHECK lo_header IS NOT INITIAL.
    
        DATA(lo_items) = lo_header->get_related_entities( iv_relation_name = 'BTHeaderItemsExt' ).
        CHECK lo_items->size( ) = 1.
    
        DATA(lo_item) = lo_items->get_first( ).
    
        DATA(lo_admini) = lo_item->get_related_entity( 'BTItemsFirstLevel' ).
        CHECK lo_admini IS NOT INITIAL.
    
        DATA(lo_product) = lo_admini->get_related_entity( 'BTItemProductExt' ).
    

    If you would like to review the content of lo_product, you have to:

    (1) double click container_proxy:

    (2) double click DATA_REF:

    (3) double click:

    (4) double click ATTRIBUTE_REF:

    (5) double click:

    So totally you need to perform FIVE times double click in order to review content:

    Using ABAP debugger script

    (1) Click Script tab:

    (2) Create a new script:

    Choose a name for your script:

    (3) Use the following source code to overwrite the default source code:

    *---------------------------------------------------------------------*
    *       CLASS lcl_debugger_script DEFINITION
    *---------------------------------------------------------------------*
    *
    *---------------------------------------------------------------------*
    CLASS lcl_debugger_script DEFINITION INHERITING FROM  cl_tpda_script_class_super  .
    
      PUBLIC SECTION.
        METHODS: prologue  REDEFINITION,
          init    REDEFINITION,
          script  REDEFINITION,
          end     REDEFINITION.
        INTERFACES: if_tpda_script_w_input,
          if_tpda_script_w_output.
    
      PRIVATE SECTION.
        DATA: entity_name TYPE string.
        DATA: value TYPE string.
        DATA: output TYPE tpda_transfer_it_unsorted.
        DATA: bol_object_name TYPE crmt_ext_obj_name.
        METHODS get_attribute
          IMPORTING io_oref_descr     TYPE REF TO cl_tpda_script_orefdescr
                    iv_attribute_name TYPE string
          RETURNING VALUE(ro_descr)   TYPE REF TO cl_tpda_script_data_descr.
    
    ENDCLASS.                    "lcl_debugger_script DEFINITION
    *---------------------------------------------------------------------*
    *       CLASS lcl_debugger_script IMPLEMENTATION
    *---------------------------------------------------------------------*
    *
    *---------------------------------------------------------------------*
    CLASS lcl_debugger_script IMPLEMENTATION.
    
      METHOD prologue.
    *** generate abap_source (source handler for ABAP)
        super->prologue( ).
      ENDMETHOD.                    "prolog
    
      METHOD if_tpda_script_w_input~get_parameters.
    
        DATA lt_input      TYPE tpda_transfer_it.
        DATA ls_input      TYPE tpda_transfer_struc.
    
        ls_input-id   = 'ENTITY'.
        APPEND ls_input TO lt_input.
        p_parameters_it = lt_input.
    
      ENDMETHOD.                    "if_tpda_script_w_input~get_parameters
    
      METHOD if_tpda_script_w_input~set_parameter_values.
    
    *   Tabelle mit Inputparameter und Wert
        DATA lt_input     TYPE tpda_transfer_it.
        DATA ls_input     TYPE tpda_transfer_struc.
    
        lt_input = p_parameter_values_it.
        LOOP AT lt_input INTO ls_input.
          IF ls_input-id = 'ENTITY'.
            entity_name = ls_input-value.
          ENDIF.
        ENDLOOP.
    
      ENDMETHOD.                    "if_tpda_script_w_input~set_parameter_values
    
      METHOD init.
    *** insert your initialization code here
      ENDMETHOD.                    "init
    
      METHOD script.
    
        DATA lr_data_descr      TYPE REF TO cl_tpda_script_data_descr.
        DATA lr_struct_descr    TYPE REF TO cl_tpda_script_structdescr.
        DATA lr_cx              TYPE REF TO cx_root.
        DATA ls_quick           TYPE tpda_scr_quick_info.
        DATA lv_name            TYPE string.
        DATA lt_struct          TYPE tpda_scr_struct_comp_it.
        DATA ls_struct          TYPE tpda_scr_struct_comp.
        DATA ls_output          TYPE tpda_transfer_struc.
        DATA lr_symbsimple      TYPE REF TO tpda_sys_symbsimple.
        DATA ls_varinfo         TYPE tpda_quick_vars.
    
        FIELD-SYMBOLS: <lv_value> TYPE any.
    
        TRY.
            CLEAR output.
    
    *        BREAK-POINT.
    
            ls_varinfo = cl_tpda_script_data_descr=>get_variable_info( 'LO_PRODUCT' ).
    
    *       get object type name
            IF ls_varinfo-varvalue = 'OBJECT'.
    *         class instance passed directly
              lv_name = entity_name && '-CONTAINER_PROXY->DATA_REF->OBJECT_NAME'.
            ELSE.
    *         variable of class instance passed
              lv_name = ls_varinfo-varvalue && '-CONTAINER_PROXY->DATA_REF->OBJECT_NAME'.
            ENDIF.
    
            ls_quick = cl_tpda_script_data_descr=>get_quick_info( lv_name ).
            ASSIGN ls_quick-quickdata TO <lv_value>.
            lr_symbsimple ?= <lv_value>.
            bol_object_name = lr_symbsimple->valstring.
    
    *       get content
            IF ls_varinfo-varvalue = 'OBJECT'.
              lv_name = entity_name && '-CONTAINER_PROXY->DATA_REF->ATTRIBUTE_REF->*'.
            ELSE.
              lv_name = ls_varinfo-varvalue && '-CONTAINER_PROXY->DATA_REF->ATTRIBUTE_REF->*'.
            ENDIF.
    
            lr_data_descr = cl_tpda_script_data_descr=>factory( lv_name ).
            lr_struct_descr ?= lr_data_descr.
    
            lr_struct_descr->components(
              IMPORTING
    *            p_components_it      =
                p_components_full_it =  lt_struct
            ).
    
            LOOP AT lt_struct INTO ls_struct.
              ls_output-id = ls_struct-compname.
              TRY.
                  ASSIGN ls_struct-symbquick-quickdata TO <lv_value>.
                  lr_symbsimple ?= <lv_value>.
                  ls_output-value = lr_symbsimple->valstring.
                CATCH cx_root INTO lr_cx.
                  ls_output-value = lr_cx->get_text( ).
              ENDTRY.
              APPEND ls_output TO output.
            ENDLOOP.
    
            DATA lt_col_alv                   TYPE tpda_script_service_source_tab.
            DATA ls_col_alv                   LIKE LINE OF lt_col_alv.
            ls_col_alv-fieldname = ls_col_alv-content = 'ID'.
            APPEND ls_col_alv TO lt_col_alv.
            ls_col_alv-fieldname = ls_col_alv-content = 'VALUE'.
            APPEND ls_col_alv TO lt_col_alv.
    
            CALL METHOD cl_tpda_script_data_display=>data_display
              EXPORTING
                p_list_header = 'Query Selection Parameters'
                p_column_it   = lt_col_alv
                p_popup       = 'X'
              CHANGING
                p_data_it     = output.
    
    *        BREAK-POINT.
          CATCH cx_root INTO lr_cx.
            BREAK-POINT.                                       "#EC NOBREAK
            value = lr_cx->get_text( ).
        ENDTRY.
      ENDMETHOD.                    "script
    
      METHOD end.
    *** insert your code which shall be executed at the end of the scripting (before trace is saved)
    *** here
    
      ENDMETHOD.                    "end
    
      METHOD if_tpda_script_w_output~get_parameter_values.
    
        DATA lt_param TYPE tpda_transfer_it_unsorted.
        DATA ls_param TYPE tpda_transfer_struc.
    
        ls_param-id = 'VARIABLE'.
        ls_param-value = entity_name.
        APPEND ls_param TO lt_param.
        ls_param-id = 'OBJECT_NAME'.
        ls_param-value = bol_object_name.
        APPEND ls_param TO lt_param.
    
        APPEND INITIAL LINE TO lt_param.
    
        APPEND LINES OF output TO lt_param.
    
        p_parameter_values_it = lt_param.
    
      ENDMETHOD.                    "if_tpda_script_w_output~get_parameter_values
    
      METHOD get_attribute.
    
        DATA lr_oref_descr     TYPE REF TO cl_tpda_script_orefdescr.
        DATA lr_object_descr   TYPE REF TO cl_tpda_script_objectdescr.
        DATA ls_varinfo        TYPE tpda_quick_vars.
        DATA lv_longname       TYPE string.
    
        DATA lt_attributes TYPE tpda_script_object_attribut_it.
    
        lr_oref_descr   = io_oref_descr.
        lr_object_descr = lr_oref_descr->get_object_handle( ).
    
        lt_attributes = lr_object_descr->attributes( ).
    
        ro_descr = lr_object_descr->get_attribut_handle( lv_longname  ).
    
      ENDMETHOD.                    "get_oref_attribute
    
    ENDCLASS.                    "lcl_debugger_script IMPLEMENTATION
    

    Once done, save the script and choose “Execute Directly”.

    (4) Before you start Script by clicking button “Start Script”, make sure you use the correct variable name used in your ABAP code. In my example, it is “LO_PRODUCT”.


    Once done, click button “Start Script”:
    the BOL entity content is now automatically displayed, without five times double click any more.

    例子2:Use ABAP debugger script to view dynamic query service selection parameter in an efficient way

    In WebUI we can maintain search parameter for dynamic search:

    The value maintained in WebUI could be found from dynamic query service instance in backend via debugging. Double click variable qs:

    We need the following FIVE steps to see the selection parameter value.

    (1) double click SELECTION_PARAM_COL:

    (2) Double click ENTITY_LIST:

    (3) These four entities represent the four selection parameters we see in WebUI. Double click one of them:

    (4) Double click PARAMETER_DATA:

    (5) double click:

    Finally we see the value:

    Use ABAP debugger script to directly review variable content without so many double clicks

    (1) Click Script tab, create a new Script:

    (2) Choose a name for your script:

    Paste the following source code to overwrite automatically generated source code:

    *---------------------------------------------------------------------*
    *       CLASS lcl_debugger_script DEFINITION
    *---------------------------------------------------------------------*
    *
    *---------------------------------------------------------------------*
    CLASS lcl_debugger_script DEFINITION INHERITING FROM  cl_tpda_script_class_super  .
    
      PUBLIC SECTION.
    
        METHODS: prologue REDEFINITION,
                 init     REDEFINITION,
                 script   REDEFINITION,
                 end      REDEFINITION.
    
        INTERFACES: if_tpda_script_w_input.
    
    
      PRIVATE SECTION.
    
        DATA      queryservicename TYPE string.
        CONSTANTS querydefaultname TYPE string VALUE 'QUERY_SERVICE'.
    
    ENDCLASS.                    "lcl_debugger_script DEFINITION
    *---------------------------------------------------------------------*
    *       CLASS lcl_debugger_script IMPLEMENTATION
    *---------------------------------------------------------------------*
    *
    *---------------------------------------------------------------------*
    CLASS lcl_debugger_script IMPLEMENTATION.
      METHOD prologue.
    *** generate abap_source (source handler for ABAP)
        super->prologue( ).
      ENDMETHOD.                    "prolog
    
      METHOD init.
        queryservicename = querydefaultname.
      ENDMETHOD.                    "init
    
      METHOD script.
    
        TYPES:
            BEGIN OF ty_selparam,
              attrname TYPE string,
              sign     TYPE string,
              option   TYPE string,
              low      TYPE string,
              high     TYPE string,
            END OF ty_selparam.
    
        DATA lt_col_alv                   TYPE tpda_script_service_source_tab.
        DATA ls_col_alv                   like LINE OF lt_col_alv.
        DATA ls_selparam                  TYPE ty_selparam.
        DATA lr_query_service             TYPE tpda_quick_vars.
        DATA lv_query_service_object_name TYPE tpda_var_name.
        DATA lv_selparamcol_object_name   TYPE tpda_var_name.
        DATA lv_bo_object_name            TYPE tpda_var_name.
        DATA lv_number_of_selparam        TYPE i.
        DATA lt_attr                      TYPE tpda_script_object_attribut_it.
        DATA ls_attr                      TYPE tpda_script_object_attributes.
        DATA lt_selparam                  TYPE STANDARD TABLE OF ty_selparam.
        DATA lr_attr                      TYPE REF TO cl_tpda_script_data_descr.
        DATA lr_entity_list               TYPE REF TO cl_tpda_script_tabledescr.
        DATA lr_obj_descr                 TYPE REF TO cl_tpda_script_objectdescr.
    
        REFRESH lt_selparam.
    
        TRY.
            lv_query_service_object_name = cl_tpda_script_data_descr=>get_variable_info( queryservicename )-varvalue.
            lv_selparamcol_object_name = cl_tpda_script_data_descr=>get_variable_info( lv_query_service_object_name && '-SELECTION_PARAM_COL' )-varvalue.
            lr_entity_list ?= cl_tpda_script_data_descr=>factory( lv_selparamcol_object_name && '-ENTITY_LIST' ).
            lv_number_of_selparam = lr_entity_list->linecnt( ).
            DO lv_number_of_selparam TIMES.
              lv_bo_object_name = cl_tpda_script_data_descr=>get_variable_info( lv_selparamcol_object_name && '-ENTITY_LIST[' && sy-index && ']-BO')-varvalue.
              ls_selparam-attrname = cl_tpda_script_data_descr=>get_simple_value( lv_bo_object_name && '-PARAMETER_DATA->ATTR_NAME' ).
              ls_selparam-option = cl_tpda_script_data_descr=>get_simple_value( lv_bo_object_name && '-PARAMETER_DATA->SIGN' ).
              ls_selparam-sign = cl_tpda_script_data_descr=>get_simple_value( lv_bo_object_name && '-PARAMETER_DATA->OPTION' ).
              ls_selparam-low = cl_tpda_script_data_descr=>get_simple_value( lv_bo_object_name && '-PARAMETER_DATA->LOW' ).
              ls_selparam-high = cl_tpda_script_data_descr=>get_simple_value( lv_bo_object_name && '-PARAMETER_DATA->HIGH' ).
              APPEND ls_selparam TO lt_selparam.
            ENDDO.
            REFRESH lt_col_alv.
            ls_col_alv-fieldname = ls_col_alv-content = 'ATTRNAME'.
            APPEND ls_col_alv TO lt_col_alv.
            ls_col_alv-fieldname = ls_col_alv-content = 'SIGN'.
            APPEND ls_col_alv TO lt_col_alv.
            ls_col_alv-fieldname = ls_col_alv-content = 'OPTION'.
            APPEND ls_col_alv TO lt_col_alv.
            ls_col_alv-fieldname = ls_col_alv-content = 'LOW'.
            APPEND ls_col_alv TO lt_col_alv.
            ls_col_alv-fieldname = ls_col_alv-content = 'HIGH'.
            APPEND ls_col_alv TO lt_col_alv.
            CALL METHOD cl_tpda_script_data_display=>data_display
              EXPORTING
                p_list_header = 'Query Selection Parameters'
                p_column_it   = lt_col_alv
                p_popup       = 'X'
              CHANGING
                p_data_it     = lt_selparam.
    
          CATCH cx_tpda_varname
                cx_tpda_data_descr_invalidated
                cx_sy_move_cast_error
                cx_tpda_table_wrong_key
                cx_tpda_script_no_simple_type
                cx_tpda_table_wrong_table_type.
            me->raise_error( ).
        ENDTRY.
      ENDMETHOD.                    "script
    
      METHOD end.
    *** insert your code which shall be executed at the end of the scripting (before trace is saved)
    *** here
    
      ENDMETHOD.                    "end
    
      METHOD if_tpda_script_w_input~get_parameters.
    
        DATA l_input   TYPE tpda_transfer_struc.
    
        IF p_parameters_it IS INITIAL.
          l_input-id      = 'Query Service Variable Name'.
          l_input-value   = querydefaultname.
          APPEND l_input TO p_parameters_it.
        ENDIF.
    
      ENDMETHOD.                    "if_tpda_script_w_input~get_parameters
    
      METHOD if_tpda_script_w_input~set_parameter_values.
    
        DATA l_input   TYPE tpda_transfer_struc.
    
        READ TABLE p_parameter_values_it INTO l_input INDEX 1.
        queryservicename = l_input-value.
    
      ENDMETHOD.                    "if_tpda_script_w_input~set_parameter_values
    
    ENDCLASS.                    "lcl_debugger_script IMPLEMENTATION
    

    Specify the name of your query service variable in line 21. In my example, it is QS.
    Start script by clicking button “Start Script”:

    Now you see all four selection parameter value displayed in ALV.

    要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  • 相关阅读:
    配置Apache虚拟主机
    Apache: You don't have permission to access / on this server
    wordpress之备份与恢复数据
    ruby之各种概念
    Oracle之比较NVARCHAR2字符串
    Centos安装ruby--jekyll
    Linux之IO Redirection
    SecureCRT导入已有会话
    jdk1.8新特性应用之Iterable
    jdk1.8新特性应用之Collection
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/13581987.html
Copyright © 2020-2023  润新知