Skip to end of metadata
Go to start of metadata

Usage of ALV for any kind of list is standard for a while. And so are the troubles with field catalog.

With CL_SALV_TABLE we do not need a field catalog any longer, but we can not (easily) switch to editable ALV. SALV and function REUSE_ALV_GRID_DISPLAY both use CL_GUI_ALV_GRID class internally, so why does SALV not need a field catalog?

The answer is quite simple: The field catalog is created at runtime. So I created this small method:

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
METHOD get_lvc_t_fcat_4_itab.
*Importing  IT_TABLE  TYPE TABLE
*Returning  VALUE( RT_FCAT )  TYPE LVC_T_FCAT
  DATA:
    lo_columns                  TYPE REF TO cl_salv_columns_table,
    lo_aggregations             TYPE REF TO cl_salv_aggregations,
    lo_salv_table               TYPE REF TO cl_salv_table,
    lr_table                    TYPE REF TO data.
  FIELD-SYMBOLS:
    <table>         TYPE STANDARD TABLE.
* create unprotected table from import table
* This is required because IMPORTed IT_TABLE can not be passed to CHANGING parameter t_table
  CREATE DATA lr_table LIKE it_table.
  ASSIGN lr_table->* TO <table>.
* New SALV Instance
  TRY.
      cl_salv_table=>factory(
        EXPORTING
          list_display = abap_false
        IMPORTING
          r_salv_table = lo_salv_table
        CHANGING
          t_table      = <table> ).
    CATCH cx_salv_msg.                                  "#EC NO_HANDLER
  ENDTRY.
* get columns object (basic field catalog data)
  lo_columns  = lo_salv_table->get_columns( ).
* get aggregations object (Sorts&Sums)
  lo_aggregations = lo_salv_table->get_aggregations( ).
* use method to create field catalog from this information
  rt_fcat =
    cl_salv_controller_metadata=>get_lvc_fieldcatalog(
      r_columns             = lo_columns
      r_aggregations        = lo_aggregations ).
ENDMETHOD.

People who can't get used to METHODs may replace METHOD/ENDMETHOD with FORM/ENDFORM.

You can manipulate the field catalog as you are used.

I observed one (minor) irregularity: If a field in the internal table is defined with data element that has direct type reference (no domain), weird errors may occur - as they do with SALV.

See below my ingenious comment about using F1 and F4 for any structures not defined in DDIC

7 Comments

  1. Hi,

    Thanks for the code... It is fantastic......

    But I have a Problem while using it. The Reference Field and Reference table are not getting filled up while using the code and hence the standard F4 help available is not popping up. Is there any way to overcome that.... I have tried but in Vain... I added them manually in the required records . I have used the standard Data Elements as references to my Internal Table

  2. Former Member

    Hi. 

    Very usefull article.

    There is one condition using CL_SALV_TABLE, , sometimes we get display ALV with no header column title, so the answer is we just add 2  logic after your coding

    at GET_LVC_FIELDCATALOG.

    1. Modifying RT_FCAT internal table as you need. in this case need modify SCRTEXT_S , SCRTEXT_M, SCRTEXT_L.

    2. Implement this Method after you modified RT_FCAT.

    CL_SALV_CONTROLLER_METADATA=>SET_LVC_FIELDCATALOG(
              T_FIELDCATALOG  = RT_FCAT
              R_COLUMNS             = LO_COLUMNS
              R_AGGREGATIONS        = LO_AGGREGATIONS ).

  3. Uhh! I am so stupid. Thought about this so long before using the obvious solution:

    After gettig the field catalog (hereold-style slis), loop at it and add reference table an field:

    ...
    
    gt_fieldcat =
        cl_salv_controller_metadata=>get_slis_fieldcatalog(
          r_columns             = lo_columns
          r_aggregations        = lo_aggregations ).
    
    ...
    
    FIELD-SYMBOLS:
        <fieldcat>     LIKE LINE OF gt_fieldcat,
        <any>          TYPE ANY,
        <rec>          TYPE ANY.
      DATA:
        lr_data        TYPE REF TO data,
        lv_help_id     TYPE string.
    LOOP AT gt_fieldcat ASSIGNING <fieldcat>.
    * gt_alv_out is the table to be displayed using i.e.FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      IF <rec> IS NOT ASSIGNED.
    * just create a line dynamically that it works even for an empty table
        CREATE DATA lr_data LIKE LINE OF gt_alv_out.
        ASSIGN lr_data->* TO <rec>.
      ENDIF.
    * get information about every field
      ASSIGN COMPONENT  <fieldcat>-fieldname OF STRUCTURE <rec> TO <any>.
      DESCRIBE FIELD <any> HELP-ID lv_help_id.
    * set reference table and fieldname - perfect F1 and F4!
    * (wonder why SAP can't do it themselves)
      SPLIT lv_help_id AT '-'
        INTO <fieldcat>-ref_tabname <fieldcat>-ref_fieldname.
    ENDLOOP.
    (Like it!)
    
  4. Hi Clemens,

    I really like your brilliant idea with taking the reference table and field with that HELP-ID extension.

    I was searching for a solution some time ago for this. I'm using a different approach to build the fieldcatalog for alv using RTTS but this was the only open issue for me, I didn't had any way to find this reference information and now my solution it's complete :)

    Thanks a lot

  5. Hi Valentin,

    thank you, I like your RTTS approach!

    Well the F4 still is not perfect via help-id: It does not work if the fields are not part of a dictionary structure: Any search-help attached to the data element, check table or fixed values at domain level are not recognized.

    I don't like to have so many code lines to make this work. And looking at the coding of standard help determination as processed on screen fields makes me really sick,

    Regards

    Clemens

  6. Former Member

    Thank you! This is extremely useful. Great code!

    Sean

  7. Former Member

    Awesome - thank you!