Reading content data from batch job variants

In quite some cases the basis team is asked: in which batch job variant is this company code XXXX used? Or we need to add another sales organization to all the batch jobs, can you provide us a list with jobs using sales organization YYYY?

Some hints are given in this blog.

But no real solution.

Continue reading for a custom code solution!

Custom program to scan batch job variant content

If you load the program in the next section, create texts so that the start screen looks like this:

First test program on small batch of programs with know variants.

Example search all programs starting with ZA for the variant text US:

Result is list of program and variants with the key word:

Custom program coding

*&---------------------------------------------------------------------*
*& Report ZVARSEARCH
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZVARSEARCH.

* _____________________________________________________________________
*|                              T A B L E S                            |
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
TABLES: trdir, tadir.

* _____________________________________________________________________
*|                              T Y P E S                              |
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
TYPES: BEGIN OF ty_source,
         line(255) TYPE c,
       END OF ty_source.

TYPES: BEGIN OF ty_objname,
         obj_name TYPE sobj_name,
       END OF ty_objname.

TYPES: BEGIN OF ty_variant,
         variant TYPE variant,
       END OF ty_variant.

* _____________________________________________________________________
*|              I T A B S  & W O R K A R E A S                         |
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
DATA: gt_selop       TYPE STANDARD TABLE OF    vanz,
      gt_params      TYPE STANDARD TABLE OF    vanz,
      gt_params_nonv TYPE STANDARD TABLE OF    vanz,
      gt_selop_nonv  TYPE STANDARD TABLE OF    vanz,
      gs_vanz        TYPE                      vanz,
      lv_text        TYPE                      string,
      gv_report      TYPE                      vari_reprt,
      mv_report      TYPE                      vari_reprt,
      gt_variant     TYPE STANDARD TABLE OF    ty_variant,
      gs_variant     TYPE                      ty_variant,
      gv_program     TYPE                      syrepid,
      gv_subrc       TYPE                      sysubrc,
      gt_reports     TYPE STANDARD TABLE OF    trdir,
      gs_reports     TYPE                      trdir,
      gt_valuetab    TYPE STANDARD TABLE OF    rsparams,
      gt_objname     TYPE STANDARD TABLE OF    ty_objname.

FIELD-SYMBOLS:
      <fs_vanz>      TYPE vanz.
* _____________________________________________________________________
*|                 S E L E C T I O N - S C R E E N                     |
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
SELECTION-SCREEN BEGIN OF BLOCK aa WITH FRAME.
SELECTION-SCREEN BEGIN OF BLOCK bb WITH FRAME TITLE text-t01.
PARAMETERS:     ra_srch   RADIOBUTTON GROUP a1
                             USER-COMMAND tab1 DEFAULT 'X',
                ra_srch2  RADIOBUTTON GROUP a1,
                ra_wday   RADIOBUTTON GROUP a1,
                ra_synt   RADIOBUTTON GROUP a1,
                ch_all   AS CHECKBOX  MODIF ID a2.
SELECTION-SCREEN END OF BLOCK bb.
SELECT-OPTIONS: so_prog   FOR trdir-name.
PARAMETERS:     p_string  TYPE s_text       MODIF ID a1.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF BLOCK cc WITH FRAME.
PARAMETERS:     pa_prog   TYPE program,
                pa_local  AS CHECKBOX.
SELECTION-SCREEN END OF BLOCK cc.
SELECTION-SCREEN END OF BLOCK aa.

* _____________________________________________________________________
*|           A T  S E L E C T I O N - S C R E E N  O U T P U T         |
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.
    CASE screen-group1.
      WHEN 'A1'.

        IF ra_srch  IS INITIAL
        AND ra_srch2 IS INITIAL.
          CLEAR p_string.
          screen-input = '0'.
        ELSE.
          screen-input = '1'.
        ENDIF.
      WHEN 'A2'.
        IF ra_synt = 'X'.
          screen-input = '1'.

        ELSE.
          CLEAR ch_all.
          screen-input = '0'.
        ENDIF.
    ENDCASE.
    MODIFY SCREEN.
  ENDLOOP.

* _____________________________________________________________________
*|                 S T A R T - O F - S E L E C T I O N                 |
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
START-OF-SELECTION.
  IF ( NOT ra_srch  IS INITIAL OR NOT ra_srch2 IS INITIAL )
  AND p_string IS INITIAL.
    MESSAGE e001(00) WITH 'No string entered'.
  ENDIF.

  PERFORM select_programs.
  PERFORM select_variants.

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  SELECT_PROGRAMS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM select_programs.

  DATA: lv_tabix   TYPE   sytabix,
        lv_lines   TYPE   i.

*-Only executable programs are taken into account
  IF ch_all IS INITIAL.
    SELECT * FROM trdir
       INTO TABLE gt_reports
         WHERE name IN so_prog
                     AND subc = '1'.

*-For syntax checking you can also include other program types
  ELSE.
    SELECT * FROM trdir
      INTO TABLE gt_reports
        WHERE name IN so_prog.
  ENDIF.

  SORT gt_reports BY name.
  DESCRIBE TABLE gt_reports LINES lv_lines.

*-check if the user wants to start from a certain program
*-(so skip all preceding programs)
  IF NOT pa_prog IS INITIAL.

    READ TABLE gt_reports WITH KEY name = pa_prog
     TRANSPORTING NO FIELDS.
    IF sy-subrc = 0.
      lv_tabix = sy-tabix + 1.
      IF lv_tabix < lv_lines.
*-      Delete all preceding reports including the start from report
        DELETE gt_reports FROM 1 TO lv_tabix.
      ENDIF.
    ENDIF.

  ENDIF.

  IF gt_reports[] IS INITIAL.
    MESSAGE s001(00) WITH 'No reports selected'.
    WRITE:/ 'No reports selected'.
  ENDIF.

ENDFORM.                    " SELECT_PROGRAMS
*&---------------------------------------------------------------------*
*&      Form  SELECT_VARIANTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM select_variants .

  DATA: lt_results     TYPE STANDARD TABLE OF rsvobs,
        lv_found       TYPE                   flag.

  REFRESH gt_objname.

  CHECK NOT gt_reports[] IS INITIAL.

*-retrieve already all details for quicker reading
  IF pa_local = 'X'.                         "Exclude local programs
    SELECT obj_name INTO TABLE gt_objname
      FROM tadir
      FOR ALL ENTRIES IN gt_reports
      WHERE pgmid = 'R3TR'
        AND object = 'PROG'
        AND obj_name = gt_reports-name.
  ELSE.                                      "Include local programs
    SELECT obj_name INTO TABLE gt_objname
      FROM tadir
      FOR ALL ENTRIES IN gt_reports
      WHERE pgmid = 'R3TR'
        AND object = 'PROG'
        AND obj_name = gt_reports-name
        AND devclass <> '$TMP'.
  ENDIF.

  LOOP AT gt_reports INTO gs_reports.

    READ TABLE gt_objname
      WITH KEY obj_name = gs_reports-name
        TRANSPORTING NO FIELDS.
    CHECK sy-subrc = 0.

    gv_program = gv_report = gs_reports-name.

*=====================================================================*
*-  PERFORM A SYNTAX CHECK FOR PROGRAM
*=====================================================================*
*-  Get the source code of the report for syntax checking
    PERFORM program_syntax_check USING    gv_program
                                 CHANGING gv_subrc.

*-  Skip programs with syntax errors for further variant checking
    IF gv_subrc <> 0.
      IF ra_synt = 'X'.
        MESSAGE s000(3w) WITH 'SYNTAX ERROR==>' gv_report.
        WRITE:/      gv_report,
                  50 'CONTAINS SYNTAX ERROR'.
      ELSE.
        CONTINUE.
      ENDIF.
    ENDIF.

*---------------------------------------------------------------------*
*-  Only process variants for select-options search and working days
*---------------------------------------------------------------------*
    CHECK ra_srch = 'X' OR ra_srch2 = 'X' OR ra_wday = 'X'.

*-  Select variants for a program
*-  (exclude the very old ones and also the standard SAP ones)
    SELECT variant INTO TABLE gt_variant
      FROM varid
          WHERE report = gv_report
            AND ( edat > 20100101  OR  aedat > 20100101 )
            AND ( ename <> 'SAP'   AND aename <> 'SAP'  ).

    CHECK NOT gt_variant[] IS INITIAL.
    REFRESH lt_results.

*-  Process all variants
    LOOP AT gt_variant INTO gs_variant.
      CLEAR lv_found.

*-    Give a message to indicate which program/variant is being checked
*-    (comes in handy in case the program cancels during the check as
*-    you can then restart this program starting from the object that
*-    dumped)
      MESSAGE s000(3w) WITH 'Checking' gv_report '-' gs_variant-variant.

      CALL FUNCTION 'RS_VARIANT_CONTENTS'
        EXPORTING
          report                      = gv_report
          variant                     = gs_variant-variant
          no_import                   = ' '
          execute_direct              = ''
        TABLES
          l_params                    = gt_params
          l_params_nonv               = gt_params_nonv
          l_selop                     = gt_selop
          l_selop_nonv                = gt_selop_nonv
          valutab                     = gt_valuetab
       EXCEPTIONS
         variant_non_existent        = 1
         variant_obsolete            = 2
         OTHERS                      = 3.

      CHECK sy-subrc = 0.

*=====================================================================*
*-    SEARCH VARIANTS FOR A CERTAIN FIELD VALUE
      IF ra_srch = 'X'.
*=====================================================================*
        FIND FIRST OCCURRENCE OF p_string
        IN TABLE gt_valuetab
        IN CHARACTER MODE.
        IF sy-subrc = 0.
          CONCATENATE 'String' p_string 'found'
             INTO lv_text SEPARATED BY space.
          MESSAGE s000(3w) WITH 'FOUND:' gv_report
                                gs_variant-variant lv_text.

          WRITE:/   'FOUND:',
                  7 gv_report,
                 50 gs_variant-variant,
                 80 lv_text.
        ENDIF.

*=====================================================================*
*-    SEARCH VARIANTS FOR A CERTAIN FIELD NAME OR FIELD DESCRIPTION
      ELSEIF ra_srch2 = 'X'.
*=====================================================================*
*-      Check select-options field names and descriptions for a
*-      certain string
        LOOP AT gt_selop ASSIGNING <fs_vanz>.
          FIND FIRST OCCURRENCE OF  p_string
          IN <fs_vanz>-name IN CHARACTER MODE.
          IF sy-subrc = 0.
            lv_found ='X'.
            EXIT.
          ENDIF.
        ENDLOOP.

        IF lv_found IS INITIAL.
          LOOP AT gt_selop_nonv ASSIGNING <fs_vanz>.
            FIND FIRST OCCURRENCE OF  p_string
            IN <fs_vanz>-name IN CHARACTER MODE.
            IF sy-subrc = 0.
              lv_found ='X'.
              EXIT.
            ENDIF.
          ENDLOOP.
        ENDIF.

        IF lv_found IS INITIAL.
          LOOP AT gt_params  ASSIGNING <fs_vanz>.
            FIND FIRST OCCURRENCE OF p_string
            IN <fs_vanz>-name IN CHARACTER MODE.

            IF sy-subrc = 0.
              lv_found ='X'.
              EXIT.
            ENDIF.
          ENDLOOP.
        ENDIF.

        IF lv_found IS INITIAL.
          LOOP AT gt_params_nonv  ASSIGNING <fs_vanz>.
            FIND FIRST OCCURRENCE OF p_string
            IN <fs_vanz>-name IN CHARACTER MODE.

            IF sy-subrc = 0.
              lv_found ='X'.
              EXIT.
            ENDIF.
          ENDLOOP.
        ENDIF.

        IF lv_found = 'X'.
          CONCATENATE 'String' p_string 'found'
                      INTO lv_text SEPARATED BY space.
          MESSAGE s000(3w) WITH 'FOUND:' gv_report
                                gs_variant-variant lv_text.
          WRITE:/   'FOUND:',
                  7 gv_report,
                 50 gs_variant-variant,
                 80 lv_text.
        ENDIF.

*=====================================================================*
*-    SEARCH VARIANTS FOR VARIABLES THAT REQUIRE CALENDAR ENTRY
      ELSEIF ra_wday = 'X'.
*=====================================================================*
        LOOP AT gt_selop INTO gs_vanz
             WHERE vname <> ''.
          IF gs_vanz-vname CS 'XWD' OR gs_vanz-vname CS 'WDAY'.
            lv_found ='X'.
            EXIT.
          ENDIF.
        ENDLOOP.
        IF lv_found IS INITIAL.
          LOOP AT gt_selop_nonv INTO gs_vanz
               WHERE vname <> ''.
            IF gs_vanz-vname CS 'XWD' OR gs_vanz-vname CS 'WDAY'.
              lv_found ='X'.
              EXIT.
            ENDIF.
          ENDLOOP.
        ENDIF.
        IF lv_found IS INITIAL.
          LOOP AT gt_params INTO gs_vanz
               WHERE vname <> ''.
            IF gs_vanz-vname CS 'XWD' OR gs_vanz-vname CS 'WDAY'.
              lv_found ='X'.
              EXIT.
            ENDIF.
          ENDLOOP.
        ENDIF.
        IF lv_found IS INITIAL.
          LOOP AT gt_params_nonv INTO gs_vanz
               WHERE vname <> ''.
            IF gs_vanz-vname CS 'XWD' OR gs_vanz-vname CS 'WDAY'.
              lv_found ='X'.
              EXIT.
            ENDIF.
          ENDLOOP.
        ENDIF.

        IF lv_found = 'X'.
          MESSAGE s000(3w) WITH 'FOUND:' gv_report gs_variant-variant
                                'Calendar logic present'.
          WRITE:/   'FOUND:',
                  8 gv_report,
                 50 gs_variant-variant,
                 80 'Calendar logic present'.

        ENDIF.

      ENDIF.

    ENDLOOP.

  ENDLOOP.

ENDFORM.                    " SELECT_VARIANTS
*&---------------------------------------------------------------------*
*&      Form  PROGRAM_SYNTAX_CHECK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_LV_SUBRC  text
*----------------------------------------------------------------------*
FORM program_syntax_check   USING    p_program
                            CHANGING p_subrc.

  DATA: lt_source    TYPE STANDARD TABLE OF    ty_source.

  CLEAR p_subrc.
  REFRESH lt_source.

  READ REPORT gv_program INTO lt_source.

  CALL FUNCTION 'RS_SYNTAX_CHECK'
    EXPORTING
      i_program        = p_program
    IMPORTING
      o_error_subrc    = p_subrc
    TABLES
      i_source         = lt_source.

ENDFORM.                    " PROGRAM_SYNTAX_CHECK

ABAP escape functions

The escape function is used in IT systems to indicate a real value of a character in stead of the control function of the character. Example is the < character which means smaller than, but in HTML is also a control character. For more background read wikipedia.

In ABAP code you sometimes need to set a URL with these characters, or build a HTML text. Then you really need to use these rules.

Documentation of the Esacpe function in ABAP can be found in this URL.

Example is the following code:

cl_demo_output=>display(
  escape( val    = 'IF a < b.'
          format = cl_abap_format=>e_xml_text ) ).

The result will be IF a &lt; b. < is in xml replaced by escape character &lt;.

Demo program

A full list can be found in ABAP program DEMO_ESCAPE. Start the program and select the type you want to check (do select the Show Differences tick box):

Output is now show for the diverse characters:

Find batch jobs with deleted or locked step users

If a batch job step user is locked or deleted, that step will fail. To find batch jobs with deleted or locked step users, start program BTCAUX09:

This will give a full list of jobs that you might want to start analyzing for deletion:

Why clean up? You will most likely find a lot of jobs in Scheduled status that never ran and are planned very long time ago. All batch job related transactions will have to plough through this irrelevant data.

OSS notes

Notes:

ABAP code to call URL

In ABAP code you might need to reach out to a browser.

The code to call a URL is pretty simple:

DATA ld_url(1000).

ld_url = 'https://www.saptechnicalguru.com'.

CALL FUNCTION 'CALL_BROWSER'
  EXPORTING
    url = ld_url.

Simply construct your URL and use function “CALL_BROWSER”.

For a webdynpro to run in browser use this code:

CALL FUNCTION 'WDY_EXECUTE_IN_BROWSER'
   EXPORTING
     application         = 'ESH_SEARCH_RESULTS_UI'
EXCEPTIONS
     invalid_application = 1
     browser_not_started = 2
     action_cancelled    = 3
OTHERS              = 4.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 RAISING execution_error.
ENDIF.

Application ESH_SEARCH_RESULTS_UI is an example here of a web dynpro.