Meine Notizen zu SAP ABAP

Last Updated on 20. August 2021 by Thomas J. Fehr

AAdvanced
BBusiness
AApplication
PProgramming

ABAP Concepts:

  • Transaction
  • Programm
    • Manage Business Process
  • Screen
  • Data Dictionary
    • Database
  • Subroutine
    • Divide long programs to a subroutine
  • GUI Status
    • Custom Menus

ABAP Objects

Naming Conventions

  • Customer objects prefixed with “Y” or “Z” (from A to X used by SAP)
  • Object names denote functionality
  • Variable names denote usage
  • Never use literal values (SELECT * from VBAK WHERE AUART IN (‘ZSP0’, ‘ZSP2’).
  • Interfacenames start with IF_* / ZIF_*
  • Variablename: lv_<name> => local variable

Prefixes

lv_local variable
ls_local structure
ty_type (Datastructure)
lt_local table

A Framework Class

Global Class

Inheriting a global class

Remove final attribute from the class to inheriting

Change the visibility from the instance attribute to protected

To inheriting a class do:

CLASS <name> DEFINITION
  INHERITING FROM <class_to_inheriting_from>.

Abstract Class

Define a base class as abstract

Sonstiges

lv_text = TEXT-t01 && | | && lv_text.
Space character in a text&& | | &&
TEXT-<xxx>Textsymbol
*Comment out a line
CLASS <name> DEFINITION
ENDCLASS.
Interface
CLASS <name> IMPLEMENTATION
ENDCLASS.
Class object
CLASS IMPLEMENTATION
INHERITING FROM <class_name>.
ENDCLASS.
Inheriting
METHODES:
show_details REDEFINITION.
Overwriting an inheriting methode
super->show_details( ).Call the methode in the superclass
TRY.
lcl_start=>run ( ) .
CATCH cx_root INTO lcl_start=>mo_error.
ENDTRY.
Unhandled exceptions cause short dumps
1Paket
2Unterpaket
3Globale Klassen eines Unterpaketes
4Programme (Reports) eines Unterpaketes
5Program (Report) mit Klassen, Felder usw.

Prüfen, aktivieren und ausführen

ABAP Unit / Test Classes

CLASS lcl_test DEFINITION
      FOR TESTING
      DURATION SHORT
      RISK LEVEL harmless.

      PRIVATE SECTION.
      DATA: lo_test TYPE REF TO zcl_main "class under test
      METHODES: setup, teardown          "Fixture methodes
      METHODES: test_method1 FOR TESTING "Test method

ENDCLASS.
FOR TESTINGmark as an unit test class
DURATIONSHORT
MEDIUM
LONG
RISK LEVELHARMLESS (no changes to data or settings)
DANGEROUS (date may changed)
CRITICAL (data or settings may be changed)
CL_ABAP_UNIT_ASSERTEvaluating test results

Elementare Datentypen

Elementare ABAP-Typen

cTextfeldFixe länge
NNummern TextfeldFixe länge
DDatumsfeldFixe länge
TZeitfeldFixe länge
XHexadezimalFixe länge
PGepackte ZahlFixe länge
IIntegerFixe länge
FFloatFixe länge
STRINGTextVariable Länge
XSTRINGByteVariable Länge

Lokale Datenstruktur

TYPES: BEGIN OF ty_matdata,
  matnr TYPE c LENGTH 15,
  mattext TYPE c LENGTH 30,
  matunit TYPE c LENGTH 3,
  END OF ty_matdata.

Zuweisung / Nutzung der lokalen Struktur

data ls_matdata type ty_matdata.

ls_matdata-matnr = '1'.

Lokale Tabelle

data lt_material type TABLE OF ty_matdata.

Tabelle befüllen:

do 5 times.

* sy-index => runtime variable mit aktuellem index
  ls_matdata-matnr = sy-index.
  CONCATENATE 'Mein Material' ls_matdata-matnr into ls_matdata-mattext.
  ls_matdata-matunit = 'ST'.
  append ls_matdata to lt_material.

ENDDO.

Tabelle ausgeben:

loop at lt_material into ls_matdata.
* / => Zeilenumbruch  
  write / ls_matdata.
ENDLOOP.

Tabelle ausgeben mit FIELD-SYMBOLS: (Performence)

*&---------------------------------------------------------------------*
* FIELD-SYMBOLS
*&---------------------------------------------------------------------*
FIELD-SYMBOLS <fs_matdata> type any.

loop at lt_material ASSIGNING <fs_matdata>.
* / => Zeilenumbruch
  write / <fs_matdata>.
ENDLOOP.

Wichtige Befehle

DO x TIME.
<code>
ENDDO.
CONCATENATE
APPEND
LOOP AT <table> INTO <record>.
ENDLOOP.
WRITE
DATA <name> TYPE <Datentyp>
IF <Abfrage>. (=, >, <…)
ELSEIF <Abfrage>
ELSE.
ENDIF.
CASE <Variable>.
WHEN <Bedingung>.
WHEN <Bedingung 2>.
WHEN OTHERS.
END CASE.

Open SQL

SELECT * FROM <table> INTO TABLE <lt_name>Einfache Select-Abfrage
SELECT * FROM <table> INTO CORRESPONDING FIELDS OF TABLE <lt_name> Select-Abfrage, welche nur die namensgleichen Felder berücksichtigt

Nützliches

GitHub: SAP Styleguides

https://github.com/SAP/styleguides/blob/main/clean-abap/cheat-sheet/CleanABAPCheatSheetV1.4.1.pdf

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.