Difference between revisions of "SAP ABAP CLASES"

From SapWiki
Line 66: Line 66:
 
   PUBLIC SECTION.
 
   PUBLIC SECTION.
 
     METHODS: constructor IMPORTING i_iddocumento TYPE zidocumento,
 
     METHODS: constructor IMPORTING i_iddocumento TYPE zidocumento,
      aprobar EXPORTING e_subrc  TYPE sy-subrc
+
                aprobar EXPORTING e_subrc  TYPE sy-subrc
                        e_mensaje TYPE string.
+
                                  e_mensaje TYPE string.
  
 
     DATA gt_sol TYPE STANDARD TABLE OF zhr_solicitudes.
 
     DATA gt_sol TYPE STANDARD TABLE OF zhr_solicitudes.

Revision as of 18:29, 7 April 2020

CLASS-METHODS

The CLASS-METHODS statement declares a static method meth. For the name meth, the naming conventions apply.

With the class component selector (=>), static methods can be used independently of objects. In static methods, the static components of the class or its superclasses can be accessed only if the component selector is not used.

REPORT ytest_clases.
CLASS cl_gui DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS gui_download IMPORTING i_filename TYPE string
                                         i_data_tab TYPE STANDARD TABLE.

ENDCLASS.

CLASS cl_gui IMPLEMENTATION.
  METHOD gui_download.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename                = i_filename
      TABLES
        data_tab                = i_data_tab
*       FIELDNAMES              =
      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
        OTHERS                  = 22.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.

  ENDMETHOD.

ENDCLASS.

DATA gt_t001 TYPE STANDARD TABLE OF t001.

START-OF-SELECTION.

  SELECT * INTO TABLE gt_t001 FROM t001 UP TO 10 ROWS.

  cl_gui=>gui_download( EXPORTING i_filename = 'c:\temp\ytest_t001.txt'
                                  i_data_tab = gt_t001 ).

CONSTRUCTOR

REPORT ytest_solicitudes.

CLASS cl_sol DEFINITION.
  PUBLIC SECTION.
    METHODS: constructor IMPORTING i_iddocumento TYPE zidocumento,
                 aprobar EXPORTING e_subrc   TYPE sy-subrc
                                   e_mensaje TYPE string.

    DATA gt_sol TYPE STANDARD TABLE OF zhr_solicitudes.

ENDCLASS.

CLASS cl_sol IMPLEMENTATION.
  METHOD constructor.
    SELECT * INTO TABLE gt_sol FROM zhr_solicitudes WHERE iddocumento = i_iddocumento.

  ENDMETHOD.

  METHOD aprobar.
    LOOP AT gt_sol INTO DATA(ls_sol)
           WHERE subty <> '0904'.
    ENDLOOP.
    IF sy-subrc <> 0.
      e_subrc = 4.
      e_mensaje = 'Solicitud de vacaciones que desea aprobar no existe.'.
      RETURN.
    ENDIF.

*---- update Registro
    ls_sol-status_solicitud = '02'.
    UPDATE zhr_solicitudes FROM ls_sol.
    COMMIT WORK.

    e_subrc = sy-subrc.
    IF sy-subrc = 0.
      e_mensaje = 'Solicitud Aprobada con éxito'.
    ELSE.
      e_mensaje = 'Error al Aprobar solicitud'.
    ENDIF.
  ENDMETHOD.

ENDCLASS.

DATA g_sol TYPE REF TO cl_sol.

PARAMETERS p_id TYPE zidocumento.

START-OF-SELECTION.
  CREATE OBJECT g_sol EXPORTING i_iddocumento = p_id.

  g_sol->aprobar( IMPORTING e_subrc = DATA(l_subrc)
                            e_mensaje = DATA(l_mensaje)
                ).

  WRITE:/ l_mensaje.