Pages

Friday, September 13, 2024

Calling External API from ABAP to send an SMS

Requirement: Call an external API to send an SMS message.
Prerequisite: RFC destination and external API.

Note: Make sure to test the RFC connection in SM59. Check the SSL certificates. Ask basis team for help for any connection issues.
 
    DATA: lv_message     TYPE string,

          lv_date        TYPE char10,

          lv_msg         TYPE string,

          lr_client      TYPE REF TO if_http_client,

          lo_rest_client TYPE REF TO cl_rest_http_client,

          lo_response    TYPE REF TO if_rest_entity.


    " This is the message that will be sent to SMS

    WRITE us_flt_order-sddt TO lv_date.

    lv_message = |Change Notification: FO# | && us_flt_order-flt_ordno

      && | Flight | && us_flt_order-fltnr && | to | && us_flt_order-dstn

      && | on | && lv_date.

 

    " Create a link to the API by the RFC destination

    CALL METHOD cl_http_client=>create_by_destination

      EXPORTING

        destination              = 'SMS_API_1' "RFC destination

      IMPORTING

        client                   = lr_client

      EXCEPTIONS

        argument_not_found       = 1

        destination_not_found    = 2

        destination_no_authority = 3

        plugin_not_active        = 4

        internal_error           = 5

        OTHERS                   = 6.

 

    "Instantiate REST client

    CREATE OBJECT lo_rest_client

      EXPORTING

        io_http_client = lr_client.

 

    lr_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).

 

    IF lr_client IS BOUND AND lo_rest_client IS BOUND.

 

      DATA(request_entity) = lo_rest_client->if_rest_client~create_request_entity( ).

      CALL METHOD lo_rest_client->if_rest_client~set_request_header

        EXPORTING

          iv_name  = 'Content-Type'

          iv_value = 'application/json'. "Set your header.


      "Set JSON payload

      request_entity->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).

      lv_msg = '{ "msg" : "' && lv_message && '" }' .

      request_entity->set_string_data( lv_msg ).

 

      "Perform POST

      lo_rest_client->if_rest_resource~post( request_entity ).

 

      "Get response

      lo_response = lo_rest_client->if_rest_client~get_response_entity( ).


      "Get status. 200 for success

      DATA(http_status) = lo_response->get_header_field( '~status_code' ).

      DATA(reason) = lo_response->get_header_field( '~status_reason' ).


      "Read response

      DATA(response) = lo_response->get_string_data( ).

 

      "Close the connection

      CALL METHOD lr_client->close

        EXCEPTIONS

          http_invalid_state = 1

          OTHERS             = 2.

    ENDIF.

 

Friday, September 5, 2014

Sending an email attachment with more than 255 characters

Note: Sample code in program BCS_EXAMPLE_7
  1.  Put everything in a string 

      DATA: lv_string TYPE string, "temporary string
            lv_fkimg TYPE char17, "quantity
            lv_unit TYPE char14, "unit price
            lv_lines TYPE sy-tabix. "lines


      FIELD-SYMBOLS <lfs_alv> TYPE gy_inv_alv. "for my table

      CONSTANTS:lc_tab TYPE char2 

                      VALUE cl_abap_char_utilities=>horizontal_tab, "tab
                lc_cret TYPE char2 
                      VALUE cl_abap_char_utilities=>cr_lf. "new line

    *Get the number of records
      DESCRIBE TABLE pt_inv LINES lv_lines.


    *Get the data from internal table
      LOOP AT pt_inv ASSIGNING <lfs_alv>.
        CLEAR: lv_string,
               lv_fkimg,
               lv_unit,
               lv_gross.

    *Convert numeric to character
        MOVE <lfs_alv>-fkimg TO lv_fkimg. "quantity
        MOVE <lfs_alv>-unitprice TO lv_unit. "unit price


    *Put negative sign in front
        CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
          CHANGING
            value = lv_unit.

    *Save everything in a temporary string
        CONCATENATE <lfs_alv>-vkorg
                    <lfs_alv>-kunwe
                    <lfs_alv>-ship_name
                    <lfs_alv>-we_street
                    <lfs_alv>-we_street2
                    <lfs_alv>-we_city
                    <lfs_alv>-we_country
                    <lfs_alv>-we_postcode
                    <lfs_alv>-we_telnum
                    <lfs_alv>-matnr
                    <lfs_alv>-arktx
                    <lfs_alv>-fkdat
                    <lfs_alv>-vbeln
                    lv_fkimg
                    lv_unit
          INTO lv_string RESPECTING BLANKS.

    *Add the temporary string and new line to the final string
        IF sy-tabix NE lv_lines.
          CONCATENATE pv_string lv_string lc_cret INTO pv_string.
        ELSE.

    *For the last record, don't add new line
          CONCATENATE pv_string lv_string INTO pv_string.
        ENDIF.
      ENDLOOP.

  2. Convert string to binary table and attach to email

      DATA: lt_attach TYPE solix_tab, "binary table
            lv_subject TYPE so_obj_des, "subject
            send_request       TYPE REF TO cl_bcs,
            lt_text            TYPE bcsy_text, "body msg
            document           TYPE REF TO cl_document_bcs, "doc
            sender             TYPE REF TO cl_sapuser_bcs, "sender
            recipient          TYPE REF TO if_recipient_bcs, "recipient
            bcs_exception      TYPE REF TO cx_bcs,
            sent_to_all        TYPE os_boolean.

      FIELD-SYMBOLS <lfs_email> TYPE LINE OF gy_t_email. "for email recipients


    *Convert string to binary table
      TRY.

          cl_bcs_convert=>string_to_solix(
            EXPORTING
              iv_string   = pv_string "final string
              iv_codepage = '4103'  "suitable for MS Excel, leave empty
              iv_add_bom  = 'X'     "for other doc types
            IMPORTING
              et_solix  = lt_attach ).

        CATCH cx_bcs.

          MESSAGE e445(so).
      ENDTRY.

    *Create the email
      TRY.
    *Create persistent send request
        send_request = cl_bcs=>create_persistent( ).

    *Subject
        lv_subject = 'Weekly Report'.

    *Body of email
        APPEND 'Please find the attached weekly sales report'(090) TO lt_text.

    *Create Document
        document = cl_document_bcs=>create_document(
                        i_type    = 'RAW'
                        i_text    = lt_text
                        i_subject = lv_subject ).

    *Add attachment to document
        CALL METHOD document->add_attachment
          EXPORTING
            i_attachment_type    = 'XLS'
            i_attachment_subject = lv_subject
            i_att_content_hex    = lt_attach.

    *Add document to send request
        CALL METHOD send_request->set_document( document ).

    *Set sender
        sender = cl_sapuser_bcs=>create( sy-uname ).
        CALL METHOD send_request->set_sender
          EXPORTING
            i_sender = sender.

    *Add recipient (e-mail address) from table
        LOOP AT pt_email ASSIGNING <lfs_email>.
          recipient = cl_cam_address_bcs=>create_internet_address(
                                            <lfs_email>-low ).

    *Add recipient with its respective attributes to send request
          CALL METHOD send_request->add_recipient
            EXPORTING
              i_recipient = recipient
              i_express   = 'X'.
        ENDLOOP.

    *Send document
        CALL METHOD send_request->send(
          EXPORTING
            i_with_error_screen = 'X'
          RECEIVING
            result              = sent_to_all ).
        IF sent_to_all = 'X'.
          MESSAGE s000 WITH 'Mail has been Successfully Sent.'(085).
        ENDIF.

        COMMIT WORK.
      ENDTRY.