Tuesday, January 5, 2016

How to programmatically check syntax of ABAP report

Dynamic generation of ABAP reports is very useful feature. While generating ABAP reports on the fly from other ABAP report it is very useful to get syntax of the generated ABAP checked before it will run. Having the syntax check in place can prevent to undesirable dumps.

In this blog post I’m not going to deal with many ABAP tools that are available in the system (like tcode SAMT – ABAP program set processing, tcode SCI - Code Inspector, tcode UCCHECK – Check for syntax errors in Unicode environment, etc.). These tools are very useful when performing of mass check for many programs is needed, like during upgrade projects.  Here I’m just looking for a ways how to perform syntax check of one ABAP report from other one. In order to explore this I created two test programs that leverage different approaches of ABAP code syntax checks.


1. Syntax check via statement "SYNTAX-CHECK FOR":


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
REPORT ZMM_ABAP_REP_SYNTAX_CHECK01.
* ver 1 via statement "SYNTAX-CHECK FOR"
PARAMETERS: p_prg TYPE sy-repid.

DATA: lt_source TYPE STANDARD TABLE OF string,
      ls_msg   TYPE string,
      ls_line  TYPE i,
      ls_wrd   TYPE string,
      ls_dir   TYPE trdir.

READ REPORT p_prg INTO lt_source.

SELECT SINGLE * FROM trdir INTO ls_dir WHERE name = sy-repid.

ls_dir-uccheck = ' '.
SYNTAX-CHECK FOR lt_source MESSAGE ls_msg LINE ls_line WORD ls_wrd
             DIRECTORY ENTRY ls_dir.
IF sy-subrc = 4.
  MESSAGE ls_msg TYPE 'I'.
ENDIF.

ls_dir-uccheck = 'X'.
SYNTAX-CHECK FOR lt_source MESSAGE ls_msg LINE ls_line WORD ls_wrd
             DIRECTORY ENTRY ls_dir.
IF sy-subrc = 4.
  MESSAGE ls_msg TYPE 'I'.
ENDIF.


Source code available: here

Example of output:


2. Syntax check via FM EXTENDED_PROGRAM_CHECK interactive version:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
REPORT ZMM_ABAP_REP_SYNTAX_CHECK02.
* ver 2 interactive version via FM EXTENDED_PROGRAM_CHECK, 
* output comes in SAP screen nicely formatted 
PARAMETERS: p_prg TYPE sy-repid.
DATA: lt_result     TYPE slin_result,
      lt_resultstat TYPE slin_result_stat.

CALL FUNCTION 'EXTENDED_PROGRAM_CHECK'
  EXPORTING
    program     = p_prg
  IMPORTING
    result      = lt_result
    result_stat = lt_resultstat.

CALL FUNCTION 'EXTENDED_PROGRAM_CHECK_SHOW'
  EXPORTING
    result      = lt_result
    result_stat = lt_resultstat
    repid       = p_prg.

Source code available: here

Example of output:

As an example of second method you can take a look into SAP BW program RSAN_WB_VERI on how this is used in more sophisticated way.

Related posts:
How to programmatically activate ABAP report

4 comments:

Anonymous said...

FYI: You can do a mass syntax check of reports in transaction SAMT

Martin Maruskin said...

Thanks for your comment! That is correct - exactly same what is written in my blog.

Max said...

As always, thanks for the information.
Your posts are great !

Martin Maruskin said...

Thanks Max for your comment, really appreciated!