Showing posts with label report. Show all posts
Showing posts with label report. Show all posts

Thursday, November 23, 2017

Send Message or Email to Internal or External User

In the previous post, I showed how to send a very simple message to another logged on user in SAP, hence if the user is not logged in, he/she will not get the message. Here I will introduce another function module known as SO_NEW_DOCUMENT_SEND_API1 to send email to internal SAP user or External user as an email.

REPORT  y_evan_email.

DATA : maildata TYPE sodocchgi1.
DATA : mailtxt TYPE TABLE OF solisti1 WITH HEADER LINE. " To store message
DATA : mailrec TYPE TABLE OF somlreci1 WITH HEADER LINE. "To store receiver

START-OF-SELECTION.
  CLEAR : maildata, mailtxt, mailrec.

  REFRESH:mailtxt, mailrec.

  maildata-obj_name = 'message1'.
  maildata-obj_descr = 'This is not urgent, just Testing'. "Subject
  maildata-obj_langu = sy-langu.

  mailtxt-line = 'Hello World, I am Message 1'.
  APPEND mailtxt.

  maildata-obj_name = 'message2'.
  maildata-obj_langu = sy-langu.

  mailtxt-line = 'And this is message 2'.
  APPEND mailtxt.

"Send to external
  mailrec-receiver = 'evan.christiawan@mail.co.id'.
*  mailrec-COPY = 'X'. "Set as CC
*  mailrec-BLIND_COPY = 'X'.  "Set as BCC
  mailrec-NOTIF_READ = 'X'.
  mailrec-rec_type = 'U'. "Use if it is an external mail
  APPEND mailrec.

"Send to internal
  mailrec-receiver = 'SOL_EVAN'.
*  mailrec-COPY = 'X'. "CC
*  mailrec-BLIND_COPY = 'X'.  "BCC
*  mailrec-NO_FORWARD = 'X'.
  mailrec-no_print = 'X'.
*  mailrec-to_answer = 'X'.
  mailrec-EXPRESS = 'X'. "Show pop up notification
  mailrec-NOTIF_READ = 'X'.
  APPEND mailrec.

  CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
    EXPORTING
  document_data = maildata
  document_type = 'RAW'
  put_in_outbox = ' '
  commit_work = 'X'
*   IP_ENCRYPT                       =
*   IP_SIGN                          =
* IMPORTING
*   SENT_TO_ALL                      =
*   NEW_OBJECT_ID                    =
    TABLES
   OBJECT_HEADER                    = mailtxt
   OBJECT_CONTENT                   = mailtxt
*   CONTENTS_HEX                     =
*   OBJECT_PARA                      =
*   OBJECT_PARB                      =
      receivers                        = mailrec
   exceptions
     too_many_receivers               = 1
     document_not_sent                = 2
     document_type_not_exist          = 3
     operation_no_authorization       = 4
     parameter_error                  = 5
     x_error                          = 6
     enqueue_error                    = 7
     OTHERS                           = 8
            .
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


The following sample program will create a message based on table mailtxt and will send it to the receiver referencing table mailrec. You can change the attribute of the receiver to set whether it is a CC or BCC, etc. But i will show the main difference for the internal and external receiver which is the rec_type attribute. This attribute need to be set as 'U' to tell SAP that the receiver is external.

Here is the Sample Output from the above code:

External / Email



Internal

- Get Notification if express attribute in the mailrec is checked

- Received the message on tcode SO01





Monday, May 1, 2017

Uploading XML File to SAP ABAP Internal Table

In the previous post I have share exporting or downloading SAP ABAP Internal Table to XML File, but how about the vice versa? Hence, i will explain it as well in 3 similar simple stages:

  1. Prepare your XML File
    For this example, i will use the following XML
  2. Create Transformation:
    
    <?xml version="1.0" encoding="utf-8"?>
    <ROOT>
    <purchase_doc>
    <purchase_no>6000000025</purchase_no>
    <item_no>00010</item_no>
    <changed_on>2015-02-05</changed_on'>
    <material_no>100-100</material_no>
    <stor_loc/>
    </purchase_doc>
    <purchase_doc>
    <purchase_no>6000000026</purchase_no>
    <item_no>00010</item_no>
    <changed_on>2015-02-05</changed_on>
    <material_no>100-100</material_no>
    <stor_loc/>
    </purchase_doc>
    </ROOT>
    
    

    1. Go to tcode STRANS or XLST_TOOL
    2. Create an X XSLT Program Transformation
    3. Copy and Modify the following source code as template
    4. <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" version="1.0">
        <xsl:output encoding="UTF-8indent="nomethod="xml" version="1.0"/>
        <xsl:strip-space elements="*"/>

        <!-- custom -->
        <xsl:template match="/">
          <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
            <asx:values>
              <IEKPO>
                <xsl:apply-templates select="//<purchase_doc>"/>
              </IEKPO>
            </asx:values>
          </asx:abap>
        </xsl:template>

        <xsl:template match="<purchase_doc>">
          <item>
            <EBELN>
              <xsl:value-of select="<purchase_no>"/>
            </EBELN>
            <EBELP>
              <xsl:value-of select="<item_on>"/>
            </EBELP>
            <AEDAT>
              <xsl:value-of select="<changed_on>"/>
            </AEDAT>
            <MATNR>
              <xsl:value-of select="<material_no>"/>
            </MATNR>
            <LGORT>
              <xsl:value-of select="<stor_loc>"/>
            </LGORT>
          </item>
        </xsl:template>
        <!--End Custom-->

      </xsl:transform>

  3. Complete your Program

    1. Create a Data Type/Structure/Table Type based on your XML File data
    2. Create the internal table in your ABAP Report
      DATA: it_out TYPE STANDARD TABLE OF <YOUR_DATA_TYPE>.
            wa_out TYPE <YOUR_DATA_TYPE>.
    3. Insert, modify and perform the following FORM.
    form F_XML_IMPORT .
    * Table for the XML content
    DATA: it_xml       TYPE STANDARD TABLE OF char2048.
    
    
    DATA: it_result_xml TYPE abap_trans_resbind_tab,
          wa_result_xml TYPE abap_trans_resbind.
    
    DATA: gs_rif_ex     TYPE REF TO cx_root,
          gs_var_text   TYPE string.
    
    * Set XML File Location
    DATA p_file_name type string.
    p_file_name = "Your_XML_File_Location".
    
    * Get the XML file
    CALL METHOD cl_gui_frontend_services=>gui_upload
      EXPORTING
        filename                = p_file_name
      CHANGING
        data_tab                = it_xml
      EXCEPTIONS
        file_open_error         = 1
        file_read_error         = 2
        no_batch                = 3
        gui_refuse_filetransfer = 4
        invalid_type            = 5
        no_authority            = 6
        unknown_error           = 7
        bad_data_format         = 8
        header_not_allowed      = 9
        separator_not_allowed   = 10
        header_too_long         = 11
        unknown_dp_error        = 12
        access_denied           = 13
        dp_out_of_memory        = 14
        disk_full               = 15
        dp_timeout              = 16
        not_supported_by_gui    = 17
        error_no_gui            = 18
        OTHERS                  = 19.
    
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
    
    * Fill the result table with a reference to the data table.
    * Within the XSLT stylesheet, the data table can be accessed with
    * "IPERSON".
    
    GET REFERENCE OF it_out INTO wa_result_xml-value.
    wa_result_xml-name = 'IEKPO'.
    APPEND wa_result_xml TO it_result_xml.
    
    * Perform the XSLT stylesheet
    TRY.
        CALL TRANSFORMATION <YOUR_TRANSFORMATION_NAME>
        SOURCE XML it_xml
        RESULT (it_result_xml).
    
      CATCH cx_root INTO gs_rif_ex.
    
        gs_var_text = gs_rif_ex->get_text( ).
        MESSAGE gs_var_text TYPE 'E'.
    
    ENDTRY.
    endform.                    " F_XML_IMPORT