Ir al contenido principal

OPEN DATASET - Trabajar con archivos del servidor (sy-subrc 8)

 OPEN DATASET - Trabajar con archivos del servidor

Muchas veces nos encontramos que no sabemos que pasa con un archivo en el servidor, porque el comando OPEN DATASET, nos devuelve un SY-SUBRC = 8, muy poco explicito.

No sabemos si no existe, o simplemente no tenemos permiso.

Con este código, podremos detectar lo que sucede del lado del servidor.


REPORT y_test_data_set_joel.


PARAMETERSp_rt LIKE rlgrap-filename OBLIGATORY"ruta en servidor
PARAMETERSpa1 AS CHECKBOX.           "crea y escribe archivo
PARAMETERSpa2 AS CHECKBOX.           "Levanta archivo y lo lista
PARAMETERSp_l1 LIKE rlgrap-filename"linea 1
PARAMETERSp_l2 LIKE rlgrap-filename"linea 2

CONSTANTSc_error TYPE sy-msgty VALUE 'E'.

DATAl_message   TYPE string,
      o_exception TYPE REF TO cx_root.
DATABEGIN OF rec OCCURS 0,
        campo1 TYPE rlgrap-filename,
        campo2 TYPE rlgrap-filename.
DATAEND OF rec.
DATAlinea(255TYPE c.

IF pa1 IS NOT INITIAL.

*  Cargar un par de datos de pruebas.
  rec-campo1 p_l1.
  APPEND rec.
  rec-campo1 p_l2.
  APPEND rec.

*  Abrir el archivo destino.
  OPEN DATASET p_rt FOR OUTPUT IN TEXT MODE ENCODING DEFAULT MESSAGE l_message.

*  Ver si el archivo fue creado correctamente..
  IF sy-subrc NE 0.
    FORMAT COLOR INTENSIFIED OFF.
    ULINE AT (80).
    WRITE/ sy-vline,
            ' No se pudo crear al archivo  : ',
            80 sy-vline.
    FORMAT COLOR INTENSIFIED OFF.
    WRITE/ sy-vline,
             ' 'p_rt.
*             80 sy-vline.
    WRITE' Mensaje obtenido >>>' l_message80 sy-vline.
    ULINE AT (80).
    EXIT.
  ENDIF.
*  Recorrer la tabla que contiene las informaciones.
  LOOP AT rec.
*     Cargar la linea que sera utilizada como buffer salida.
    linea  rec-campo1.
*     Carga la tabla interna con el contenido del archivo plano.
    TRANSFER linea TO p_rt.
  ENDLOOP.
*  Cerrar el archivo ASCII plano.
  CLOSE DATASET p_rt.
ENDIF.


IF pa2 IS NOT INITIAL.

  TRY.
*  Abrir el archivo destino.
      OPEN DATASET p_rt FOR INPUT IN TEXT MODE ENCODING DEFAULT
      MESSAGE l_message.
*  Ver si el archivo fue abierto correctamente..
      IF sy-subrc NE 0.
        FORMAT COLOR INTENSIFIED OFF.
        ULINE AT (80).
        WRITE/ sy-vline,
                ' No se pudo abrir el archivo  : ',
                80 sy-vline.
        FORMAT COLOR INTENSIFIED OFF.
        WRITE/ sy-vline,
                 ' 'p_rt.
*                 80 sy-vline.
        WRITE' Mensaje obtenido >>>' l_message80 sy-vline.
        ULINE AT (80).

      ELSE.
*  Recorrer el DATASET que contiene las informaciones.
        CLEAR rec.
        REFRESH rec.
        DO.
          READ DATASET p_rt INTO linea.
          IF sy-subrc NE 0.
            EXIT.
          ENDIF.
*     Cargar la tabla con los campos adecuados.
          rec-campo1 linea.
          APPEND rec.
        ENDDO.
        WRITE'Contenido de la ruta: ' p_rt .
        LOOP AT rec.
          WRITE / rec-campo1.
        ENDLOOP.
      ENDIF.
*   Error
    CATCH cx_root
      INTO o_exception.

*     Gets error message
      CALL METHOD o_exception->if_message~get_text
        RECEIVING
          result l_message.

      MESSAGE l_message
         TYPE c_error.

  ENDTRY.
*  Cerrar el archivo ASCII plano.
  CLOSE DATASET p_rt.
ENDIF.

Comentarios

Entradas más populares de este blog

A to Z of OLE Excel in ABAP 7.4

  SAP users, both business and end users always need to download the output of a report to spreadsheet and do their analytics.   The standard excel output from a report is very simple process but it is old fashioned and the spreadsheet looks quite boring.  There is no default formatting and the users have to do all the hard work of changing the fonts, coloring the texts, marking the borders etc. Updated 16th Aug 2019  –  If you are working in non-ABAP 7.4 (below), there is complete reference program for you too. Go to the end of this article.  Thank you  Legxis  ( LeonievonK ) for the share. I acknowledge, whatever I mentioned above can be achieved in many ways programmatically. We can do it in the old traditional ABAP way but  providing multiple tabs in the spreadsheet and formatting is quite tricky with non  OLE  method. OLE = Object Linking and Embedding The high level agenda of this article is to be the  G.O.A.T.  (please google it if you do not know the full form)  of OLE Excel Gu
How to add custom field in Additional B Tab for SAP Sales Order In this article we want to explain step by step how to add custom field in SAP Sales Order transaction VA01/VA02/VA03. This time Mr ABAPGurus will give the tutorial of enhancement to add custom field in SAP Sales order. In this sample we will add new custom field for comments and customer satisfaction, this data will save into separate table from SAP Standard table ( VBAK / VBAP ). SAP provided us with ADDITIONAL TAB in the sales order transaction ( VA01/VA02/VA03 ) which allow customer to add custom fields.  T he different between ADDITIONAL A and ADDITIONAL B is the ADDITIONAL A for field which already predefine in Sales Order Header ( VBAK ) fields and ADDITIONAL B is for field that freely define. This sample we will use ADDITIONAL B because we will store data from customer satisfaction into Z database table. 1. Create one Z table using SE11 SAP Transaction code. 2.Using SE38 Transaction code and ope

VOFM

Creating VOFM custom routine, functionality same as SAP standard routine 103 with Additional Functionality 1.    Introduction 1.1   Document Purpose 1.2    This is the document helps you to create custom routine for sales order for below specific requirement: Functional description: In case of sale order with the SD document category ‘K’ or ‘L’ or ‘H’ related to an invoice, we need to fill the fixed value date (VBKD_VALDT) with the billing reference date (VBRK_FKDAT). For example: Invoice 90001111, the billing date is 22.07.2013 If we create a sale order related to this invoice, we want the fixed value date equal to the billing reference date. As per the requirement: Sap standard routine ‘103’ triggers whenever sales order is being created with reference. Sap standard routine is configured in VOFM transaction as: Sap standard routine ‘103’ has the functionality of copying the billing document header data and billing document line item data into the