SAP ABAP 740

From SapWiki

Ejemplos

Manejo de strings

character_string = |{ a_numeric_variable }|.
character_string = |This resulted in return code { sy-subrc }|.
LO_OBJECT->STRING2XML( |{ converted_xml }{ xml_row-row_close_tag }| ).
v_var1 = 'Esto es'.
v_var2 = 'un'.
v_var3 = 'String'.
lv_result = v_var1 && v_var2 && v_var3.
[WIDTH     = len]
[ALIGN     = LEFT|RIGHT|CENTER|(val)]
[PAD       = c]
[CASE      = RAW|UPPER|LOWER|(val)]
[SIGN      = LEFT|LEFTPLUS|LEFTSPACE|RIGHT|RIGHTPLUS|RIGHTSPACE|(val)]
[EXPONENT  = exp]
[DECIMALS  = dec]
[ZERO      = YES|NO|(val)]
[XSD       = YES|NO|(val)]
[STYLE     =  SIMPLE|SIGN_AS_POSTFIX|SCALE_PRESERVING
             |SCIENTIFIC|SCIENTIFIC_WITH_LEADING_ZERO
             |SCALE_PRESERVING_SCIENTIFIC|ENGINEERING
             |(val)]
[CURRENCY  = cur]
[NUMBER    = RAW|USER|ENVIRONMENT|(val)]
[DATE      = RAW|ISO|USER|ENVIRONMENT|(val)]
[TIME      = RAW|ISO|USER|ENVIRONMENT|(val)]
[TIMESTAMP = SPACE|ISO|USER|ENVIRONMENT|(val)]
[TIMEZONE  = tz]
[COUNTRY   = cty] ...

l_dir = |including a \{ bracket|.
l_dir = |c:\\temp|.  "c:\temp'
TRY.
  WRITE: /   |string|.
  WRITE: /   |{ my_variable }|.
  WRITE: /   |i'm a { my_variable }!|.
  WRITE: /   |i'm number { my_number * 10 }!| .
  WRITE: /   |client 000 is called: | &&
             |{ clients[ mandt = '000' ]-mtext }|.
  WRITE: /   |i'm going to be in upper case: | &&
             |{ my_variable CASE = UPPER }|.
  WRITE: /   |function and formatting:| &&
             | date raw - { my_string->date( ) DATE = RAW }| &&
             | date iso - { my_string->date( ) DATE = ISO }|.
  CATCH cx_root.
ENDTRY.

Manejo de Tablas

DATA l_pernr TYPE p0001-pernr VALUE '00003019'.

SELECT * INTO TABLE @DATA(lt_p2010) FROM pa2010 WHERE pernr = @l_pernr.

*1 line_exists
IF line_exists( lt_p2010[ pernr = l_pernr begda = '20181101' ] ).
* do somithing
  WRITE:/ 'Existe'.
ENDIF.

*2 l_index
DATA(l_index) = line_index( lt_p2010[ pernr = l_pernr begda = '20181101' ] ).
WRITE:/ l_index.


*3 Modify the contents based on TABLE-KEY
TRY.
    lt_p2010[ pernr = l_pernr ]-begda = '20200101'.
  CATCH cx_sy_itab_line_not_found.
ENDTRY.

*4
DATA(ls_p2010) = lt_p2010[ pernr = l_pernr ].
WRITE:/ ls_p2010-begda.

*5
data lt_p0002 type TABLE OF p0002.

lt_p0002 = CORRESPONDING #( lt_p2010 ).

data(ls_p0002) = lt_p0002[ 1 ].
write:/ ls_p0002-pernr.
*6 Inicializar tabla
lt_p0002 = VALUE #( ( pernr = l_pernr  begda = sy-datum ) ).
*7 agregar registros a tabla (append)
lt_p0002 = VALUE #( BASE lt_p0002 ( pernr = l_pernr  begda = '20200201' ) ).
*8 FILTER
DATA lt_p0002_s TYPE STANDARD TABLE OF p0002 WITH NON-UNIQUE SORTED KEY pernr COMPONENTS pernr.

lt_p0002_s = VALUE #( ( pernr = l_pernr     begda = '20200201' )
                      ( pernr = '00003020'  begda = '20200301' )
                   ).

DATA(lt_p0002_h) = FILTER #( lt_p0002_s USING KEY pernr WHERE pernr = l_pernr ).

* Create a filter internal table with multiple values
DATA filter_tab TYPE SORTED TABLE OF p0002-pernr
                WITH UNIQUE KEY table_line.

filter_tab = VALUE #( ( l_pernr ) ).

* Apply filters
lt_p0002_h = FILTER #( lt_p0002_s IN filter_tab
                               WHERE pernr = table_line ).

*
TYPES: BEGIN OF ty_filter,
         pernr TYPE p0002-pernr,
         begda TYPE p0002-begda,
       END OF ty_filter,
       ty_filter_tab TYPE HASHED TABLE OF ty_filter
                     WITH UNIQUE KEY pernr begda.

DATA(lt_filter_tab) = VALUE ty_filter_tab( ( pernr = l_pernr begda = '20200201' ) ).

DATA lt_p0002_3 TYPE STANDARD TABLE OF p0002.

lt_p0002_3 = VALUE #( ( pernr = l_pernr     begda = '20200201' )
                      ( pernr = '00003020'  begda = '20200301' )
                   ).

clear lt_p0002_h.

lt_p0002_h = FILTER #( lt_p0002_3 IN lt_filter_tab
                   WHERE pernr = pernr  AND begda = begda ).

VALUE

types ty_ra type RANGE OF vbak-auart.
data(ra_auart) = VALUE ty_ra(
                              ( low = 'Z1' sign = 'I' option = 'EQ' )
                              ( low = 'Z2' sign = 'I' option = 'EQ' )
                            ).
 select vbeln into table @data(LT_vbeln) from vbak where auart in @ra_auart.
DATA itab TYPE RANGE OF i. 
itab = VALUE #( sign = 'I'  option = 'BT' ( low = 1  high = 10 ) 
                                         ( low = 21 high = 30 ) 
                                         ( low = 41 high = 50 ) 
                           option = 'GE' ( low = 61 )  ).
data ra_ktart TYPE RANGE OF p2006-ktart.
CASE lw_solicitudes-subty.
  WHEN '0100'.
    ra_ktart = VALUE #( ( low = '01' sign = 'I' option = 'EQ' )
                           ( low = '02' sign = 'I' option = 'EQ' )                     ).
  WHEN '0300' OR '0301' OR '0309'.
    ra_ktart = VALUE #( ( low = '17' sign = 'I' option = 'EQ' ) ).
   WHEN '0901'.
     ra_ktart = VALUE #( ( low = '10' sign = 'I' option = 'EQ' ) ).
ENDCASE.

CONV

data: l_dir type char200.
l_dir = 'c:\temp'.
data(l_resp) =  cl_gui_frontend_services=>directory_exist( DIRECTORY = CONV #( l_dir ) ).
DATA l_date_c(8) VALUE '20200205'.
DATA(l_day) = cl_rs_time_service=>get_weekday( i_date = CONV #( l_date_c ) ).
IF l_day > 4.
  WRITE:/ 'Solo lunes a jueves'.
  RETURN.
ENDIF.  

COND

DATA(time) = COND string( WHEN sy-timlo < '120000' THEN |{ sy-timlo TIME = ISO } AM|
                          WHEN sy-timlo > '120000' THEN |{ CONV t( sy-timlo - 12 * 3600 ) TIME = ISO } PM|
                          WHEN sy-timlo = '120000' THEN |High Noon|
                          ELSE 'nada' ).

SWITCH

DATA l_flag TYPE flag.
DATA(l_status) = SWITCH #( l_flag
                             WHEN 'X' THEN 'OK'
                             ELSE 'Error'
                         ).
DATA(l_number) = SWITCH string( sy-index WHEN 1 THEN 'uno'
                                      WHEN 2 THEN 'dos'
                                      WHEN 3 THEN 'tres'
                                      else 'nada' ).

BOOLC

 p_f = boolc( p2001[] is not initial ).

NEW

 DATA g_sol TYPE REF TO cl_sol.
 CREATE OBJECT g_sol
   EXPORTING
     i_iddocumento = p_id
   EXCEPTIONS
     sol_no_existe = 1.
 IF sy-subrc <> 0.
   WRITE:/ |Sol { p_id } no existe|.
   RETURN.
 ENDIF.
 data(g_sol2) = NEW cl_sol( i_iddocumento = p_id ).

Códigos Varios

DATA(l_day) = cl_rs_time_service=>get_weekday( i_date = sy-datum ).
IF l_day > 4.
  WRITE:/ 'Solo lunes a jueves'.
  RETURN.
ENDIF.   
IF cl_rs_time_service=>get_weekday( i_date = sy-datum ) > 4.
  WRITE:/ 'Solo lunes a jueves'.
  RETURN.
ENDIF.   
********************
DATA(components) = CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_name( 'T100' ) )->components.

Demos

[1] Demos