quarta-feira, 20 de dezembro de 2017

[CODIGO] ABAP para XML

Tutorial de como realizar conversão de Tabela Interna no SAP para XML.
Clique aqui caso queira ver o procedimento inverso.

Programa (SE38):
*-------------------------------------------------------------------*
*  Report Z_ABAP_TO_XML                                             *
*                                                                   *
*-------------------------------------------------------------------*
*  Write the data from an internal ABAP table into an XML document, *
*  and write it onto your frontend computer                         *
*-------------------------------------------------------------------*

REPORT z_abap_to_xml.

TYPE-POOLS: abap.

CONSTANTS gs_file TYPE string VALUE ´C:\temp\customers.xml´.
* This is the structure for the data to go into the XML file

TYPES: BEGIN OF ts_person,
  cust_id(4)    TYPE n,
  firstname(20) TYPE c,
  lastname(20)  TYPE c,
END OF ts_person.

* Table for the XML content

DATA: gt_itab        TYPE STANDARD TABLE OF char2048.

* Table and work area for the data to fill the XML file with

DATA: gt_person      TYPE STANDARD TABLE OF ts_person,
      gs_person      TYPE ts_person.

* Source table that contains references
* of the internal tables that go into the XML file

DATA: gt_source_itab TYPE abap_trans_srcbind_tab,
      gs_source_wa   TYPE abap_trans_resbind.

* For error handling

DATA: gs_rif_ex      TYPE REF TO cx_root,
      gs_var_text    TYPE string.

* Fill the internal table

gs_person-cust_id   = ´3´.
gs_person-firstname = ´Bill´.
gs_person-lastname  = ´Gates´.
APPEND gs_person TO gt_person.

gs_person-cust_id   = ´4´.
gs_person-firstname = ´Frodo´.
gs_person-lastname  = ´Baggins´.
APPEND gs_person TO gt_person.

* Fill the source table with a reference to the data table.
* Within the XSLT stylesheet, the data table can be accessed with
* "IPERSON".

GET REFERENCE OF gt_person INTO gs_source_wa-value.
gs_source_wa-name = ´IPERSON´.
APPEND gs_source_wa TO gt_source_itab.

* Perform the XSLT stylesheet

TRY.

    CALL TRANSFORMATION z_abap_to_xml
    SOURCE (gt_source_itab)
    RESULT XML gt_itab.

  CATCH cx_root INTO gs_rif_ex.

    gs_var_text = gs_rif_ex->get_text( ).
    MESSAGE gs_var_text TYPE ´E´.

ENDTRY.

* Download the XML file to your client

CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
    filename                = gs_file
  CHANGING
    data_tab                = gt_itab
  EXCEPTIONS
    file_write_error        = 1
    no_batch                = 2
    gui_refuse_filetransfer = 3
    invalid_type            = 4
    no_authority            = 5
    unknown_error           = 6
    header_not_allowed      = 7
    separator_not_allowed   = 8
    filesize_not_allowed    = 9
    header_too_long         = 10
    dp_error_create         = 11
    dp_error_send           = 12
    dp_error_write          = 13
    unknown_dp_error        = 14
    access_denied           = 15
    dp_out_of_memory        = 16
    disk_full               = 17
    dp_timeout              = 18
    file_not_found          = 19
    dataprovider_exception  = 20
    control_flush_error     = 21
    not_supported_by_gui    = 22
    error_no_gui            = 23
    OTHERS                  = 24.

IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Transformation (STRANS): <z_abap_to_xml>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <CUSTOMERS>
      <xsl:apply-templates select="//IPERSON/item"/>
    </CUSTOMERS>
  </xsl:template>

  <xsl:template match="IPERSON/item">
    <item>
      <customer_id>
        <xsl:value-of select="CUST_ID"/>
      </customer_id>
      <first_name>
        <xsl:value-of select="FIRSTNAME"/>
      </first_name>
      <last_name>
        <xsl:value-of select="LASTNAME"/>
      </last_name>
    </item>
  </xsl:template>

</xsl:transform>

XML: <customers.xml>

















Post Original

[CODIGO] XML para ABAP

Tutorial de como realizar conversão de XML para Tabela Interna no SAP.
Clique aqui caso queira ver o procedimento inverso.

XML: <customers.xml>
<?xml version="1.0" encoding="iso-8859-1" ?>
<CUSTOMERS>
  <PERSON>
    <customer_id>1</customer_id>
    <first_name>Jan</first_name>
    <last_name>Krohn</last_name>
  </PERSON>
  <PERSON>
    <customer_id>2</customer_id>
    <first_name>James</first_name>
    <last_name>Kirk</last_name>
  </PERSON>
</CUSTOMERS>

Programa (SE38):

*-------------------------------------------------------------------*
*  Report Z_XML_TO_ABAP                                             *
*                                                                   *
*-------------------------------------------------------------------*
*  Read the data from an XML file into an ABAP internal table,      *
*  and print it on the screen                                       *
*-------------------------------------------------------------------*

REPORT z_xml_to_abap.

TYPE-POOLS abap.

CONSTANTS gs_file TYPE string VALUE ´C:\temp\customers.xml´.

* This is the structure for the data from the XML file

TYPES: BEGIN OF ts_person,
         cust_id(4)    TYPE n,
         firstname(20) TYPE c,
         lastname(20)  TYPE c,
       END OF ts_person.

* Table for the XML content

DATA: gt_itab       TYPE STANDARD TABLE OF char2048.

* Table and work ares for the data from the XML file

DATA: gt_person     TYPE STANDARD TABLE OF ts_person,
      gs_person     TYPE ts_person.

* Result table that contains references
* of the internal tables to be filled

DATA: gt_result_xml TYPE abap_trans_resbind_tab,
      gs_result_xml TYPE abap_trans_resbind.

* For error handling

DATA: gs_rif_ex     TYPE REF TO cx_root,
      gs_var_text   TYPE string.

* Get the XML file from your client

CALL METHOD cl_gui_frontend_services=>gui_upload
  EXPORTING
    filename                = gs_file
  CHANGING
    data_tab                = gt_itab
  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 gt_person INTO gs_result_xml-value.
gs_result_xml-name = ´IPERSON´.
APPEND gs_result_xml TO gt_result_xml.

* Perform the XSLT stylesheet

TRY.
    CALL TRANSFORMATION z_xml_to_abap
    SOURCE XML gt_itab
    RESULT (gt_result_xml).

CATCH cx_root INTO gs_rif_ex.

    gs_var_text = gs_rif_ex->get_text( ).
    MESSAGE gs_var_text TYPE ´E´.

ENDTRY.

* Now let´s see what we got from the file

LOOP AT gt_person INTO gs_person.
  WRITE: / ´Customer ID:´, gs_person-cust_id.
  WRITE: / ´First name :´, gs_person-firstname.
  WRITE: / ´Last name  :´, gs_person-lastname.
  WRITE : /.
ENDLOOP. "gt_person.

Transformation (STRANS): <z_xml_to_abap>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="PERSON">
    <item>
      <CUST_ID>
        <xsl:value-of select="customer_id"/>
      </CUST_ID>
      <FIRSTNAME>
        <xsl:value-of select="first_name"/>
      </FIRSTNAME>
      <LASTNAME>
        <xsl:value-of select="last_name"/>
      </LASTNAME>
    </item>
  </xsl:template>

</xsl:transform>

Post Original

quinta-feira, 14 de dezembro de 2017

[CLASS] ALV usando a classe CL_SALV_TABLE

*&---------------------------------------------------------------------*
*& Report  YCHX_ALVEXAMPLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ychx_alvexample.

DATAit_usr    TYPE TABLE OF usr02,
             wa_usr TYPE usr02.

*----------------------------------------------------------------------*
*       CLASS cl_event_handler DEFINITION
*----------------------------------------------------------------------*
CLASS cl_event_handler DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS on_before_salv_function         " BEFORE_SALV_FUNCTION
      FOR EVENT if_salv_events_functions~before_salv_function
                  OF cl_salv_events_table
      IMPORTING e_salv_function.

    CLASS-METHODS on_after_salv_function          " AFTER_SALV_FUNCTION
      FOR EVENT if_salv_events_functions~before_salv_function
                  OF cl_salv_events_table
      IMPORTING e_salv_function.

    CLASS-METHODS on_added_function               " ADDED_FUNCTION
      FOR EVENT if_salv_events_functions~added_function
                  OF cl_salv_events_table
      IMPORTING e_salv_function.

    CLASS-METHODS on_top_of_page                  " TOP_OF_PAGE
      FOR EVENT if_salv_events_list~top_of_page
                  OF cl_salv_events_table
      IMPORTING r_top_of_page
                  page
                  table_index.

    CLASS-METHODS on_end_of_page                  " END_OF_PAGE
      FOR EVENT if_salv_events_list~end_of_page
                  OF cl_salv_events_table
      IMPORTING r_end_of_page
                  page.

    CLASS-METHODS on_double_click                 " DOUBLE_CLICK
      FOR EVENT if_salv_events_actions_table~double_click
                  OF cl_salv_events_table
      IMPORTING row
                  column.

    CLASS-METHODS on_link_click                   " LINK_CLICK
      FOR EVENT if_salv_events_actions_table~link_click
                  OF cl_salv_events_table
      IMPORTING row
                  column.
ENDCLASS.                    "cl_event_handler DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_event_handler IMPLEMENTATION.

  METHOD on_before_salv_function.
    BREAK-POINT.
  ENDMETHOD.                    "on_before_salv_function

  METHOD on_after_salv_function.
    BREAK-POINT.
  ENDMETHOD.                    "on_after_salv_function

  METHOD on_added_function.
    BREAK-POINT.
  ENDMETHOD.                    "on_added_function

  METHOD on_top_of_page.
    BREAK-POINT.
  ENDMETHOD.                    "on_top_of_page

  METHOD on_end_of_page.
    BREAK-POINT.
  ENDMETHOD.                    "on_end_of_page

  METHOD on_double_click.
    BREAK-POINT.
  ENDMETHOD.                    "on_double_click

  METHOD on_link_click.
    BREAK-POINT.
  ENDMETHOD.                    "on_link_click
ENDCLASS.                    "cl_event_handler IMPLEMENTATION

*&---------------------------------------------------------------------*
*&      START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

  SELECT FROM usr02 UP TO 30 ROWS
    APPENDING CORRESPONDING FIELDS OF TABLE it_usr
    ORDER BY bname.

  PERFORM display_alv.

*&---------------------------------------------------------------------*
*&      Form  display_alv
*&---------------------------------------------------------------------*
FORM display_alv.

  DATA:
    lo_table        TYPE REF TO cl_salv_table,
    lo_events      TYPE REF TO cl_salv_events_table,
    lo_columns  TYPE REF TO cl_salv_columns_table,
    lo_column    TYPE REF TO cl_salv_column_list,
    lo_functions TYPE REF TO cl_salv_functions..

  TRY.
      cl_salv_table=>factoryIMPORTING r_salv_table lo_table
                                               CHANGING  t_table      it_usr ).

      lo_events lo_table->get_event).
      SET HANDLER cl_event_handler=>on_before_salv_function FOR lo_events.
      SET HANDLER cl_event_handler=>on_after_salv_function FOR lo_events.
      SET HANDLER cl_event_handler=>on_added_function FOR lo_events.
      SET HANDLER cl_event_handler=>on_top_of_page FOR lo_events.
      SET HANDLER cl_event_handler=>on_end_of_page FOR lo_events.
      SET HANDLER cl_event_handler=>on_double_click FOR lo_events.
      SET HANDLER cl_event_handler=>on_link_click FOR lo_events.

*     ALV-Toolbar
      lo_columns lo_table->get_columns).
      lo_columns->set_optimizeabap_true ).
      lo_functions lo_table->get_functions).
      lo_functions->set_allabap_true ).
      lo_table->set_screen_status(
        pfstatus      'STANDARD_FULLSCREEN'
        report         'SAPLSLVC_FULLSCREEN'
        set_functions lo_table->c_functions_all ).

*     Set column as hotspot
      lo_columns lo_table->get_columns).
      lo_column ?= lo_columns->get_column'BNAME' ).
      lo_column->set_cell_typeif_salv_c_cell_type=>hotspot ).

      lo_table->display).

    CATCH cx_salv_msg.             " cl_salv_table=>factory()
      WRITE'cx_salv_msg exception'.
      STOP.
    CATCH cx_salv_not_found.       " cl_salv_columns_table->get_column()
      WRITE'cx_salv_not_found exception'.
      STOP.
  ENDTRY.
ENDFORM.                    "display_alv

[FORM] VERIFICAR_JOB_EXECUCAO

*&---------------------------------------------------------------------*
*&      Form  VERIFICAR_JOB_EXECUCAO
*&---------------------------------------------------------------------*
*       verifica se já tem um Job em execução
*----------------------------------------------------------------------*
FORM verificar_job_execucao .

  DATAit_jobcount TYPE TABLE OF tbtco.

  IF sy-batch EQ 'X'.

    SELECT jobname
                   jobcount
    INTO CORRESPONDING FIELDS OF TABLE it_jobcount
    FROM tbtco
    WHERE jobname <NOME_DO_JOB>
    AND   status   'R'.

    IF LINESit_jobcount gt 1.
      MESSAGE |Aguardando JOB anterior encerrar.| TYPE 'I'.
      LEAVE PROGRAM.
    ENDIF.
  ENDIF.

ENDFORM.                    " VERIFICAR_JOB_EXECUCAO