Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
joachimrees1
Active Contributor
You know I have a AdT template to jump start my reports, and I also like to share it, so maybe you can profit from it as well!

Latest version should be at the ende of that blog post: https://blogs.sap.com/2020/06/18/cleaning-up-my-adt-report-template-part-2-more-cleanabap/ .

As one should do, I check my template from time to time, to see if maybe some things should be changed. And yes, the did! So now it looks like this:
Types: gty_data type ${my_table} ,
gty_tab_data TYPE STANDARD TABLE OF gty_data WITH EMPTY KEY.
*solely for select-options.
data: gso_${my_table} type ${my_table}.

SELECTION-SCREEN BEGIN OF BLOCK sel_opt WITH FRAME TITLE text-t01.
SELECT-Options: so_${my_table_field} for gso_${my_table}-${my_table_field}.
SELECTION-SCREEN END OF BLOCK sel_opt.
SELECTION-SCREEN BEGIN OF BLOCK mode WITH FRAME TITLE text-t02.
PARAMETERS: pa_disp TYPE flag RADIOBUTTON GROUP mode DEFAULT 'X',
pa_proc TYPE flag RADIOBUTTON GROUP mode.
SELECTION-SCREEN END OF BLOCK mode.

CLASS lcl_report DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: init.
METHODS: get_data RETURNING VALUE(rt_data) TYPE gty_tab_data,
display_data CHANGING ct_data TYPE gty_tab_data,
process_data IMPORTING it_data TYPE gty_tab_data.
ENDCLASS.

CLASS lcl_report IMPLEMENTATION.
METHOD init.
DATA(lo_report) = NEW lcl_report( ).
DATA(lt_data) = lo_report->get_data( ).

CHECK lt_data IS NOT INITIAL.

CASE abap_true.
WHEN pa_disp.
lo_report->display_data( CHANGING ct_data = lt_data ).
WHEN pa_proc.
lo_report->process_data( EXPORTING it_data = lt_data ).
ENDCASE.
ENDMETHOD.

METHOD get_data.
select * from ${my_table} into CORRESPONDING FIELDS OF table rt_data UP TO 500 ROWS
where ${my_table_field} in so_${my_table_field}
ORDER BY PRIMARY KEY.
ENDMETHOD.

METHOD process_data.
LOOP AT it_data ASSIGNING FIELD-SYMBOL(<data>).
*do something
ENDLOOP.
ENDMETHOD.

METHOD display_data.
TRY.
cl_salv_table=>factory( IMPORTING r_salv_table = data(lr_alv)
CHANGING t_table = ct_data ).
CATCH cx_salv_msg.
ENDTRY.
lr_alv->get_layout( )->set_key( VALUE #( report = sy-repid ) ).
lr_alv->get_layout( )->set_default( abap_true ).
lr_alv->get_layout( )->set_save_restriction( if_salv_c_layout=>restrict_none ).
lr_alv->get_functions( )->set_all( abap_true ).
CALL METHOD lr_alv->display.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
lcl_report=>init( ).

Here's some explanations and steps I took:

This method looked relay clumsy (lots of lines, even though not much is happening)
METHOD init.
DATA: lt_data TYPE gty_tab_data,
lo_report TYPE REF TO lcl_report.
CREATE OBJECT lo_report.
lo_report->get_data( CHANGING ct_data = lt_data ).
CHECK lt_data IS NOT INITIAL.

CASE abap_true.
WHEN pa_disp.
lo_report->display_data( CHANGING ct_data = lt_data ).
WHEN pa_proc.
lo_report->process_data( EXPORTING it_data = lt_data ).
ENDCASE.
ENDMETHOD.

Refactoring in little steps:
* data lo_report TYPE REF TO lcl_report.
* CREATE OBJECT lo_report.
DATA(lo_report) = NEW lcl_report( ).

* DATA: lt_data TYPE gty_tab_data.
* lo_report->get_data( CHANGING ct_data = lt_data ).
*Not in this picture: the parameter of get_data has been turned into a RETURNING one to enable this:
DATA(lt_data) = lo_report->get_data( ).

 

Also not in the picture: when I tryed changing the method signature to
get_data RETURNING VALUE(rt_data) TYPE gty_tab_data,

I got an error that returning parameters my not be typed generically. Makes sense!
So I also had to change:
TYPES: gty_data     TYPE usr01,
* gty_tab_data TYPE STANDARD TABLE OF gty_data.
gty_tab_data TYPE STANDARD TABLE OF gty_data WITH EMPTY KEY.

So those changes have the init method look like this now:
  METHOD init.
DATA(lo_report) = NEW lcl_report( ).
DATA(lt_data) = lo_report->get_data( ).

CHECK lt_data IS NOT INITIAL.

CASE abap_true.
WHEN pa_disp.
lo_report->display_data( CHANGING ct_data = lt_data ).
WHEN pa_proc.
lo_report->process_data( EXPORTING it_data = lt_data ).
ENDCASE.
ENDMETHOD.

Note: I could have integrated the CHECK lt_data with the get_data ( CHECK lo_report->get_data( ) is not initial ) but I choose not to:
That line with the CHECK makes a good target for a break point.

One more thing I changed:
I added a " ORDER BY PRIMARY KEY." to the select, to save me an ATC-warning or two!

And: not that it matters much, but it surely doesn't hurt either: I changed the sequence of the methods, think it makes more sense now!

What AdT templates do you use and how do you improve them?

best
Joachim
1 Comment
Labels in this area