[ABAP] Leistungstammsatz anlegen

DATA: lv_service  TYPE bapisrv_asmd.
DATA: lv_servicex TYPE bapisrv_asmdx.
DATA: it_bapiret2 TYPE STANDARD TABLE OF bapiret2 WITH DEFAULT KEY.

lv_service-service           = '1234567890'. " Leistungsnummer
lv_service-matl_group        = '10000000'.   " Warengruppe
lv_service-base_uom          = 'ST'.         " Basismengeneinheit
lv_service-serv_cat          = 'SERV'.       " Leistungstyp
lv_service-val_class         = '1000'.       " Bewertungsklasse
lv_service-master_langu      = sy-langu.     " Sprache
lv_service-master_langu_iso  = cl_i18n_languages=>sap1_to_sap2( im_lang_sap1 = lv_service-master_langu ).

lv_servicex-service          = lv_service-service.
lv_servicex-matl_group       = abap_true.
lv_servicex-base_uom         = abap_true.
lv_servicex-serv_cat         = abap_true.
lv_servicex-val_class        = abap_true.
lv_servicex-master_langu     = abap_true.
lv_servicex-master_langu_iso = abap_true.

DATA it_shorttext TYPE STANDARD TABLE OF bapisrv_asmdt WITH DEFAULT KEY.

it_shorttext = VALUE #( ( language     = lv_service-master_langu
                          language_iso = lv_service-master_langu_iso
                          short_text   = 'Kurztext'
                          change_id    = 'I' ) ).

DATA(it_longtext) = VALUE srv_sm_text_tty( ( language     = sy-langu
                                             language_iso = cl_i18n_languages=>sap1_to_sap2( im_lang_sap1 = sy-langu )
                                             format       = '/'
                                             line         = 'Langtext'
                                             change_id    = 'I' ) ).

CALL FUNCTION 'BAPI_SERVICE_CREATE'
  EXPORTING
    im_service_data     = lv_service
    im_service_datax    = lv_servicex
    testrun             = ''
  TABLES
    return              = it_bapiret2
    service_description = it_shorttext
    service_long_texts  = it_longtext.

TRY.
    IF lines( it_bapiret2 ) > 0.
      IF it_bapiret2[ 1 ]-type CA 'EAX'.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
      ENDIF.
    ENDIF.
  CATCH cx_sy_itab_line_not_found.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDTRY.

cl_demo_output=>write_data( lv_service ).
cl_demo_output=>write_data( lv_servicex ).
cl_demo_output=>write_data( it_shorttext ).
cl_demo_output=>write_data( it_longtext ).
cl_demo_output=>write_data( it_bapiret2 ).

* HTML-Code vom Demo-Output holen
DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
cl_abap_browser=>show_html( EXPORTING
                              title        = 'Leistungsstammsatz'
                              html_string  = lv_html
                              container    = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
WRITE: space.

[ABAP] Leistungstammsatz lesen

PARAMETERS: p_srv_nr TYPE asmd-asnum. "bapisrvpar-service.

START-OF-SELECTION.
  DATA: lv_service_info TYPE bapisrv.
  DATA: it_servicetextlines TYPE STANDARD TABLE OF bapisrvtxt WITH DEFAULT KEY.
  DATA: it_bapiret2 TYPE STANDARD TABLE OF bapiret2 WITH DEFAULT KEY.

  CALL FUNCTION 'BAPI_SERVICE_GET_DETAIL'
    EXPORTING
      servicenumber      = p_srv_nr
    IMPORTING
      servicegeneraldata = lv_service_info
    TABLES
      servicetextlines   = it_servicetextlines
      return             = it_bapiret2.

  cl_demo_output=>write_data( p_srv_nr ).
  cl_demo_output=>write_data( lv_service_info ).
  cl_demo_output=>write_data( it_servicetextlines ).
  cl_demo_output=>write_data( it_bapiret2 ).

* HTML-Code vom Demo-Output holen
  DATA(lv_html) = cl_demo_output=>get( ).

* Daten im Inline-Browser im SAP-Fenster anzeigen
  cl_abap_browser=>show_html( EXPORTING
                                title        = 'Leistungsstammsatz'
                                html_string  = lv_html
                                container    = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
  WRITE: space.

[ABAP] Adressenaufbereitung mit CUSTOMER_ADDRESS_TO_ITF

DATA: lv_address TYPE bapiaddr3.
DATA: it_bapi_return TYPE STANDARD TABLE OF bapiret2 WITH DEFAULT KEY.
 
CALL FUNCTION 'BAPI_USER_GET_DETAIL'
  EXPORTING
    username = sy-uname
  IMPORTING
    address  = lv_address
  TABLES
    return   = it_bapi_return.

DATA: lv_customer TYPE scustom.
lv_customer-name = lv_address-fullname.
lv_customer-street = lv_address-street.
lv_customer-postbox = lv_address-po_box.
lv_customer-postcode = lv_address-postl_cod1.
lv_customer-city   = lv_address-city.
lv_customer-country = lv_address-country.
lv_customer-region = lv_address-region.
lv_customer-telephone = lv_address-tel1_numbr.
lv_customer-email = lv_address-e_mail.

DATA: lv_sender_country TYPE szad_field-send_cntry.
lv_sender_country = 'US'.

DATA: it_address_lines TYPE tline_tab.

CALL FUNCTION 'CUSTOMER_ADDRESS_TO_ITF'
  EXPORTING
    is_customer        = lv_customer
    iv_sending_country = lv_sender_country
*   IV_NUMBER_OF_LINES = 5
  IMPORTING
    et_address         = it_address_lines.

cl_demo_output=>write_data( lv_address ).
cl_demo_output=>write_data( lv_customer ).
cl_demo_output=>write_data( lv_sender_country ).
cl_demo_output=>write_data( it_address_lines ).
cl_demo_output=>display( ).

[ABAP] Adressenaufbereitung mit ADDRESS_INTO_PRINTFORM

* https://www.berater-wiki.de/Adressenaufbereitung#Funktionsbaustein_ADDRESS_INTO_PRINTFORM

DATA: lv_address TYPE bapiaddr3.
DATA: it_bapi_return TYPE STANDARD TABLE OF bapiret2 WITH DEFAULT KEY.

CALL FUNCTION 'BAPI_USER_GET_DETAIL'
  EXPORTING
    username = sy-uname
  IMPORTING
    address  = lv_address
  TABLES
    return   = it_bapi_return.

DATA: ls_address TYPE  adrs1.
ls_address-title_text = lv_address-title_p.
ls_address-name1      = lv_address-fullname.
ls_address-street     = lv_address-street.
*  ls_address-po_box     = is_customer-postbox.
ls_address-post_code1 = lv_address-postl_cod1.
*  ls_address-region     = is_customer-region.
ls_address-city1      = lv_address-city.
ls_address-country    = lv_address-country.
DATA: lv_sender_country TYPE land1.
lv_sender_country = 'US'.

DATA: it_address_lines TYPE szadr_printform_table.
CALL FUNCTION 'ADDRESS_INTO_PRINTFORM'
  EXPORTING
    address_1               = ls_address
    address_type            = '1' "normal/company
    sender_country          = lv_sender_country
    *    number_of_lines         = 5
  IMPORTING
    address_printform_table = it_address_lines.
    
cl_demo_output=>write_data( ls_address ).
cl_demo_output=>write_data( lv_sender_country ).
cl_demo_output=>write_data( it_address_lines ).
cl_demo_output=>display( ).

[ABAP] Neue / obsolete Transaktionscodes für S/4 HANA suchen

* https://thinkdoforward.com/neue-transaktionen-mit-s-4hana-so-einfach-findest-zu-sie/
PARAMETERS: p_rel TYPE saprl DEFAULT '751'.
SELECT-OPTIONS: so_ta FOR lv_s_tcode.

START-OF-SELECTION.

  SELECT c~rel_name, c~s_tcode, c~t_tcode, tx~ttext
    INTO TABLE @DATA(it_prgn_corr2)
    FROM prgn_corr2 AS c
    INNER JOIN tstc AS t ON ( c~t_tcode = t~tcode )
    RIGHT OUTER JOIN tstct AS tx ON ( t~tcode = tx~tcode )
    WHERE c~rel_name >= @p_rel
      AND c~s_tcode IN @so_ta
      AND tx~sprsl = @sy-langu.

  SORT: it_prgn_corr2 BY s_tcode rel_name.


  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_prgn_corr2 ).

* 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( 'Überschrift' ).
      o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
      o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

      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.

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

[ABAP] Textpatterns in Strings mittles Vergleichsoperator CP (Covers Pattern) suchen

* https://www.berater-wiki.de/Vergleichsoperatoren_Zeichen
* Variante 1 (Wildcard)
DATA(lv_test) = |abc-123|.

IF lv_test CP '*-*'.
  WRITE: / '- in der Mitte.'.
ELSE.
  WRITE: / 'kein - in der Mitte'.
ENDIF.

* Variante 2 (bestimmtes Zeichen)
DATA(lv_test) = |abc-123|.

IF lv_test CP 'ABC-*'.
  WRITE: / 'Schema ABC-XXX'.
ELSE.
  WRITE: / 'kein Schema ABC-XXX'.
ENDIF.

* Variante 3 (bestimmtes Zeichen)
DATA(lv_test) = |abc-1234|.

IF lv_test CP '+++-+++'.
  WRITE: / 'Schema XXX-XXX'.
ELSE.
  WRITE: / 'kein Schema XXX-XXX'.
ENDIF.

[ABAP] Fixkontentabelle (T030) auslesen

* Kontenplan
DATA: lv_ktopl TYPE ktopl.
* Vorgangsschlüssel
DATA: lv_ktosl TYPE ktosl.
* Bewertungsmodifikationskonstante
DATA: lv_bwmod TYPE bwmod.
* Konto-Modifikation
DATA: lv_komok TYPE komok.
* Bewertungsklasse
DATA: lv_bklas TYPE bklas.
* Nummer des Sachkontos
DATA: lv_konts TYPE saknr.
* Nummer des Sachkontos
DATA: lv_konth TYPE saknr.

SELECT-OPTIONS: so_ktopl FOR lv_ktopl.
SELECT-OPTIONS: so_ktosl FOR lv_ktosl.
SELECT-OPTIONS: so_bwmod FOR lv_bwmod.
SELECT-OPTIONS: so_komok FOR lv_komok.
SELECT-OPTIONS: so_bklas FOR lv_bklas.
SELECT-OPTIONS: so_konts FOR lv_konts.
SELECT-OPTIONS: so_konth FOR lv_konts.

START-OF-SELECTION.

* T030 (Fixkontentabelle)
  SELECT *
    INTO TABLE @DATA(it_t030)
    FROM t030
    WHERE ktopl IN @so_ktopl.
*      ktosl IN @so_ktosl
*      bwmod IN @so_bwmod
*      komok IN @so_komok
*      bklas IN @so_bklas
*      konts IN @so_konts
*      konth IN @so_konth.

  IF sy-subrc = 0.
    TRY.
        DATA: o_salv TYPE REF TO cl_salv_table.

        cl_salv_table=>factory( EXPORTING
                                  r_container  = cl_gui_container=>default_screen
                                IMPORTING
                                  r_salv_table = o_salv
                                CHANGING
                                  t_table      = it_t030 ).

        o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
        o_salv->get_columns( )->set_optimize( abap_true ).
        o_salv->get_functions( )->set_all( ).
        o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).
        o_salv->display( ).

        WRITE: / space.
      CATCH cx_root INTO DATA(e_text).
        WRITE: / e_text->get_text( ).
    ENDTRY.
  ENDIF.

[ABAP] Einkaufsbestelltext zum Material auslesen

* https://www.berater-wiki.de/SAPscript-Textbaustein_lesen_mit_Funktionsbaustein_READ_TEXT_und_speichern_mit_SAVE_TEXT
* Texte: Anwendungsobjekt
* siehe SE75
CONSTANTS: co_obj_type TYPE thead-tdobject VALUE 'MATERIAL'. " Materialtexte Einkauf/Lagerung
* Text-ID
* siehe SE75 --> Doppelklick auf Objekt
CONSTANTS: co_obj_id TYPE thead-tdid VALUE 'BEST'. " Einkaufsbestelltext

DATA: it_thead TYPE STANDARD TABLE OF thead WITH DEFAULT KEY.
DATA: it_lines TYPE tline_t.
DATA: it_ascii TYPE tdtab_c132.

* Materialnummer mit führenden Nullen
DATA(lv_matnr) = CONV matnr( '000000001122334455' ).

* erst 'SELECT_TEXT' zur Prüfung aufrufen (ob Text vorhanden)
* 'READ_TEXT' wirft sonst bei Nichtvorhandensein des Textes eine Abbruchmessage
* Texterstellung Text auswählen
CALL FUNCTION 'SELECT_TEXT'
  EXPORTING
    object                  = co_obj_type
    name                    = CONV thead-tdname( lv_matnr )
    id                      = co_obj_id
    language                = sy-langu
  TABLES
    selections              = it_thead   " Textheader der gefundenen Texte
  EXCEPTIONS
    wrong_access_to_archive = 1
    OTHERS                  = 2.

IF sy-subrc = 0.
  IF lines( it_thead ) > 0.
* Texterstellung Text lesen
    CALL FUNCTION 'READ_TEXT'
      EXPORTING
        id                      = co_obj_id
        language                = sy-langu
        name                    = CONV thead-tdname( lv_matnr )
        object                  = co_obj_type
      TABLES
        lines                   = it_lines " Textzeilen des gelesenen Textes
      EXCEPTIONS
        id                      = 1
        language                = 2
        name                    = 3
        not_found               = 4
        object                  = 5
        reference_check         = 6
        wrong_access_to_archive = 7
        OTHERS                  = 8.

    IF sy-subrc = 0.
      IF lines( it_lines ) > 0.
* Textkonvertierung ITF (SAPscript-Format) in ASCII
* Stile und Formatierungen aus SAP-Script-Text entfernen
        CALL FUNCTION 'CONVERT_ITF_TO_ASCII'
          EXPORTING
            formatwidth       = 132
          IMPORTING
            c_datatab         = it_ascii
          TABLES
            itf_lines         = it_lines
          EXCEPTIONS
            invalid_tabletype = 1
            OTHERS            = 2.
        IF sy-subrc = 0.
          cl_demo_output=>write_data( it_thead ).
          cl_demo_output=>write_data( it_lines ).
          cl_demo_output=>write_data( it_ascii ).
          cl_demo_output=>display( ).
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.
ENDIF.