How to get Internal Table Components at runtime

This code can be helpful to get fields names of an Internal Table at Runtime.

The Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
*&---------------------------------------------------------------------*
*& Report ZTEST_INTERNAL_TAB_COMP
*&
*&---------------------------------------------------------------------*
*&***********DEMO Code for http://abap-explorer.blogspot.com/***********
*&
*&---------------------------------------------------------------------*

report ztest_internal_tab_comp line-size 100.
type-pools: abap.

*Declare the type of the internal table
types: begin of x_final,
matnr type matnr,
werks type werks_d,
flag type c length 1,
value type p length 10 decimals 2,
end of x_final.

data:
*The Internal table whose components are to found
i_data type sorted table of x_final with unique key matnr,
*Table to hold the components
tab_return type abap_compdescr_tab,
*Work area for the component table
components like line of tab_return.

*Call Perform to get the Int. Table Components
perform get_int_table_fields using i_data
changing tab_return.
*Display Components
loop at tab_return into components.
write: / components-name, components-type_kind,
components-length,components-decimals.
endloop.




*&---------------------------------------------------------------------*
*& Form get_int_table_fields
*&---------------------------------------------------------------------*
* Get the Components of an internal table
*----------------------------------------------------------------------*
* -->T_DATA text
* -->T_RETURN text
*----------------------------------------------------------------------*
form get_int_table_fields using t_data type any table
changing t_return type abap_compdescr_tab.

data:
oref_table type ref to cl_abap_tabledescr,
oref_struc type ref to cl_abap_structdescr,
oref_error type ref to cx_root,
text type string.
*Get the description of data object type
try.
oref_table ?=
cl_abap_tabledescr=>describe_by_data( t_data ).
catch cx_root into oref_error.
text = oref_error->get_text( ).
write: / text.
exit.
endtry.
*Get the line type
try.
oref_struc ?= oref_table->get_table_line_type( ).
catch cx_root into oref_error.
text = oref_error->get_text( ).
write: / text.
exit.
endtry.

append lines of oref_struc->components to t_return.

endform. " GET_INT_TABLE_FIELDS


Now if we test it...


The code can also be used to get components of a structure.

User Defined Conversion Exits

This post will show how to create custom user defined Conversion-Exits.

Step 1) Create a Function Group and two Function Modules:

a) Create a Function Group. For eg. ZCONVERSION_EXIT.

b) Create two Function Modules with naming convention:

CONVERSION_EXIT_xxxxx_INPUT and CONVERSION_EXIT_xxxxx_OUTPUT, where XXXXX is the name of the Conversion Routine. Here I will create a Routine ZZERO so the
name of the Function Modules will be CONVERSION_EXIT_ZZERO_INPUT and CONVERSION_EXIT_ZZERO_OUTPUT. While create the Function Module you may get some error like "Function Module name is reserved for SAP", you can ignore this.



The Function Modules should have one Import Parameter Input and one Export Parameter Output.

The FM for conversion from External Format to Internal Format:



The Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FUNCTION conversion_exit_zzero_input.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(INPUT)
*" EXPORTING
*" REFERENCE(OUTPUT)
*"----------------------------------------------------------------------

output = input.
REPLACE ALL OCCURRENCES OF 'Ø' IN output
WITH '0' IN CHARACTER MODE.

ENDFUNCTION.


The FM for conversion from Internal Format to External Format:



The Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
FUNCTION CONVERSION_EXIT_ZZERO_OUTPUT.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(INPUT)
*" EXPORTING
*" REFERENCE(OUTPUT)
*"----------------------------------------------------------------------
output = input.
REPLACE ALL OCCURRENCES OF '0' IN output
WITH 'Ø' IN CHARACTER MODE.

ENDFUNCTION.


Step 2) Create a data Element and a Domain.

Create the Data Element ZZERO:



The Domain ZZERO:



The name of the conversion routine created has to provided here. Now if the FM's for Conversion are created, if you double click on the Routine name, it will display the FM's.



Step 3)Now we can use a sample code to test the Conversion Routine:
The Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*&---------------------------------------------------------------------*
*& Report ZTEST_CONVERSION_EXIT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ztest_conversion_exit.


PARAMETERS: p_value TYPE c LENGTH 20 OBLIGATORY.

DATA: v_result TYPE zzero. " data type with the Coversion Exit ZZERO

v_result = p_value.

WRITE: /5 'Input:',22 p_value,
/5
'Converted Value:',22 v_result.


Now test it, If we give an input as "345RGT00VDF4340".



When Executed:



The "0" has been replaced by "Ø"

Similarly the Input Conversion Routine can be used and tested.