[ABAP] Customizing-Objekte zu einem Datenfeld lesen

TYPES : BEGIN OF ty_s_customizing_tables,
          tabname TYPE dd02l-tabname,
          ddtext  TYPE dd02t-ddtext,
        END OF ty_s_customizing_tables.

TYPES: ty_it_customizing_tables TYPE STANDARD TABLE OF ty_s_customizing_tables WITH DEFAULT KEY.

DATA: it_customizing_tables TYPE ty_it_customizing_tables.

* Klasse für Eventhandling definieren
CLASS lcl_events DEFINITION.
  PUBLIC SECTION.
* Doppelklick
    CLASS-METHODS: on_double_click FOR EVENT double_click OF cl_salv_events_table
      IMPORTING
          row
          column
          sender.
ENDCLASS.

CLASS lcl_events IMPLEMENTATION.
  METHOD on_double_click.
* Tabelle / Pflegeview
    DATA(lv_tab) = CONV dd02l-tabname( it_customizing_tables[ row ]-tabname ).

* Customizingobjekte zu Tabelle anzeigen, ins SAP-IMG abspringen
    CALL FUNCTION 'OBJECT_MAINTENANCE_CALL'
      EXPORTING
        tabname                        = lv_tab
      EXCEPTIONS
        interface_not_correct          = 1
        transaction_not_maintained     = 2
        transaction_not_found          = 3
        table_not_activ                = 4
        table_not_found                = 5
        subobject_not_found_in_project = 6
        subobject_not_found_in_guide   = 7
        object_not_found_in_project    = 8
        object_not_found_in_guide      = 9
        table_has_no_object_in_project = 10
        table_has_no_object_in_guide   = 11
        outline_not_found              = 12
        call_transaction_recurring     = 13
        system_failure                 = 14
        OTHERS                         = 15.

    DATA(lv_err) = ||.

    CASE sy-subrc.
      WHEN 1.
        lv_err = |INTERFACE_NOT_CORRECT|.
      WHEN 2.
        lv_err = |TRANSACTION_NOT_MAINTAINED|.
      WHEN 3.
        lv_err = |TRANSACTION_NOT_FOUND|.
      WHEN 4.
        lv_err = |TABLE_NOT_ACTIV|.
      WHEN 5.
        lv_err = |TABLE_NOT_FOUND|.
      WHEN 6.
        lv_err = |SUBOBJECT_NOT_FOUND_IN_PROJECT|.
      WHEN 7.
        lv_err = |SUBOBJECT_NOT_FOUND_IN_GUIDE|.
      WHEN 8.
        lv_err = |OBJECT_NOT_FOUND_IN_PROJECT|.
      WHEN 9.
        lv_err = |OBJECT_NOT_FOUND_IN_GUIDE|.
      WHEN 10.
        lv_err = |TABLE_HAS_NO_OBJECT_IN_PROJECT|.
      WHEN 11.
        lv_err = |TABLE_HAS_NO_OBJECT_IN_GUIDE|.
      WHEN 12.
        lv_err = |OUTLINE_NOT_FOUND|.
      WHEN 13.
        lv_err = |CALL_TRANSACTION_RECURRING|.
      WHEN 14.
        lv_err = |SYSTEM_FAILURE|.
      WHEN 15.
        lv_err = |OTHERS|.
    ENDCASE.

    MESSAGE |{ lv_err }| TYPE 'S' DISPLAY LIKE 'E'.

  ENDMETHOD.
ENDCLASS.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (10) l_field FOR FIELD p_field.
PARAMETER: p_field TYPE dd03vv-fieldname DEFAULT 'VSBED'.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.

  l_field = 'Feldname:'.

START-OF-SELECTION.

* Customizingtabellen zu einem Datenfeld lesen
  SELECT t~tabname,
         tx~ddtext
    FROM dd03vv AS t
    INNER JOIN dd02l AS c ON t~tabname = c~tabname
    INNER JOIN dd02t AS tx ON t~tabname = tx~tabname
    INTO TABLE @it_customizing_tables
    WHERE t~fieldname = @p_field     " Feldname
      AND t~tabclass = 'TRANSP'      " transparente Tabellen
      AND t~as4local = 'A'           " aktive Objekte
      AND c~contflag IN ('C', 'G')   " Typ: Customizing-Tabellen
      AND tx~ddlanguage = @sy-langu. " Anmeldesprache

  TRY.
* SALV-Table
      DATA: o_salv TYPE REF TO cl_salv_table.

      cl_salv_table=>factory( IMPORTING
                                r_salv_table   = o_salv
                              CHANGING
                                t_table        = it_customizing_tables ).

* Grundeinstellungen
      o_salv->get_functions( )->set_all( abap_true ).
      o_salv->get_columns( )->set_optimize( abap_true ).
      o_salv->get_display_settings( )->set_list_header( |Customizing-Tabellen zu Datenfeld { p_field } (Doppelklick -> IMG)| ).
      o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
      o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

* Spaltenüberschriften: technischer Name und Beschreibungstexte
      LOOP AT o_salv->get_columns( )->get( ) ASSIGNING FIELD-SYMBOL(<c>).
        DATA(o_col) = <c>-r_column.
        o_col->set_short_text( || ).
        o_col->set_medium_text( || ).
        o_col->set_long_text( |{ o_col->get_columnname( ) } [{ o_col->get_long_text( ) }]| ).
      ENDLOOP.

* Handler registrieren
      SET HANDLER lcl_events=>on_double_click FOR o_salv->get_event( ).

      o_salv->display( ).
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

Links

[ABAP] Datumsfeld auf Gültigkeit prüfen

Variante 1 (check_date)

TRY.
    cl_reca_date=>check_date( CONV d( '01.01.1999' ) ).

  CATCH cx_root INTO DATA(e1).
    WRITE: / e1->get_text( ).
ENDTRY.

TRY.
    cl_reca_date=>check_date( CONV d( '19990101' ) ).

  CATCH cx_root INTO DATA(e2).
    WRITE: / e2->get_text( ).
ENDTRY.

TRY.
    cl_reca_date=>check_date( CONV d( '1234' ) ).

  CATCH cx_root INTO DATA(e3).
    WRITE: / e3->get_text( ).
ENDTRY.

Variante 2 (is_date_ok)

IF abap_true = cl_reca_date=>is_date_ok( id_date = sy-datum ).
  WRITE: / 'Ok.'.
ELSE.
  WRITE: / 'Fehlerhaft.'.
ENDIF.

[ABAP] Zeit zwischen zwei Terminen

* Anzahl Tage
DATA(lv_days) = cl_reca_date=>get_days_between_two_dates( EXPORTING
                                                            id_datefrom = '20190101'
                                                            id_dateto   = '20190201' ).

WRITE: / lv_days.

* Anzahl Monate
DATA(lv_months) = cl_reca_date=>months_between_two_dates( EXPORTING
                                                            id_date_from = '20190101'
                                                            id_date_to   = '20190201' ).

WRITE: / lv_months.

* Anzahl Jahre, Monate, Tage
DATA: lv_years TYPE i.
DATA: lv_months TYPE i.
DATA: lv_days TYPE i.

cl_reca_date=>get_date_diff( EXPORTING
                               id_date_from     = '20190101'
                               id_date_to       = '20190201'
                             IMPORTING
                               ed_years         = lv_years
                               ed_months        = lv_months
                               ed_calendar_days = lv_days
                           ).

WRITE: / lv_years.
WRITE: / lv_months.
WRITE: / lv_days.

[ABAP] JavaScript-Code über MSScriptControl ausführen

* http://www.thescarms.com/VBasic/Scripting.aspx

* JS-Code mit Beispielfunktionen einfügen
DATA(it_js_code) = VALUE stringtab( ( |var oJSON = \{| )
                                    ( |  "name" : "Heinz",| )
                                    ( |  "age"  : 56,| )
                                    ( |  "city" : "Hamburg"| )
                                    ( |\};| )
                                    ( |function add(a, b) \{| )
                                    ( |  return a + b;| )
                                    ( |\};| )
                                    ( |function getJSONString() \{| )
                                    ( |  return oJSON["name"];| )
                                    ( |\};| )
                                   ).

DATA: lv_jscode TYPE string.

* VBA-Code in String wandeln
LOOP AT it_js_code ASSIGNING FIELD-SYMBOL(<c>).
  DATA(lv_codeline) = condense( <c> ).
  IF lv_jscode IS INITIAL.
    lv_jscode = lv_codeline.
  ELSE.
    lv_jscode = |{ lv_jscode }{ cl_abap_char_utilities=>cr_lf }{ lv_codeline }|.
  ENDIF.
ENDLOOP.

* Scriptcontrol-Objekt erzeugen
DATA: o_scr TYPE ole2_object.
* Variablen für Fehleranalyse
DATA: o_error TYPE ole2_object.
DATA: lv_line TYPE string.
DATA: lv_number TYPE string.
DATA: lv_desc TYPE string.
DATA: lv_source TYPE string.

CREATE OBJECT o_scr 'MSScriptControl.ScriptControl'.

* wenn Erzeugung ok
IF sy-subrc = 0 AND o_scr-handle <> 0 AND o_scr-type = 'OLE2'.
  WRITE: / 'Header', o_scr-header.
  WRITE: / 'Type', o_scr-type.
  WRITE: / 'Handle', o_scr-handle.
  WRITE: / 'CB-Index', o_scr-cb_index.
  WRITE: / 'CLSID', o_scr-clsid.

* GUI anzeigen
  SET PROPERTY OF o_scr 'AllowUI' = 1.
* Sprache ist 'JScript'
  SET PROPERTY OF o_scr 'Language' = 'JScript'.

* Code hinzufügen und Syntaxcheck
  CALL METHOD OF o_scr 'AddCode'
    EXPORTING
      #1 = lv_jscode.

* wenn Syntax ok
  IF sy-subrc = 0.
* Funktionsaufruf mit Übergabeparametern
    DATA: lv_res_i TYPE i.
    CALL METHOD OF o_scr 'Eval' = lv_res_i
      EXPORTING
        #1 = 'add(1, 2)'.

    WRITE: / 'add(1, 2) =', lv_res_i.

* Aufruf Stringfunktion
    DATA: lv_res_str TYPE string.
    CALL METHOD OF o_scr 'Eval' = lv_res_str
      EXPORTING
        #1 = 'getJSONString()'.

    WRITE: / 'getJSONString() =', lv_res_str.

* Aufruf einer nicht vorhandenen Funktion --> Fehlerauswertung
    DATA: lv_res_input TYPE string.
    CALL METHOD OF o_scr 'Eval' = lv_res_input
      EXPORTING
        #1 = 'getInput()'.

    IF sy-subrc = 0.
      WRITE: / 'getInput() =', lv_res_input.
    ELSE.
* Fehlerauswertung
      GET PROPERTY OF o_scr 'Error' = o_error.
      GET PROPERTY OF o_error 'Line' = lv_line.
      GET PROPERTY OF o_error 'Number' = lv_number.
      GET PROPERTY OF o_error 'Description' = lv_desc.
      GET PROPERTY OF o_error 'Source' = lv_source.

      WRITE: / 'Line:', lv_line.
      WRITE: / 'Number:', lv_number.
      WRITE: / 'Desc:', lv_desc.
      WRITE: / 'Source:', lv_source.
    ENDIF.
  ELSE.
    WRITE: / 'Fehler im Code: ', sy-subrc.
  ENDIF.

* Objekt zerstören
  FREE OBJECT o_scr.
ENDIF.

[ABAP] Neuen Modus öffnen, Transaktion ausführen, Modus schließen

TRY.
* Modus öffnen
    DATA: o_mode TYPE REF TO if_imc_child_mode.

    cl_imc_mode=>create_mode( EXPORTING modetype = cl_imc_mode=>modetype_main_mode
                              IMPORTING mode     = o_mode ).

* Transaktion starten
    o_mode->call( tcode = 'SE11'
*              data  =
                ).

    DATA: ret TYPE string.

* Modus wieder schließen?
    CALL FUNCTION 'POPUP_TO_CONFIRM'
      EXPORTING
        titlebar              = 'Frage'
        text_question         = 'Modus wieder schließen?'
        display_cancel_button = abap_false
      IMPORTING
        answer                = ret.

    CASE ret.
      WHEN '1'.
* Ja -> Modus schließen
        o_mode->close( ).
    ENDCASE.
  CATCH cx_root INTO DATA(e_txt).
ENDTRY.