Generation of ABAP report in runtime

本小妞迷上赌 提交于 2019-12-13 07:42:43

问题


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


回答1:


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 ?




回答2:


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] }.



回答3:


You should consider using SAPQuery. SAP documentation here: https://help.sap.com/saphelp_erp60_sp/helpdata/en/d2/cb3efb455611d189710000e8322d00/content.htm



来源:https://stackoverflow.com/questions/35053253/generation-of-abap-report-in-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!