Category Archive ABAP code

Read / write file with ABAP

Read file :



  DATA : lo_error  TYPE REF TO cx_root,
         lw_error  TYPE string.

*                    ENCODING DEFAULT = ANSCII on main SAP system.
  OPEN DATASET lw_fullname FOR INPUT IN TEXT MODE ENCODING DEFAULT. 
  IF sy-subrc <> 0.
    " Manage erreur
  ENDIF.

* For each line
  DO.
*   Read line
    TRY.
          READ DATASET lw_fullname INTO lw_string.

       CATCH cx_sy_conversion_codepage INTO lo_error.  " En cas d'erreur d`encodage, 
        lw_error = lo_error->get_text( ).              " fichier en UTF-8 alors que 
        WRITE / lw_error.                              " lu en mode défaut par exemple
        EXIT.       
    ENDTRY.

    IF sy-subrc = 0.
      " Treat data
      APPEND lw_string TO li_string.
    ELSE.
      EXIT.
    ENDIF.

  ENDDO.
  
  CLOSE DATASET lw_fullname.

Assign screen variable to ABAP

Example :


  DATA : li_xvepo TYPE TABLE OF VEPOVB.

  FIELD-SYMBOLS: <fi_xvepo> TYPE ANY.

  ASSIGN ('(SAPLV51P)GT_XVEPO') TO <fi_xvepo>.
  IF <fi_xvepo> IS ASSIGNED.
    MOVE <fi_xvepo> TO li_xvepo.
  ENDIF.

 

Search tag: dynpro, structure, table, dynamique, dynamic, global data

 

Function to read Excel file

Use FM ALSM_EXCEL_TO_INTERNAL_TABLE to read Excel file

Caution, this use OLE_Objects and can’t be run in background.

Read More

Convert date to posting date

Posting date = date comptable.
Use the FM below :


  CALL FUNCTION 'FIRST_DAY_IN_PERIOD_GET'
    EXPORTING
      i_gjahr        = gv_gjhar
      i_periv        = gc_24
      i_poper        = gv_poper
    IMPORTING
      e_date         = s_fkdat-low
    EXCEPTIONS
      input_false    = 1
      t009_notfound  = 2
      t009b_notfound = 3
      OTHERS         = 4.
  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 'LAST_DAY_IN_PERIOD_GET'
    EXPORTING
      i_gjahr        = gv_gjhar
      i_periv        = gc_24
      i_poper        = gv_poper
    IMPORTING
      e_date         = s_fkdat-high
    EXCEPTIONS
      input_false    = 1
      t009_notfound  = 2
      t009b_notfound = 3
      OTHERS         = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

 

Search tag: date comptable, conversion

 

EXPORT / IMPORT internal_table TO / FROM MEMORY ID

Export :


DATA : li_table_imported TYPE TABLE OF db_dable.

EXPORT my_table FROM li_table_imported TO MEMORY ID 'MEM_TAB'.

 

Import:


DATA : li_table_exported TYPE TABLE OF db_dable.

IMPORT my_table TO li_table_exported FROM MEMORY ID 'MEM_TAB'.

 

Clear:


 FREE MEMORY ID 'MEM_TAB'.

USER EXIT for sales order (VA01 / VA02)

Use forms in this include: MV45AFZZ.

Check also includes : MV45AFZA and MV45AFZB.

Read More

WBS elements and projects

Table for :

  • WBS elements : PRPS
  • WBS projects : PROJ.

Search tag: OTP

Get Material document in error (in COGI) with ABAP

To get material document in error, check table AFFW.

Read More

Status tables in SAP

Table JEST to see active and inactive status on objects.

Table TJ02 (and text table TJ02T) for internal status.

Table TJ30 (and text table TJ30T) for external status.

 
Example to change status with BAPI
Read More

Write special characters with ABAP

Use class : CL_ABAP_CHAR_UTILITIES.

Example, write a carriage return (Carriage Return and Line Feed) :


WRITE : 'Line 1', CL_ABAP_CHAR_UTILITIES=>CR_LF, 'Line 2'.