Showing posts with label STAGES. Show all posts
Showing posts with label STAGES. Show all posts

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-8" indent="no" method="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
    
    

Thursday, April 27, 2017

Export / Download SAP ABAP Internal Table to XML

Hello, in this tutorial I will share on of way to export or download your internal table into XML file by the help of XML Transformations. There are 3 stages in the procedure which are:

  1. Create Table Type:
    1. Go to SE11.
    2. Create a structure of your internal table
    3. Create a table type based on the structure
    4.  Make Sure to save and activate them
  2. Create Transformation:
    1. Go to tcode STRANS or XSLT_TOOL
    2. Create an S Simple Transformation
    3. Click the sparkling magic wand button a.k.a Edit Simple Transformation Graphically (Ctrl+Shift+F11)
    4. Insert New Root in Data Roots
    5. Fill in any root name of the xml for Root-Name and <Table_Type> created in first stage for Type-Name
    6. Then drag the data root into Simple Transformation
    7. You might want to change the Field name in the XML by double click the element
    8. You can make the field into an attribute.
    9. Save Activate the transformation
  3. Complete your Program:
    1. Make sure you already have the particular internal table filled with data
    2. Prepare the output location
    3. Insert, modify and perform the following FORM.
FORM f_xml_export.
  DATA: lo_xml_doc TYPE ref TO CL_XML_DOCUMENT,
        lv_string TYPE string,
        lv_file TYPE string.

  CHECK <YOUR_INTERNAL_TABLE>[] IS NOT INITIAL.

*File path
  lv_file = '<YOUR_FILE_PATH>'.

*Transform internal table to XML DOM
  CALL TRANSFORMATION <YOUR_TRANSFORMATION_NAME>
  SOURCE <YOUR_ROOT_NAME> = <YOUR_INTERNAL_TABLE[]
  RESULT XML lv_string.

CREATE OBJECT lo_xml_doc.
lo_xml_doc->parse_string( lv_string ).
* Render To XML Table
data it_xml TYPE DCXMLLINES.
CALL FUNCTION 'SDIXML_DOM_TO_XML'
  EXPORTING
    document            = lo_xml_doc->m_document
   PRETTY_PRINT        = 'X'
* IMPORTING
*   XML_AS_STRING       =
*   SIZE                =
 TABLES
   XML_AS_TABLE        = it_xml
* EXCEPTIONS
*   NO_DOCUMENT         = 1
*   OTHERS              = 2
          .
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename     = lv_file
      filetype     = 'BIN'
    TABLES
      data_tab     = it_xml
    EXCEPTIONS
      OTHERS       = 1.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM.                    " F_XML_EXPORT

Tuesday, January 31, 2017

Simple Write Report in SAP

Preparation

Although I have posted my very first Hello World post, I haven't demonstrate any tutorial yet. Therefore, i will guide you to create your very first report Program at SAP with ABAP. First of all, you will need: - SAP GUI on your computer - Authenticated SAP account to access an SAP Server If you do not have an access to SAP, I am afraid you have to create your very own SAP Server known as SAP IDES (Internet Demonstration and Evaluation System) for the purpose of training and learning by following one of two tutorials here:

Where to Start?

Once you managed to logged on SAP System, you can start go to ABAP Editor with t-code SE38 in the command field at the top left of the SAP screen.


Type your program name starting with letter Y or Z (OBLIGATORY) like Y_MY_PROGRAM, then press Create Button and type the Program title and choose Executable Program as the type. After that save and choose $TMP (Local Object) as the package unless you need to transport this program.


PRINT, I mean WRITE


Once you are in ABAP Editor, you can start create your very first program in the next ABAP Editor Window, simply write these two lines of codes:


REPORT  y_my_program.
WRITE 'Hello World!'.
Once you are done, Press Direct Processing Button (F8) And voila, your Hello World! Report Program in SAP :D On any other language, you might end your programming statement with semicolon (;), in ABAP, you use dot (.) just like a sentence. You can write more words or variable in the write sentence using semicolon (:), it is used to input multiple data in many ABAP syntax like write.

Example:
WRITE: 'This', 'is' , 'my', 'first', 'program'.
This is the same like writing the following statements


WRITE 'This'.
WRITE 'is'.
WRITE 'my'.
WRITE 'first'.
WRITE 'program'.

Next Line Please


These statement will give the same exact result like this, continuing the previous Hello World, it seems that it gives a space to every word


To enter a new line you can use slash (/) in the write syntax.

WRITE / 'This is my second line'.

However, using slash seems too redundant to have some line spacing, hence you can use
SKIP [number of lines] to skip some lines.

SKIP 2.

Stick Your Notes

Another important thing to know is commenting in your ABAP code, you can use asterisk (*) to give a full commend in a line and double quote (") to give partial commend at the end of a statement. Last tips to do a block commenting, you can make the use of ctrl + (<) to comment and ctrl + (>) to uncomment.

*Write any bullshit in the line when there is an asterisk in the front 
WRITE 'End of my Hello World Report'. "This is the end

Compilation

To give the whole idea of this tutorial, please mind to look and try yourself compiled code below.

Source Code:

REPORT  y_my_program.

WRITE 'Hello World!'.

WRITE: 'This', 'is' , 'my', 'first', 'program'.

*Next line
WRITE / 'This is my second line'.

*Another method to make new line
SKIP 2.

*Write any bullshit in the line when there is an asterisk in the front
WRITE 'End of my Hello World Report'. "This is the end

*    _    ____    _    ____
*   / \  | __ )  / \  |  _ \  Block Comment
*  / _ \ |  _ \ / _ \ | |_) | Use CTRL + <
* / ___ \| |_) / ___ \|  __/  will automatically put asterisk
*/_/   \_\____/_/   \_\_|     in the front of the block

Result:


Hello World!

Hello World! This is the first post i just published on the day i made this blog on 31th January 2017. I believe most of the readers here have been familiar with my "Hello World!" phrase which is used in your very first program with a particular new language and here I will share my knowledge in ABAP language in this blog for everyone to learn.

If you haven't heard of ABAP, it is stands for Advanced Business Application Programming, originally Allgemeiner Berichts-Aufbereitungs-Prozessor in German, a high-level programming language created by the German software company, SAP SE. Hence, it can only be used in its ERP Software's environment, SAP for customisations of feature implementation in it.

By the way, my name is Evan Christiawan, I am a graduate student from Murdoch University majoring in Computer Science and Cyber Forensics. Currently, I work as ABAP technical consultant at Soltius Indonesia, a subsidiary of Metrodata Group that focuses its business in dealing with SAP Solution to help companies run better.

I think that's all for the introduction and won't introduce more about me unless you want to know me more :). Hopefully i can keep updating posts/notes in this blog and help lots of people who are curious in learning ABAP, a language of SAP.