Monday, March 11, 2024

SAP ABAP: Determine Timezone for Plant

    DATA:

      lt_tzone TYPE STANDARD TABLE OF tznzone WITH DEFAULT KEY,

      l_tzone  TYPE tznzone.


    " get time zone for plant

    SELECT SINGLE land1, regio FROM t001w

      WHERE werks = @pa_werks 

      INTO ( @DATA(l_land1), @DATA(l_regio) ).

    IF sy-subrc = 0.

      IF l_regio IS INITIAL.

        " determine timezone without region

        SELECT tzone FROM ttz5 WHERE land1 = @l_land1 

          INTO TABLE @lt_tzone.

      ELSE.

        " determine timezone with region

        SELECT tzone FROM ttz5s WHERE

          land1 = @l_land1 AND

          bland = @l_regio AND

          tzonedft = 'X'

          INTO TABLE @lt_tzone.

      ENDIF.

      IF lines( lt_tzone ) > 0.

        SORT lt_tzone. " ensure stable result

        l_tzone = lt_tzone[ 1 ].

      ENDIF.

    ENDIF.

    IF sy-subrc <> 0.

      l_tzone = 'CET'.

    ENDIF.

Wednesday, June 30, 2021

Custom Alert in PP/DS

Create new alert type in table /sapapo/atype



Use BAdI /SAPAPO/AM_ALERTLIST

Implement Method READ_CUSTDEF_PP_ALERTS

  DATA:

    ls_atype      TYPE /sapapo/satypes,

    ls_alert      TYPE /sapapo/alert_str,

    ls_a          TYPE /sapapo/requirements_alertstr, " /sapapo/lc_mat_order_alertstr.


  " check if alert type should be considered

  READ TABLE t_atypes INTO ls_atype WITH TABLE KEY

    aot   = mc_aot_req_receipt_alert

    at_id = mc_my_new_alerttype.

  CHECK sy-subrc = 0.


  FIELD-SYMBOLS:

    <ls_alert_general> TYPE c,

    <ls_alert_order>   TYPE c.


    " generate alert

    ls_a-aot   = mc_aot_req_receipt_alert.   " /SAPAPO/C_AOTYPE Object Type of Alert

    ls_a-at_id = mc_my_new_alerttype. " /SAPAPO/C_ATYPE Type of Alert

    " ls_a-status_icon = 'W'.

    " ls_a-priority = '2'. " Priority of Alert - warning

    " ls_a-prioicon = ''. " Priority - Icon

    " ls_a-colcode = ''. " Line Color in ALV

    ls_a-description    = 'my alert'. " Text for Alert

    ls_a-key_fig        = ls_io-real_quantity.

    CONDENSE ls_a-key_fig.

    ls_a-aid_int        = cl_system_uuid=>create_uuid_c22_static( ). 

    ls_a-aid            = ls_a-aid_int. " Alert ID

    ls_a-vfrom          = vfrom.

    ls_a-vto            = vto.

    ls_a-uname_from     = sy-uname.

    ls_a-sec_attr       = 'X'.

    ls_a-plver          = plver.

    ls_a-pegid          = ls_io-pegid.

    ls_a-matid          = ls_matloc-matid.

    ls_a-locid          = ls_matloc-locid.

    ls_a-ordertxt       = ls_atpcat-atptext40.

    ls_a-orderid        = ls_io-orderid.

    ls_a-position_no    = ls_io-position_no.

    ls_a-line_no        = ls_io-line_no.

    ls_a-is_input_node  = ls_io-is_input_node.

    ls_a-orderpriority  = ls_io-priority.

    ls_a-orderisplanned = 'X'.

    ls_a-atpcat         = ls_io-category.

    ls_a-actid          = ls_io-actid.

    ls_a-matnr          = ls_matloc-matnr.

    ls_a-maktx          = ls_mattxt-maktx.

    ls_a-locno          = ls_matloc-locno.

    ls_a-loctx          = ls_loct-descr40.


    <ls_alert_general> = <ls_alert_order>.

    INSERT ls_alert INTO TABLE t_alerts.


Thursday, September 3, 2020

Enable SAP to communicate via https to AWS

 1. Get Client Certificate from https://www.amazontrust.com/repository/

    e.g. CN=Amazon Root CA 1,O=Amazon,C=US

    Show the PEM-Format and save it as amazon.pse

2. Open transaction STRUST and switch to change mode

   Double-click node "SSL-Client..." in tree

   Use Button "Import Certificate" (at the bottom) to import amazon.pse (saved in 1.)

   Use Button "In Liste aufnehmen" and save.


Test with:

REPORT zjso_http_request.

CONSTANTS:
  gc_proxy_host    TYPE string VALUE 'http-proxy.xxx.xxx',
  gc_proxy_service TYPE string VALUE '1234'.

CLASS lcl_test DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS do.
ENDCLASS.

START-OF-SELECTION.
  lcl_test=>do).

CLASS lcl_test IMPLEMENTATION.
  METHOD do.

    DATA:
      l_url           TYPE string,
      l_client        TYPE REF TO if_http_client,
      l_http_status   TYPE i,
      l_status_text   TYPE string,
      lt_cookie       TYPE tihttpcki,
      ls_cookie       TYPE ihttpcki,
      lt_header_field TYPE tihttpnvp,
      ls_header_field TYPE ihttpnvp,
      l_str_msg       TYPE string,
      l_raw_message   TYPE xstring,
      o_conv_r        TYPE REF TO cl_abap_conv_in_ce.

  
    l_url `http://dummy.restapiexample.com/api/v1/employees`.

    TRY.
        cl_http_client=>create_by_url(
          EXPORTING
            url           l_url
            proxy_host    gc_proxy_host
            proxy_service gc_proxy_service
          IMPORTING client  l_client
        ).
        CHECK l_client IS BOUND.

        " Anmeldedaten übermitteln
        " l_client->authenticate( username = 'abc' password = 'def' ).

        " Logon-Popup ein- bzw. ausschalten
        l_client->propertytype_logon_popup l_client->co_enabled.

        " HTTP-Prtotokoll-Version
        l_client->request->set_versionversion if_http_request=>co_protocol_version_1_1 ).

        " HTTP-Method
        l_client->request->set_methodif_http_request=>co_request_method_get ).

        " Header-Felder explizit setzen
*      l_client->request->set_header_field( name  = '~request_method' value = 'GET' ).
*      l_client->request->set_header_field( name  = 'Content-Type' value = 'text/xml; charset=utf-8' ).
*      l_client->request->set_header_field( name  = 'Accept' value = 'text/xml, text/html' ).

        " Cookies akzeptieren
        l_client->propertytype_accept_cookie if_http_client=>co_enabled.
        " Kompression akzeptieren
        l_client->propertytype_accept_compress if_http_client=>co_enabled.

        " HTTP GET senden, evtl. Timeout angeben
        l_client->send(
          EXCEPTIONS
            http_communication_failure  1
            http_invalid_state          2
            http_processing_failed      3
            OTHERS                      4
        ).
        IF sy-subrc <> 0.
          MESSAGE 'Exception nach send' TYPE 'E'.
        ENDIF.
        WRITE'Request send to'l_url.
        SKIP.

        " Response lesen
        l_client->receive(
         EXCEPTIONS
           http_communication_failure 1
           http_invalid_state         2
           http_processing_failed     3
           OTHERS                     4
        ).
        IF sy-subrc <> 0.
          WRITE 'Exception nach Receive' COLOR COL_NEGATIVE.
        ENDIF.

        " HTTP Return Code holen
        l_client->response->get_status(
          IMPORTING
            code   l_http_status
            reason l_status_text
        ).
        WRITE:
          / 'HTTP_STATUS_CODE:'l_http_status,
          / 'STATUS_TEXT:'l_status_text.
        SKIP.

        " Header-Daten der Response
        WRITE'HEADER FIELDS' COLOR COL_HEADING.
        l_client->response->get_header_fieldsCHANGING fields lt_header_field ).
        LOOP AT lt_header_field INTO ls_header_field.
          WRITE'  ['ls_header_field-name']  'ls_header_field-value.
        ENDLOOP.
        SKIP.

        " Cookies holen
        WRITE'COOKIES' COLOR COL_HEADING.
        l_client->response->get_cookiesCHANGING cookies lt_cookie ).
        LOOP AT lt_cookie INTO ls_cookie.
          WRITE'['ls_cookie-name']  'ls_cookie-valuels_cookie-xdomainls_cookie-pathls_cookie-securels_cookie-expires.
        ENDLOOP.
        SKIP.

        " vollständige HTTP Nachricht lesen
        " CHECK l_http_status = 200.
        l_raw_message l_client->response->get_raw_message).

        " xstring -> string
*        o_conv_r = cl_abap_conv_in_ce=>create( input = l_raw_message  encoding = 'UTF-8' ).
*        o_conv_r->read( IMPORTING data = l_str_msg ).
        cl_abap_conv_in_ce=>createinput l_raw_message  encoding 'UTF-8' )->readIMPORTING data l_str_msg ).
        WRITE'RAW MESSAGE'l_str_msg.
        SKIP.

        " HTTP Body als Character-Daten
        l_str_msg l_client->response->get_cdata).
        WRITE 'CDATA' COLOR COL_HEADING.
        WRITE / l_str_msg.
        SKIP.

        " HTTP Connection schließen
        l_client->close).

      CATCH cx_root INTO DATA(l_x).
        WRITE / l_x->get_textCOLOR COL_NEGATIVE.
    ENDTRY.

  ENDMETHOD.
ENDCLASS.

SAP ABAP: Determine Timezone for Plant

    DATA:       lt_tzone TYPE STANDARD TABLE OF tznzone WITH DEFAULT KEY,       l_tzone  TYPE tznzone.     " get time zone for plant   ...