Dynamic Programming in ABAP - Part 3 - An Example - ABAP RTTS - SAP Blogs
Dynamic Programming in ABAP - Part 3 - An Example - ABAP RTTS - SAP Blogs
Products
Products Industries
Industries Support
Support Training
Training Community
Community Developer
Developer Partner
Partner
About
About
Ask a Question Write a Blog Post Login
Former Member
ABAP Development
SAP NetWeaver Application Server for ABAP | abap | application | dynamic | example | identification | internal | on
share
0 share tweet share
Follow
Hi,
https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/ 1/8
9/27/2018 Dynamic Programming in ABAP – Part 3 – An Example – ABAP RTTS | SAP Blogs
In my last blog I explained about the significance of field symbol and data
references in dynamic programming.
https://blogs.sap.com/2017/09/05/dynamic-programming-in-abap-part-1-
introduction-to-field-symbols/
https://blogs.sap.com/2017/09/11/dynamic-programming-in-abap-part-2-
introduction-to-data-reference/
Now here we will see one example of dynamic programming approach and
also a brief introduction to ABAP RTTS.
Runtime Type Identi cation (RTTI) – Provides the methods to get the
type definition of data objects at runtime.
Runtime Type Creation (RTTC) – Provides the methods to create the
data objects at runtime with any type definition.
Basically, ABAP RTTS provides a set of classes, whose methods can be used
for runtime type identification and runtime type creation. To know more about
ABAP RTTS you can follow below link:
https://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=42965
Solution: We will build one class having a method which will take any internal
table as input and write its content in a file on application server.
Class Definition:
https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/ 2/8
9/27/2018 Dynamic Programming in ABAP – Part 3 – An Example – ABAP RTTS | SAP Blogs
Class Implementation:
* Using RTTS to get the runtime type information of the internal table
lo_type_def = cl_abap_tabledescr=>describe_by_data( it_data ).
lo_table_def ?= lo_type_def.
lo_data_def = lo_table_def->get_table_line_type( ).
lo_struct_def ?= lo_data_def.
CLEAR: lo_data_def.
https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/ 3/8
9/27/2018 Dynamic Programming in ABAP – Part 3 – An Example – ABAP RTTS | SAP Blogs
EXPORTING
i_data_element = lw_field_info-rollname
i_language = sy-langu
IMPORTING
e_scrtext_m = ls_comp_detail-descr
EXCEPTIONS
error = 1.
IF ls_comp_detail-descr IS INITIAL.
ls_comp_detail-descr = wa_components-name.
ENDIF.
APPEND ls_comp_detail TO lt_comp_detail.
CLEAR: ls_comp_detail.
ENDLOOP.
ENDIF.
https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/ 4/8
9/27/2018 Dynamic Programming in ABAP – Part 3 – An Example – ABAP RTTS | SAP Blogs
ENDIF.
ENDMETHOD.
ENDCLASS.
Here the classes CL_ABAP_*DESCR are provided by the ABAP RTTS and
used to get the type definition of data objects at runtime. Also we have
extracted the data element name of each component of line type structure of
internal table it_data using RTTS classes. Then we fetched the data element
label using the FM WCGW_DATA_ELEMENT_TEXT_GET. This label is used
to write the header for each column of internal table it_data if
WRITE_HEADER parameter of class is provided with ABAP_TRUE.
Using the Class – The above designed class can be used as:
cl_appserver_writer=>write(
EXPORTING
iv_filename = 'D:\usr\sap\testdata.csv'
it_data = lt_data
write_header = abap_true
IMPORTING
ev_message = lv_message
).
WRITE: / lv_message.
Here we are passing one internal table of structure MARA to the class, and
subsequently its content will be written on application server as comma
separated values. However, we can pass internal table of any structure. This
file can also be downloaded from application server to an excel spreadsheet.
So this is how field symbol, data reference, generic data type, RTTS helps in
dynamic programming approach.
https://github.com/rkgupta94/ABAP-Development
https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/ 5/8
9/27/2018 Dynamic Programming in ABAP – Part 3 – An Example – ABAP RTTS | SAP Blogs
Alert Moderator
4 Comments
You must be Logged on to comment or reply to a post.
Jelena Perfiljeva
Word of caution: I’m guessing one could run into a problem using CONCATENATE with
some field types.
Thanks for sharing but you might want to work on the examples a bit more, going
forward, especially if this is targeted towards the beginners. IMHO this particular
example doesn’t take full advantage of the presented functionality and if you’re not doing
that it could’ve just been much simpler (better for beginners).
“File can also be downloaded from application server to an excel spreadsheet” – could
be a bit misleading. The file can be obtained from the application server but it’ll still be a
CSV file and what one does with it is up to them. Yes, Excel can read a CSV file but it’s a
separate process. Just to be clear. (We need to mind the potential audience in the
“newbie” blogs.)
Thank you!
https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/ 6/8
9/27/2018 Dynamic Programming in ABAP – Part 3 – An Example – ABAP RTTS | SAP Blogs
Thank you Jelena for your feedback and providing a better way for structure
to CSV conversion.
Regards,
Rahul
Michelle Crapo
Rahul,
Nice change from the last blogs. Of course I agree with Jelena on the technical side.
Maybe a revision would be a good idea. But it was great to show how it was used
instead of just the definition!
Keep going – each step is getting you closer to that perfect blog!
Michelle
Regards,
Rahul
https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/ 7/8
9/27/2018 Dynamic Programming in ABAP – Part 3 – An Example – ABAP RTTS | SAP Blogs
Trademark Sitemap
Newsletter
https://blogs.sap.com/2017/09/29/dynamic-programming-in-abap-part-3-an-example-abap-rtts/ 8/8