Skip to Content
avatar image
-2
Former Member

Custom column names in CL_SALV_TABLE

Hi:

I have one transparent table.

I made a query to retrive data from this table

I use CL_SALV_TABLE to show the results

I change the custom column name

It works.

This is the code:

REPORT  Z_AG_VALIDAR_POLIZA.
* declara una estructura
TYPES: BEGIN OF structure_molde_ordenes,
  MSEHL TYPE MSEHL,
  MSEHI   TYPE MSEHI,
  SPRAS   TYPE SPRAS,
END OF  structure_molde_ordenes.
* define una estructura
DATA:
  mi_estructura TYPE  structure_molde_ordenes,
  "crea la estructura real
  mi_tabla TYPE STANDARD TABLE OF structure_molde_ordenes,
  " crea una internal table  a partir de la estructura
  go_alv TYPE REF TO CL_SALV_TABLE,
  " crea el objeto de la clase CL_SALV_TABLE
  columns TYPE REF TO cl_salv_columns_table,
  " crea el objeto de la clase cl_salv_columns_table
  column  TYPE REF TO cl_salv_column.
  " crea el objeto de la clase cl_salv_column
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" tras declarar variables comienza la fiesta
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
START-OF-SELECTION.
 PERFORM fx_obtener_datos.
  " hace el query
  PERFORM initialize_alv.
  PERFORM display_alv.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" function para hacer el query
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM fx_obtener_datos.
 SELECT
  " recupera los  recibos para un cliente
  MSEHL
MSEHI
SPRAS
FROM
  T006A
INTO  CORRESPONDING FIELDS OF TABLE
  mi_tabla.
 ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" function para inicia la clase para presentar el reporte
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM initialize_alv.
  DATA message TYPE REF TO cx_salv_msg.
  "crea el objeto de la clase cx_salv_msg
  TRY.
    cl_salv_table=>factory(
    IMPORTING
      r_salv_table = go_alv
    CHANGING
      t_table      = mi_tabla ).
    columns = go_alv->get_columns( ).
    "ejecuta el metodo de la clase
    PERFORM enable_layout_settings.
    PERFORM optimize_column_width.
    PERFORM change_column_name.
    CATCH cx_salv_msg INTO message.
    " error handling
  ENDTRY.
ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" muestra el alv
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM display_alv.
  go_alv->display( ).
ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" layout setting
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM enable_layout_settings.
  DATA layout_settings TYPE REF TO cl_salv_layout.
  DATA layout_key      TYPE salv_s_layout_key.
  layout_settings = go_alv->get_layout( ).
  layout_key-report = sy-repid.
  layout_settings->set_key( layout_key ).
  layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" optimize_column_width
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM optimize_column_width.
  columns->set_optimize( ).
ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" change column name
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM change_column_name.
" cambia el nombre a una columna...
  DATA not_found TYPE REF TO cx_salv_not_found.
  TRY.
      column = columns->get_column( 'MSEHL' ).
      column->set_short_text( 'Desc' ).
      column->set_medium_text( 'Descripcion' ).
      column->set_long_text( 'Descripcion' ).
column = columns->get_column( 'MSEHI' ).
      column->set_short_text( 'Unidad' ).
      column->set_medium_text( 'Unidad' ).
      column->set_long_text( 'Unidad' ).
column = columns->get_column( 'SPRAS' ).
      column->set_short_text( 'Idioma' ).
      column->set_medium_text( 'Idioma' ).
      column->set_long_text( 'Idioma' ).
    CATCH cx_salv_not_found INTO not_found.
      " error handling
  ENDTRY.
ENDFORM.

But if I

* change the name of the field in the structure

* change the name in the internal table,

* use AS in the query

it stops to work....

I can not change COLUMN NAME in ALV

This is my code


REPORT  Z_AG_VALIDAR_POLIZA.
* declara una estructura
TYPES: BEGIN OF structure_molde_ordenes,
  mi_desc TYPE MSEHL,
  mi_uni   TYPE MSEHI,
  mi_idio   TYPE SPRAS,
END OF  structure_molde_ordenes.
* define una estructura
DATA:
  mi_estructura TYPE  structure_molde_ordenes,
  "crea la estructura real
  mi_tabla TYPE STANDARD TABLE OF structure_molde_ordenes,
  " crea una internal table  a partir de la estructura
  go_alv TYPE REF TO CL_SALV_TABLE,
  " crea el objeto de la clase CL_SALV_TABLE
  columns TYPE REF TO cl_salv_columns_table,
  " crea el objeto de la clase cl_salv_columns_table
  column  TYPE REF TO cl_salv_column.
  " crea el objeto de la clase cl_salv_column
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" tras declarar variables comienza la fiesta
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
START-OF-SELECTION.
 PERFORM fx_obtener_datos.
  " hace el query
  PERFORM initialize_alv.
  PERFORM display_alv.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" function para hacer el query
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM fx_obtener_datos.
 SELECT
  " recupera los  recibos para un cliente
  MSEHL AS mi_desc
MSEHI AS mi_uni
SPRAS AS mi_idio
FROM
  T006A
INTO  CORRESPONDING FIELDS OF TABLE
  mi_tabla.
 ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" function para inicia la clase para presentar el reporte
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM initialize_alv.
  DATA message TYPE REF TO cx_salv_msg.
  "crea el objeto de la clase cx_salv_msg
  TRY.
    cl_salv_table=>factory(
    IMPORTING
      r_salv_table = go_alv
    CHANGING
      t_table      = mi_tabla ).
    columns = go_alv->get_columns( ).
    "ejecuta el metodo de la clase
    PERFORM enable_layout_settings.
    PERFORM optimize_column_width.
    PERFORM change_column_name.
    CATCH cx_salv_msg INTO message.
    " error handling
  ENDTRY.
ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" muestra el alv
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM display_alv.
  go_alv->display( ).
ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" layout setting
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM enable_layout_settings.
  DATA layout_settings TYPE REF TO cl_salv_layout.
  DATA layout_key      TYPE salv_s_layout_key.
  layout_settings = go_alv->get_layout( ).
  layout_key-report = sy-repid.
  layout_settings->set_key( layout_key ).
  layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" optimize_column_width
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM optimize_column_width.
  columns->set_optimize( ).
ENDFORM.
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
" change column name
" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FORM change_column_name.
" cambia el nombre a una columna...
  DATA not_found TYPE REF TO cx_salv_not_found.
  TRY.
      column = columns->get_column( 'MSEHL' ).
      " DOES NOT WORK
      column->set_short_text( 'Desc' ).
      column->set_medium_text( 'Descripcion' ).
      column->set_long_text( 'Descripcion' ).
            column = columns->get_column( 'mi_uni' ).
            "DOES NOT WORK
      column->set_short_text( 'Unidad' ).
      column->set_medium_text( 'Unidad' ).
      column->set_long_text( 'Unidad' ).
            column = columns->get_column( 'SPRAS' ).
      column->set_short_text( 'Idioma' ).
      column->set_medium_text( 'Idioma' ).
      column->set_long_text( 'Idioma' ).
    CATCH cx_salv_not_found INTO not_found.
      " error handling
  ENDTRY.
ENDFORM.

How can I

* change the name of the field in the structure

* change the name in the internal table,

* use AS in the query

AND

CHANGE COLUMN NAME in ALV

Thanks in advance!


Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Jun 01, 2017 at 11:03 PM

    Column names are in caps, get_column( ) is case sensitive.

    ==> Change the name to upper case and it should work.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 02, 2017 at 03:00 PM
    -1

    Weird...

    But it works.

    I declare my variables with lower case

    But sap put the column name in UPPER CASE

    Thank you

    This code works:

    REPORT  Z_AG_VALIDAR_POLIZA.
    * declara una estructura
    TYPES: BEGIN OF structure_molde_ordenes,
      mi_desc type msehl,
      mi_uni   type msehi,
      mi_idio   type spras,
    END OF  structure_molde_ordenes.
    * define una estructura
    DATA:
      mi_estructura TYPE  structure_molde_ordenes,
      "crea la estructura real
      mi_tabla TYPE STANDARD TABLE OF structure_molde_ordenes,
      " crea una internal table  a partir de la estructura
      go_alv TYPE REF TO CL_SALV_TABLE,
      " crea el objeto de la clase CL_SALV_TABLE
      columns TYPE REF TO cl_salv_columns_table,
      " crea el objeto de la clase cl_salv_columns_table
      column  TYPE REF TO cl_salv_column.
      " crea el objeto de la clase cl_salv_column
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    " tras declarar variables comienza la fiesta
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    START-OF-SELECTION.
     PERFORM fx_obtener_datos.
      " hace el query
      PERFORM initialize_alv.
      PERFORM display_alv.
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    " function para hacer el query
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    FORM fx_obtener_datos.
     SELECT
      " recupera los  recibos para un cliente
      msehl as mi_desc
        msehi as mi_uni
        spras as mi_idio
    FROM
      T006A
    INTO  CORRESPONDING FIELDS OF TABLE
      mi_tabla.
     ENDFORM.
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    " function para inicia la clase para presentar el reporte
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    FORM initialize_alv.
      DATA message TYPE REF TO cx_salv_msg.
      "crea el objeto de la clase cx_salv_msg
      TRY.
        cl_salv_table=>factory(
        IMPORTING
          r_salv_table = go_alv
        CHANGING
          t_table      = mi_tabla ).
        columns = go_alv->get_columns( ).
        "ejecuta el metodo de la clase
        PERFORM enable_layout_settings.
        PERFORM optimize_column_width.
        PERFORM change_column_name.
        CATCH cx_salv_msg INTO message.
        " error handling
      ENDTRY.
    ENDFORM.
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    " muestra el alv
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    FORM display_alv.
      go_alv->display( ).
    ENDFORM.
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    " layout setting
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    FORM enable_layout_settings.
      DATA layout_settings TYPE REF TO cl_salv_layout.
      DATA layout_key      TYPE salv_s_layout_key.
      layout_settings = go_alv->get_layout( ).
      layout_key-report = sy-repid.
      layout_settings->set_key( layout_key ).
      layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
    ENDFORM.
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    " optimize_column_width
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    FORM optimize_column_width.
      columns->set_optimize( ).
    ENDFORM.
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    " change column name
    " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    FORM change_column_name.
    " cambia el nombre a una columna...
      DATA not_found TYPE REF TO cx_salv_not_found.
      TRY.
          column = columns->get_column( 'MI_UNI' ).
          column->set_short_text( 'Unidad' ).
          column->set_medium_text( 'Unidad' ).
          column->set_long_text( 'Unidad' ).
          column = columns->get_column( 'MI_DESC' ).
          column->set_short_text( 'Desc' ).
          column->set_medium_text( 'Descripcion' ).
          column->set_long_text( 'Descripcion' ).
          column = columns->get_column( 'MI_IDIO' ).
          column->set_short_text( 'Idioma' ).
          column->set_medium_text( 'Idioma' ).
          column->set_long_text( 'Idioma' ).
        CATCH cx_salv_not_found INTO not_found.
          " error handling
      ENDTRY.
    ENDFORM.
    
    
    Add comment
    10|10000 characters needed characters exceeded

    • Not so weird :-)

      SAP defines column names using the internal runtime field names. This is the same as you see in the debugger at runtime, those are always in uppercase.