Friday, 25 August 2017

Some small ABAP tools I write to improve daily work efficiency or just for fun

I write some small ABAP tools for my daily work. Some of them might not be so useful at first glance – I just write them for fun. Some of them could be used to improve work efficiency.

1. Tcode Usage Statistics Tool


This 56 lines of report can print the tcode usage for a given user per month.
REPORT ZTCODE.

PARAMETERS: month TYPE dats DEFAULT sy-datum OBLIGATORY,
           user type usr02-bname OBLIGATORY DEFAULT sy-uname.

TYPES: BEGIN OF zusertcode,
         operation type char30,
         type type char10,
         count  TYPE swncshcnt,
       END OF zusertcode.

TYPES: tt_zusertcode TYPE STANDARD TABLE OF zusertcode WITH KEY operation type.

DATA: lt_usertcode  TYPE swnc_t_aggusertcode,
      ls_result    TYPE zusertcode,
      lt_result     TYPE tt_zusertcode.

CONSTANTS: cv_tcode TYPE char30 VALUE 'Tcode',
           cv_report TYPE char30 VALUE 'Report',
           cv_count TYPE char5 value 'Count'.

START-OF-SELECTION.

  CALL FUNCTION 'SWNC_COLLECTOR_GET_AGGREGATES'
    EXPORTING
      component     = 'TOTAL'
      periodtype    = 'M'
      periodstrt    = month
    TABLES
      usertcode     = lt_usertcode
    EXCEPTIONS
      no_data_found = 1
      OTHERS        = 2.

  DELETE lt_usertcode WHERE tasktype <> '01'.

  LOOP AT lt_usertcode ASSIGNING FIELD-SYMBOL(<user>) WHERE account = user.
     CLEAR: ls_result.
     ls_result-operation = <user>-entry_id.
     ls_result-type = <user>-entry_id+72.
     ls_result-count = <user>-count.
     COLLECT ls_result INTO lt_result.
  ENDLOOP.

  SORT lt_result BY count DESCENDING.

  WRITE:  10 cv_tcode, 20 cv_report, 60 cv_count COLOR COL_NEGATIVE.
  LOOP AT lt_result ASSIGNING FIELD-SYMBOL(<result>).
      IF <result>-type = 'T'.
        WRITE: / <result>-operation COLOR COL_TOTAL UNDER cv_tcode,
                 <result>-count COLOR COL_POSITIVE UNDER cv_count.
      ELSE.
        WRITE: / <result>-operation COLOR COL_GROUP UNDER cv_report,
                 <result>-count COLOR COL_POSITIVE UNDER cv_count.
      ENDIF.
  ENDLOOP.

For example if you would like to check the tcode usage for user WANGJER during March 2017, you have to just choose any date in March and execute:

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

It shows I have used SAT for 692 times. Looks like I have some performance trouble on March?

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

The similar function could be found in tcode ST03N.

2. ABAP Class Version Browse Tool


ABAP workbench has built-in version management tool.
The version comparison could only be performed against two version at a time.

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

Sometimes I have the requirement to do some pure text analysis on the source code version, for example I need to figure out on which version a variable is introduced. So I write this small tool, which can downloads all the versions of source code into a text file, then I can simply use text analysis tool to fulfill my query.

Execute this 38 lines of tool and it will print out the source code of all versions of specified method.
REPORT tool_abap_class_version_tool.

PARAMETERS: cname TYPE seocompo-clsname OBLIGATORY DEFAULT 'CL_CRM_OPPORTUNITY_MPC_EXT',
            mname TYPE seocompo-cmpname OBLIGATORY DEFAULT 'DEFINE_OPPORTUNITY'.

DATA: lv_name      TYPE vrsd-objname,
      lt_version   TYPE STANDARD TABLE OF vrsd,
      lt_table     TYPE STANDARD TABLE OF abaptxt255,
      lt_trdir     TYPE STANDARD TABLE OF trdir,
      lt_vsmodisrc TYPE STANDARD TABLE OF smodisrc,
      lt_vsmodilog TYPE STANDARD TABLE OF smodilog.

lv_name = cname && '%' && mname.

SELECT objname versno datum zeit FROM vrsd INTO CORRESPONDING FIELDS OF TABLE lt_version
   WHERE objname LIKE lv_name ORDER BY versno DESCENDING. "#EC CI_NOFIRST

LOOP AT lt_version ASSIGNING FIELD-SYMBOL(<version>).
  CALL FUNCTION 'SVRS_GET_REPS_FROM_OBJECT'
    EXPORTING
      object_name = <version>-objname
      object_type = 'METH'
      versno      = <version>-versno
    TABLES
      repos_tab   = lt_table
      trdir_tab   = lt_trdir
      vsmodisrc   = lt_vsmodisrc
      vsmodilog   = lt_vsmodilog.

  ASSERT sy-subrc = 0.

  WRITE: / 'version number: ' COLOR COL_GROUP, <version>-versno COLOR COL_NEGATIVE,
  'Date: ' COLOR COL_KEY, <version>-datum COLOR COL_HEADING, ' time: ' COLOR COL_POSITIVE, <version>-zeit COLOR COL_NORMAL.

  LOOP AT lt_table ASSIGNING FIELD-SYMBOL(<line>).
    WRITE: / <line>-line.
  ENDLOOP.
ENDLOOP.

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

The left task is to download it locally via Workbench:

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

Now I can review all the 77 versions of this method in a single text file.

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

Sublime Text is my favorite local text editor, as you can see from above screenshot there is also ABAP plugin for it to display ABAP source code with correct syntax highlight.
In Sublime Text it is possible to open the same file in separate two views so it is very convenience for me now to compare any two versions within the same editor window.

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

3. Development History Trace Tool


For example if I would like to recall what I have changed between 2017-03-01 and 2017-03-25, I can simply execute this 67 lines of reports:
PARAMETERS: name TYPE usr02-bname OBLIGATORY,
            fro  TYPE sy-datum OBLIGATORY DEFAULT sy-datum,
            to_  TYPE sy-datum OBLIGATORY DEFAULT sy-datum.

CONSTANTS: c_name TYPE trdir-name VALUE 'Name',
           c_date TYPE trdir-udat VALUE 'Date'.

AT SELECTION-SCREEN.
  IF fro > to_.
    WRITE: / 'Invalid date period.' COLOR COL_NEGATIVE.
    RETURN.
  ENDIF.

START-OF-SELECTION.
  DATA: lt_record TYPE STANDARD TABLE OF trdir.

  SELECT name udat INTO CORRESPONDING FIELDS OF TABLE lt_record FROM trdir
     WHERE unam = name AND udat BETWEEN fro AND to_.

  IF sy-subrc <> 0.
    WRITE: / 'No record found at given date period.' COLOR COL_NEGATIVE.
    RETURN.
  ENDIF.

  SORT lt_record BY udat DESCENDING.

  WRITE:  10 c_name, 80 c_date.

  LOOP AT lt_record INTO DATA(ls_data).
    WRITE: / ls_data-name UNDER c_name COLOR COL_POSITIVE, ls_data-udat UNDER c_date COLOR COL_TOTAL.
    HIDE ls_data-name.
  ENDLOOP.

AT LINE-SELECTION.
  DATA: bdcdata_tab  TYPE TABLE OF bdcdata,
        opt          TYPE ctu_params,
        bdcdata_line TYPE bdcdata.

  bdcdata_line-program = 'SAPLWBABAP'.
  bdcdata_line-dynpro = '0100'.
  bdcdata_line-dynbegin = 'X'.
  APPEND bdcdata_line TO bdcdata_tab.

  CLEAR: bdcdata_line.

  bdcdata_line-fnam = 'BDC_CURSOR'.
  bdcdata_line-fval = 'RS38M-PROGRAMM'.
  APPEND bdcdata_line TO bdcdata_tab.


  CLEAR: bdcdata_line.
  bdcdata_line-fnam = 'BDC_OKCODE'.
  bdcdata_line-fval = '=SHOP'.
  APPEND bdcdata_line TO bdcdata_tab.

  CLEAR: bdcdata_line.
  bdcdata_line-fnam = 'RS38M-PROGRAMM'.
  bdcdata_line-fval = ls_data-name.
  APPEND bdcdata_line TO bdcdata_tab.

  opt-dismode = 'E'.
  opt-defsize = 'X'.

  CALL TRANSACTION 'SE38' USING bdcdata_tab  OPTIONS FROM opt.
  CLEAR: bdcdata_tab.

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

Double click on each entry, and ABAP editor will be opened to allow me to review the source code of chosen entry.

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

This tool is especially useful for me as when I grow old, sometimes I didn’t remember what I have changed several days ago, and if I only make changes on Z objects then there will be no log in transport requests. Use this tool and I can immediately realize what I have changed for a given time interval.

4. A compare tool: Download and analyze the runtime performance result from SAT


5. Execute some transaction code without logging on to backend system


Suppose I am too lazy to log on a backend system to execute transaction code SM04. Then I can trigger it in excel and review result there as well.
Then I just create an excel with Macro enabled ( .xlsm ), put a button and assign the following VB code to it:

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

Sub CommandButton1_Click()
    Set R3 = CreateObject("SAP.Functions")
    Set myConnction = R3.Connection
    myConnction.ApplicationServer = "replace with your own host name here!!!"
    myConnction.SystemNumber = 54
    myConnction.Client = "001"
    myConnction.user = "replace with your own user here!!!"
    myConnction.Password = "replace with your own password here!!!"
    
    If myConnction.Logon(0, True) <> True Then
      MsgBox "Logon failed"
      Exit Sub
    End If
    
    Dim callFunctionModule As Object
        
    Set callFunctionModule = R3.Add("TH_USER_LIST")
    callFunctionModule.Call

    If callFunctionModule.Exception <> "" Then
        MsgBox callFunctionModule.Exception
    End If

    If callFunctionModule.Call = True Then
        Dim result As Object
        Set result = callFunctionModule.tables("USRLIST")
        Dim aSheet As Worksheet
        Dim sheetCol As New Collection
        sheetCol.Add ActiveWorkbook.Sheets(1)
        For Each aSheet In sheetCol
            Dim i As Integer
            i = 1
            For Each user In result.Rows
                
                Client = user(2)
                UserName = user(3)
                Terminal = user(5)
                IP = user(16)
                aSheet.Cells(i, 1) = Client
                aSheet.Cells(i, 2) = UserName
                aSheet.Cells(i, 3) = Terminal
                aSheet.Cells(i, 4) = IP
                
                i = i + 1
            Next
        Next
    Else
        MsgBox " Call Failed! error: "
    End If
'log off
    myConnction.logoff
End Sub

With this script I am calling function module TH_USER_LIST from excel, parse the result and display them in excel.

Now I click the button, I can observe in ABAP system that the function module TH_USER_LIST is called:

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

And result is displayed in excel:

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

6. XML difference compare tool


No need to download additional software to compare the different XML, as there is a standard class cl_proxy_ui_utils which can do this task for you.

See the following sample code:

DATA: lv_original TYPE xstring,
      lv_changed  TYPE xstring.

lv_original = zcl_jerry_tool=>get_file_binary_by_path( 'C:\temp\1.xml' ).
lv_changed  = zcl_jerry_tool=>get_file_binary_by_path( 'C:\temp\2.xml' ).

CALL METHOD cl_proxy_ui_utils=>show_xml_diff
  EXPORTING
    doc1 = lv_original
    doc2 = lv_changed.

Execute the report and there will be a popup where the difference is displayed. The implementation of ZCL_JERRY_TOOL=>GET_FILE_BINARY_BY_PATH could be found here.

7. Mischief: send a message to another online user


The function module TH_POPUP can generate a popup window for a given online dialog user.
See example below:

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

Once executed, I will see a popup in my SAPGUI:

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

I have then written a report using this FM.

Once this report is executed, you can see a list of online users in current application server, which has the same content as in tcode SM04.

ABAP Development, ABAP Connectivity, ABAP Testing and Analysis, ABAP

Double click on a given user, and he / she will see a popup dialog in his SAPGUI. You can use this mischief report to greet your colleague, for example send a message like “hey guys, you’d better stand up and have a cup of coffee” after you see he / she has already debugged one issue for half an hour? 

Source code of report could be found here.

1 comment: