Thursday, 21 December 2017
Old and new ABAP syntax – overview sheet
https://blogs.sap.com/2016/03/02/old-and-new-abap-syntax-overview-sheet/
Inline Declarations
Start Values
TYPES: BEGIN OF ty_columns1, “Simple structure
cols1 TYPE i,
cols2 TYPE i,
END OF ty_columns1.
TYPES: BEGIN OF ty_columnns2, “Nested structure
coln1 TYPE i,
coln2 TYPE ty_columns1,
END OF ty_columns2.
DATA: struc_simple TYPE ty_columns1,
struc_nest TYPE ty_columns2.
struct_nest = VALUE t_struct(coln1 = 1
coln2-cols1 = 1
coln2-cols2 = 2 ).
Avoiding Invalid Values
DATA: date1 TYPE d,
date2 TYPE d,
result TYPE i.
date1 = '07092009'.
date2 = '16092009'.
result = date2 - date1.
Good Example
TRY.
DATA(result) = EXACT d( '20090916' ) - EXACT d( '20090907' ).
CATCH cx_sy_conversion_no_date.
...
ENDTRY.
TRY.
result = EXACT d( CONV string( date2 ) ) -
EXACT d( CONV string( date1 ) ).
CATCH cx_sy_conversion_no_date.
...
ENDTRY.
Using Conversion Rules
Bad Example
Anyone who is familiar with all the details of the ABAP conversion rules would probably expect an exception when the text in the following source code is assigned to the numeric text. However, only the digits of the text are respected. Therefore, the target field is given the value "00000042" instead of the value "00000007", which might also be expected.
DATA text TYPE string,
num_text TYPE n LENGTH 8.
...
text = '4 Apples + 2 Oranges'.
...
num_text = text.
Good Example
This issue is corrected in the source code below. The EXACT operator is used, which triggers an exception.
...
text = '4 Apples + 2 Oranges'.
...
TRY.
num_text = EXACT #( text ).
CATCH cx_sy_conversion_error.
...
ENDTRY.
CONV dtype|#( … )
dtype = Type you want to convert to (explicit)
# = compiler must use the context to decide the type to convert to
(implicit)
FOR Operator
FOR wa| IN itab [INDEX INTO idx] [cond]
TYPES: BEGIN OF ty_ship,
tknum TYPE tknum, “Shipment Number
name TYPE ernam, “Name of Person who Created the Object
city TYPE ort01, “Starting city
route TYPE route, “Shipment route
END OF ty_ship.
TYPES: ty_ships TYPE SORTED TABLE OF ty_ship WITH UNIQUE KEY tknum.
TYPES: ty_citys TYPE STANDARD TABLE OF ort01 WITH EMPTY KEY.
Populate internal table GT_CITYS with the cities from GT_SHIPS where the route
is R0001.
Reduction operator REDUCE
REDUCE must include at least
one FOR expression. You can use all kinds of FOR expressions in REDUCE:
with IN for iterating internal tables
with UNTIL or WHILE for conditional iterations
Conditional operators COND and SWITCH
COND dtype|#( WHEN log_exp1 THEN result1
[ WHEN log_exp2 THEN result2 ]
…
[ ELSE resultn ] ) …
SWITCH dtype|#( operand
WHEN const1 THEN result1
[ WHEN const2 THEN result2 ]
…
[ ELSE resultn ] ) …
II. Example for 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
THROW cx_cant_be( ) ).
III. Example for SWITCH
DATA(text) =
NEW class( )->meth(
SWITCH #( sy-langu
WHEN ‘D’ THEN `DE`
WHEN ‘E’ THEN `EN`
ELSE THROW cx_langu_not_supported( ) ) ).
Corresponding Operator
I. Definition
… CORRESPONDING type( [BASE ( base )] struct|itab [mapping|except] )
LOOP AT with GROUP BY
TYPES:
BEGIN OF ty_customer,
customer TYPE char10,
NAME TYPE char30,
city TYPE char30,
route TYPE char10,
END OF ty_customer.
TYPES: tt_customers TYPE SORTED TABLE OF ty_customer
WITH UNIQUE KEY customer.
TYPES: tt_citys TYPE STANDARD TABLE OF char30 WITH EMPTY KEY.
DATA(t_customres) =
VALUE tt_customers(
( customer = 'C0001' NAME = 'Test Customer 1' city = 'NY' route = 'R0001' )
( customer = 'C0002' NAME = 'Customer 2' city = 'LA' route = 'R0003' )
( customer = 'C0003' NAME = 'Good Customer 3' city = 'DFW' route = 'R0001' )
( customer = 'C0004' NAME = 'Best Customer 4' city = 'CH' route = 'R0003' )
( customer = 'C0005' NAME = 'So So Customer 5' city = 'NY' route = 'R0001' )
).
* Simply get the unique Routes, use WITHOUT MEMBERS
LOOP AT t_customres INTO DATA(ls_cust_2)
GROUP BY ( route = ls_cust_2-route )
ASCENDING
WITHOUT MEMBERS
REFERENCE INTO DATA(route_group_2).
WRITE: / route_group_2->route.
ENDLOOP.
LOOP AT t_customres INTO DATA(ls_cust1)
GROUP BY ( route = ls_cust1-route
SIZE = GROUP SIZE
INDEX = GROUP INDEX )
ASCENDING
REFERENCE INTO DATA(route_group).
TYPES:
BEGIN OF ENUM enum_mode,
mode_local,
mode_remote,
mode_export,
mode_import,
mode_delete,
mode_transport,
END OF ENUM enum_mode.
write : mode_local .
TYPES:
BEGIN OF ENUM enum_mode,
mode_local,
mode_remote,
mode_export,
mode_import,
mode_delete,
mode_transport,
END OF ENUM enum_mode.
DATA lv_default TYPE enum_mode.
WRITE lv_default .
* output by default is first one mode_local is printed
lv_default = mode_remote.
WRITE lv_default .
* now the output is mode_remote, by default it is first and after assignment , it has respective value
Sequential Method calls in single step.
DATA(lt_seltab) =
NEW cl_sccr_seltab_preparation(
io_profile = NEW cl_sccr_md_profile( if_sccr_md_profile=>profile_sap_all )
io_log = cl_sccr_testutils=>get_dummy_log( )
iv_timestamp = sy-uzeit && '0'
)->if_sccr_seltab~get_seltab( iv_filter_skipped = abap_true ).
DATA(lt_seltab_local) = new cl_sccr_seltab_generator_local( mv_select_client_independent )->if_sccr_seltab_generator~select_seltab( ).
"Filter the exits minus the already handled legacy exits in newer Class based exits
mt_legacy_exits = FILTER #( mt_legacy_exits EXCEPT IN lt_already_run_exits
WHERE ccphase = ccphase
AND repfbname = repfbname ).
Read table -vignesh
rt_exits = VALUE #( ( ccphase = 'AT' repfbname = 'CLIENTCOPY_SELECT_TEXTID_ALL' )
( ccphase = 'AT' repfbname = 'CLIENTCOPY_SELECT_TEXTID_STD' )
( ccphase = 'AT' repfbname = 'CLIENTTRA_SELECT_TEXTID_FORM' )
( ccphase = 'AT' repfbname = 'CLIENTTRA_SELECT_TEXTID_STYL' )
( ccphase = 'AT' repfbname = 'CLIENTCOPY_SELECT_TEXTTAB' )
( ccphase = 'NT' repfbname = 'CLIENTCOPY_REMOTE_SAPSCRIPT' )
( ccphase = 'NT' repfbname = 'CLIENTCOPY_COPY_TEXT' )
) .
et_run_methods = VALUE #( ( if_sccr_exit=>method_init_and_check_context )
( if_sccr_exit=>method_phase2_analysis )
( if_sccr_exit=>method_phase1_edit_seltab )
( if_sccr_exit=>method_phase3_postprocessing ) ).
Read about enum
TYPES:
BEGIN OF ENUM enum_mode,
mode_local,
mode_remote,
mode_export,
mode_import,
mode_delete,
mode_transport,
END OF ENUM enum_mode.
************** GROUP BY *************************
LOOP AT lt_vcl_shared_namtab[] ASSIGNING FIELD-SYMBOL()
GROUP BY ( bastabname = -bastabname
bastabfld = -bastabfld
size = GROUP SIZE
index = GROUP INDEX
) ASSIGNING FIELD-SYMBOL().
* Table field has been shared between two or more views
CHECK -size GT 1.
* Populate the found members of the grouped entries
lt_nametab[] = VALUE #( FOR ls_namtab IN GROUP ( ls_namtab ) ).
************** SWITCH ****************************
* Populate the viewcluster subobject details
mt_vcl_all_records[] = VALUE #( BASE mt_vcl_all_records ( objectname = -object
objecttype = SWITCH #( mt_vcl_cluster_header[ -objpos ]-header[ 1 ]-bastab
WHEN abap_true THEN mc_objecttype-table " S
ELSE mc_objecttype-view ) " V
record_tab = lo_data ) ).
Thanks & Regards,
Rakshith Gore
Mesh Program
*&---------------------------------------------------------------------*
*& Report YBTEST_MESH
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT YBTEST_MESH.
TYPES:
BEGIN OF ty_customer,
customer TYPE char10,
NAME TYPE char30,
city TYPE char30,
route TYPE char30,
END OF ty_customer.
TYPES: tt_customers TYPE SORTED TABLE OF ty_customer
WITH UNIQUE KEY customer.
TYPES:
BEGIN OF ty_orders,
order TYPE char10,
customer TYPE char10,
END OF ty_orders.
TYPES: tt_orders TYPE SORTED TABLE OF ty_orders
WITH UNIQUE KEY order.
TYPES:
BEGIN OF MESH ty_rep_mesh,
order_info TYPE tt_orders
ASSOCIATION order_to_customer TO customer_info
ON customer = customer,
customer_info TYPE tt_customers,
END OF MESH ty_rep_mesh.
DATA(t_customres) =
VALUE tt_customers(
( customer = 'C0001' NAME = 'Test Customer 1' city = 'NY' route = 'R0001' )
( customer = 'C0002' NAME = 'Customer 2' city = 'LA' route = 'R0003' )
( customer = 'C0003' NAME = 'Good Customer 3' city = 'DFW' route = 'R0001' )
( customer = 'C0004' NAME = 'Best Customer 4' city = 'CH' route = 'R0003' )
).
DATA(t_orders) =
VALUE tt_orders(
( order = '90001' customer = 'C0001' )
( order = '90002' customer = 'C0002' )
( order = '90003' customer = 'C0001' )
( order = '90004' customer = 'C0001' )
( order = '90005' customer = 'C0004' )
( order = '90006' customer = 'C0004' )
( order = '90007' customer = 'C0003' )
).
DATA t_mesh TYPE ty_rep_mesh.
t_mesh-order_info = t_orders.
t_mesh-customer_info = t_customres.
LOOP AT t_mesh-order_info INTO DATA(ls_order).
WRITE: / ls_order-order.
DATA(ls_cust) = t_mesh-order_info\order_to_customer[ t_mesh-order_info[ order = ls_order-order ] ].
WRITE: ` `, ls_cust-customer, ls_cust-NAME.
ENDLOOP.
INSERT (iv_tabname) USING CLIENT @iv_target_client
FROM ( SELECT * FROM (iv_tabname) USING CLIENT @iv_source_client WHERE (lv_where_statement) ).
Subscribe to:
Posts (Atom)