Is there a function to provide formatted display of a deep structure with data? - abap

I have a deep structure that I would like to display as a tree with the values of each field (sort of like the hierarchical display of a structure you can do in SE11, but with values).
Is there a class or function that does this for you? I really don't want to have to go reinvent the wheel.

Well, I would say it is faster to do DIY then to search for something generic enough to help you. You can try following coding as basis.
It does the plain recursion through the variable (be it table or structure) and it prints the fields found at bottom...
*&---------------------------------------------------------------------*
*& Form print_structure
*&---------------------------------------------------------------------*
form print_structure using im_data.
data: lr_typeref type ref to cl_abap_typedescr,
lf_ddic_in type fieldname,
lt_dfies type ddfields,
lf_string type c length 200.
field-symbols: <lt_table> type any table,
<ls_table> type any,
<lf_field> type any,
<ls_dfies> like line of lt_dfies.
lr_typeref = cl_abap_typedescr=>describe_by_data( im_data ).
case lr_typeref->type_kind.
when cl_abap_typedescr=>typekind_table. " internal table
assign im_data to <lt_table>.
loop at <lt_table> assigning <ls_table>.
perform print_structure using <ls_table>.
endloop.
when cl_abap_typedescr=>typekind_struct1 or
cl_abap_typedescr=>typekind_struct2. " deep/flat structure
lf_ddic_in = lr_typeref->get_relative_name( ).
call function 'DDIF_FIELDINFO_GET'
exporting
tabname = lf_ddic_in
all_types = 'X'
tables
dfies_tab = lt_dfies
exceptions
not_found = 1
others = 0.
check sy-subrc eq 0.
loop at lt_dfies assigning <ls_dfies>.
assign component <ls_dfies>-fieldname of structure im_data to <lf_field>.
perform print_structure using <lf_field>.
endloop.
when others. " any field
write im_data to lf_string.
write: / lf_string.
endcase.
endform. "print_structure

Would an ALV Tree work? CL_SALV_TREE

I've never seen such functionality and think there is no one in standard. Can't remeber any situation in standard where such functionality should be used. In my opinion most appropriate way to implement this - to use Column Tree. Take a look into SAPCOLUMN_TREE_CONTROL_DEMO

Related

Changing IDoc segment SDATA via field-symbols?

My scenario is that I am getting an IDOC segment's data into a field symbol and change some fields based on some validations.
My code:
READ TABLE idoc_data ASSIGNING FIELD-SYMBOL(<idocdata>) with key = 'E1EDK01'
IF sy-subrc = 0.
    lcl_struc ?= cl_abap_typedescr=>describe_by_name( 'E1EDK01' ).
    CREATE DATA dref TYPE HANDLE lcl_struc.
    ASSIGN dref->* TO FIELD-SYMBOL(<sdata>).
    IF <sdata> IS ASSIGNED.
      <sdata> = <idocdata>-sdata.
....
<idocdata>-sdata = <sdata>.
    ENDIF.
ENDIF.
Though the above snippet works fine, the continuity of field symbols is broken and now I have to pass back the changed data. How do I use ASSIGN and let the field symbols take care of the changes rather than an explicit statement?
Something similar to below snippet though this won't work since <IDOC_DATA>-SDATA and <SDATA> aren't compatible.
READ TABLE idoc_data ASSIGNING FIELD-SYMBOL(<idocdata>) with key = 'E1EDK01'
IF sy-subrc = 0.
FIELD-SYMBOLS: <sdata> TYPE E1EDK01.
ASSIGN <idocdata>-sdata TO <sdata>.
....
ENDIF.
My expectation is that when I change the data in <SDATA>-FIELD1, I want the changes to flow into <IDOCDATA>-SDATA without using <idocdata>-sdata = <sdata>.
As #Sandra mentioned above, the incompatibility of field-symbols can be resolved by using CASTING while assigning them. This would make the second snippet work.
...
IF sy-subrc = 0.
FIELD-SYMBOLS: <sdata> TYPE E1EDK01.
ASSIGN <idocdata>-sdata TO <sdata> CASTING.
...
ENDIF.

Side by side structure values comparison?

How to compare structure values component-wise and display the differences?
Now I do it in a very primitive way:
DATA: tkomp TYPE komp,
tkomp2 TYPE komp.
WRITE: `Field differences: `.
DO 500 TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE tkomp TO FIELD-SYMBOL(<fld>).
IF sy-subrc = 0.
CHECK <fld> IS NOT INITIAL AND CONV string( <fld> ) CN ' 0,.'.
ENDIF.
ASSIGN COMPONENT sy-index OF STRUCTURE tkomp2 TO FIELD-SYMBOL(<fld2>).
IF sy-subrc <> 0.
EXIT.
ENDIF.
IF <fld> <> <fld2>.
WRITE: / `Component ` && sy-index && ` differs: ` && <fld>.
ENDIF.
ENDDO.
Maybe there is more beautiful way? Maybe there is something like CL_ABAP_CORRESPONDING or something newer?
I found oldie threads, where they say The Debugger uses the class CL_TPDA_TOOL_DIFF for analyzing differences, hence is my follow-ip question: is it something that we can achieve in debugger?
I've never seen an applet in ABAP debugger that allows comparing structures against each other.
Your solution is actually quite okay. You might want to add CL_ABAP_STRUCTDESCR to get the names of the components for nicer output. It would also enable you to compare and analyze the types of the component's fields as well.
Unfortunately, there is no reusable class, function, or built-in method for that.
You will find the most precise comparison implementation in the class CL_ABAP_UNIT_ASSERT, method ASSERT_EQUALS. More precisely, the local class DATA_DIFF, method DIFF_STRUCTS, shows how to compare structures in a type- and nesting-tolerant way.
I can't speak for the class CL_TPDA_TOOL_DIFF. I've heard it mentioned before but we actually do not even have it in our SAP NW 7.52 systems.
I have also never seen a debugger view or plugin that would compare structures and display the differences. No idea where that comment comes from.
There are code snippets at below, you can pass the structure in cl_abap_typedescr=>describe_by_data.
DATA: lo_struct TYPE REF TO cl_abap_structdescr,
        lt_comp   TYPE abap_component_tab,
        ls_comp   TYPE abap_componentdescr.
 CLEAR lo_struct.
  lo_struct ?= cl_abap_typedescr=>describe_by_data( p_data = lt_list_sum  ).
  REFRESH lt_comp.
  lt_comp = lo_struct->get_components( ).
  MOVE-CORRESPONDING gt_mov_grp TO gt_mov_grp_std.
  LOOP AT lt_comp INTO ls_comp.
    CLEAR lv_numeric.
    IF ls_comp-name(4) EQ cx_move.
      lv_numeric = ls_comp-name+5(2).
      READ TABLE gt_mov_grp_std REFERENCE INTO lr_mov_grp WITH KEY report_order = lv_numeric.
      IF sy-subrc EQ 0 AND
         lr_mov_grp IS BOUND.
        APPEND INITIAL LINE TO et_data REFERENCE INTO DATA(lr_data).
        MOVE-CORRESPONDING lr_mov_grp->* TO lr_data->*.
        IF <fs> IS ASSIGNED.
          UNASSIGN <fs>.
        ENDIF.
        ASSIGN COMPONENT ls_comp-name OF STRUCTURE lt_list_sum TO <fs>.
        IF sy-subrc EQ 0 AND
           <fs> IS ASSIGNED.
          lr_data->amount = <fs>.
        ENDIF.
        IF <fs> IS ASSIGNED.
          UNASSIGN <fs>.
        ENDIF.
        ASSIGN COMPONENT cx_currency OF STRUCTURE lt_list_sum TO <fs>.
        IF sy-subrc EQ 0 AND
           <fs> IS ASSIGNED.
          lr_data->currency = <fs>.
        ENDIF.
        lr_data->datum = iv_datum.
        lr_data->werks = iv_werks.
        lr_data->kunnr = iv_kunnr.
      ENDIF.
    ENDIF.
  ENDLOOP.

How to get a datatype DF16_RAW with precision 16 by using CL_ABAP_ELEMDESCR for internal table in ABAP?

I have internal type A for DF16_RAW and E for DF34_RAW and now at runtime I am creating a dynamic table for that I want datatype DF16_RAW and DF34_RAW with specified precision based on internal type. My code is like below:
CASE WA_COL-INTTYPE.
WHEN 'A'. LO_DESCR_RESULT = CL_ABAP_ELEMDESCR=>GET_DECFLOAT16( ).
WHEN 'E'. LO_DESCR_RESULT = CL_ABAP_ELEMDESCR=>GET_DECFLOAT34( ).
Here I want to get datatype with specified precision. I don't know how it be done?
Some parts of a variable are specific to the ABAP dictionary a.k.a. "DDIC" (search help, output style for the DF* types, etc.) If you want to create one variable with information specific to the ABAP dictionary, then you must refer to an element in the DDIC (i.e. a data element or table/structure component), then use:
lo_descr_result = cl_abap_typedescr=>describe_by_name( 'DDICdataelement' ).
or
lo_descr_result = cl_abap_typedescr=>describe_by_name( 'DDICtablestruct-Component' ).

Generation of ABAP report in runtime possible?

Is there any Function module that can generate ABAP code.
For eg: FM takes tables name and join conditions as input and generate ABAP code corresponding to that.
Thanks
You should consider using SAPQuery. SAP documentation here: https://help.sap.com/saphelp_erp60_sp/helpdata/en/d2/cb3efb455611d189710000e8322d00/content.htm
1. Generic reports are possible.
Your problem is, that You will have to draw a strict frame of what is
generic and what not, this means, some stuff MUST be that generic, that
it will deal with WHATEVER You want to do before ( mostly the selection ) ,
during ( mostly manipulation ---> I would offer a badi for that ), and output.
This means, that there is at least the output-step, which can be valid for ALL
data resulting from the steps before.
Consider a generic ALV-table_output, there are a lot of examples in the repo.
If You want to be the stuff printed out simple as list, this might include
more work, like, how big is the structure, when Dou You wrap a line, and so on, consider using a flag which allows to toggle the type of output .
2. Generic reports are a transportable object.
This refers to point one. Define clear stages and limits. What does the report do, and what is it not able to do. Because, even if it is in customer's namespace, each modification still will be put into transport-layers. Therefore a strict definition of features/limits is necessary so that the amount of transports due to "oh, but we also need that"-statements will not become infinite.
2. Generic reports are strict.
What does that mean ? You might want to parse the passed data ( table names, join-binding, selection-parameter-values ) and throw exceptions, if not properly set. Much work. You should offer a badi for that. If You do not do this, expect a dump. let it dump. In the end the user of Your report-api should know ( by documentation perhaps) how to call it. If not, a dynamic SQL-dump will be the result.
3. Generic reports might benefit from badis/exits.
This is self explanaining, I think. Especially generic/dynamic selection/modification/displaying of data should be extendable in terms of
custom-modifications. When You inspect, what a f4-search-help exit works like, You will understand, what I mean.
4. Generic coding is hard to debug, mostly a blackbox.
Self explaining, in the code-section below I can mark some of those sections.
5. Generic coding has some best prectice examples in the repo.
Do not reinvent the wheel. Check, how the se16n works by debugging it,
check how se11 works by debugging it. Check, what the SQL-Query-builder
looks like in the debugger. You will get the idea very soon,
and the copy-paste should be the most simple part of Your work.
6. That are the basic parts of what You might use.
Where clause determination and setting the params.
data lt_range type rsds_trange.
data ls_range_f type rsds_frange.
data lt_where type rsds_twhere.
data ls_where like line of lt_where.
ls_range_f = value #( sign = _sign
option = _option
low = _low
high = _high ).
.
.
.
append ls_frange to lt_range.
.
.
.
call function 'FREE_SELECTIONS_RANGE_2_WHERE'
exporting
field_ranges = lt_range
importing
where_clauses = lt_where.
You have the parameter, let us create the select-result-table.
data(lt_key) = value abap_keydescr_tab( for line in _joinfields)
( name = fieldname ) ).
data(lo_structdescr) = cast cl_abap_structdescr( cl_abap_structdescr=>describe_by_name( _struct_name ) ).
data(lo_tabledescr) = cl_abap_tabledescr=>create( line_type = lo_structdescr p_key = lt_key ).
create data ro_data type handle lo_tabledescr.
.
.
.
select (sel_st)
from (sel_bind)
into corresponding fields of table t_data
where (dyn_where).
Then assign the seelct-table-result-reference to the generic table of this select.
Do You need more hints ?
Yes, such possibility exists, but not by means of function modules. INSERT REPORT statement allows generating report by populating its code from internal text table:
INSERT REPORT prog FROM itab
[MAXIMUM WIDTH INTO wid]
{ [KEEPING DIRECTORY ENTRY]
| { [PROGRAM TYPE pt]
[FIXED-POINT ARITHMETIC fp]
[UNICODE ENABLING uc] }
| [DIRECTORY ENTRY dir] }.

Syntax error "field ITAB unknown" in APPEND wa TO itab

I am trying to add a new record to my internal table and this code is giving me an error, but I am doing exactly the same thing as in my SAP book. What am I doing wrong?
TYPES : BEGIN OF personel_bilgileri,
Ad TYPE c LENGTH 20,
Soyad TYPE c LENGTH 20,
Telefon_no Type n LENGTH 12,
END OF personel_bilgileri.
TYPES personel_bilgi_tablo_tipi TYPE STANDARD TABLE OF
personel_bilgileri WITH NON-UNIQUE KEY ad soyad.
DATA : personel_bilgi_kaydi TYPE personel_bilgileri,
personel_bilgi_tablosu TYPE personel_bilgi_tablo_tipi.
personel_bilgi_kaydi-ad = 'Murat'.
personel_bilgi_kaydi-soyad = 'Sahin'.
personel_bilgi_kaydi-telefon_no = '5556677'.
APPEND personel_bilgi_kaydi TO personel_bilgileri.
personel_bilgi_kaydi-ad = 'Ayse'.
personel_bilgi_kaydi-soyad = 'Bil'.
personel_bilgi_kaydi-telefon_no = '5556611'.
APPEND personel_bilgi_kaydi TO personel_bilgileri.
personel_bilgi_kaydi-ad = 'Mehmet'.
personel_bilgi_kaydi-soyad = 'Kalan'.
personel_bilgi_kaydi-telefon_no = '5556622'.
APPEND personel_bilgi_kaydi TO personel_bilgileri.
Actually, I don't know which adding record method I should use. I mean there is too many ways to do this operation. Which method will be the true one?
I am getting this error:
The field Personel_bilgileri is unknown, but there are following fields similar names...
Moreover, I can do this with LOOP AT, but I didn't understand the usage of LOOP AT. What does it do?
In your code sample, you first defined PERSONEL_BILGILERI as a TYPE, then PERSONEL_BILGI_TABLO_TIPI as a internal table TYPE of PERSONEL_BILGILERI.
Up to that point, no variables are declared yet. Only data types.
Then:
PERSONEL_BILGI_KAYDI is defined of type PERSONEL_BILGILERI. This is a structure that you use as a work area (which is fine).
PERSONEL_BILGI_TABLOSU is defined of type PERSONEL_BILGI_TABLO_TIPI. So PERSONEL_BILGI_TABLOSU is your internal table.
When you APPEND your work area, you have to append to an internal table, not a data type. try with PERSONEL_BILGI_TABLOSU instead of your type PERSONEL_BILGI:
APPEND personel_bilgi_kaydi TO personel_bilgileri_tablosu.
You need to APPEND your WA(workarea, personel_bilgi_kaydi) in to your table (personel_bilgi_tablosu). You cant append the WA to the defined type.
So it should look like this:
APPEND personel_bilgi_kaydi TO personel_bilgi_tablosu.
Also you can use this code to show them on the page.
LOOP AT personel_bilgi_tablosu into personel_bilgi_kaydi.
write: / 'İSİM: ' ,personel_bilgi_kaydi-ad,
'SOYİSİM: ',personel_bilgi_kaydi-soyad,
'TEL NO: ', personel_bilgi_kaydi-telefon_no.
ENDLOOP.
You can use other methods to show your table on the page, such as REUSE_ALV_GRID_DISPLAY. You can get more information about that in scn.sap.com
Hope it was helpful.
Kolay gelsin.
Talha