Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic changes of column text based on user enter.

divsmart
Participant
0 Kudos

Hi Experts,

I have an additional requirement that when user enter the date in selection screen 'column text need to changes based in dynamic'. Suppose when there give 11.2020 'column text to display as FYI years 19 to 20.

Please guidance me. I have attached my code for reference.

*&---------------------------------------------------------------------*
*& Report ZCS_R_RAWMATERIAL_STOCK
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zcs_r_rawmaterial_stock.

INCLUDE rmcs0f0m .

TABLES: mara , matdoc , t001k , t001.

SELECTION-SCREEN BEGIN OF BLOCK s1 WITH FRAME TITLE TEXT-100.

PARAMETERS:
po_bukrs TYPE t001k-bukrs OBLIGATORY DEFAULT '1000',
po_matkl TYPE mara-matkl,
po_mtart TYPE mara-mtart.


SELECTION-SCREEN SKIP 1.

SELECT-OPTIONS:
so_bwkey FOR t001k-bwkey,
so_matnr FOR mara-matnr.

SELECTION-SCREEN SKIP 1.

PARAMETERS:
po_date TYPE s012-spmon.

SELECTION-SCREEN END OF BLOCK s1.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>.

DATA: new_table TYPE REF TO data,
new_line TYPE REF TO data.

CLASS lvl_class DEFINITION.

PUBLIC SECTION.

TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr,
mtart TYPE mara-mtart,
END OF ty_mara.

TYPES: BEGIN OF ty_makt,
matnr TYPE makt-matnr,
maktx TYPE makt-maktx,
END OF ty_makt.
TYPES: BEGIN OF ty_mbew,
matnr TYPE mbew-matnr,
bwkey TYPE mbew-bwkey,
bwtar TYPE mbew-bwtar,
verpr TYPE mbew-verpr,
END OF ty_mbew.
TYPES: BEGIN OF ty_mbewh,
matnr TYPE mbewh-matnr,
bwkey TYPE mbewh-bwkey,
lfgja TYPE mbewh-lfgja,
lfmon TYPE mbewh-lfmon,
lbkum TYPE mbewh-lbkum,
salk3 TYPE mbewh-salk3,
verpr1 TYPE mbewh-verpr,
END OF ty_mbewh.
TYPES: BEGIN OF ty_matdoc,
matbf TYPE matdoc-matbf,
werks TYPE matdoc-werks,
lgort_sid TYPE matdoc-lgort_sid,
bukrs TYPE matdoc-bukrs,
dmbtr TYPE matdoc-dmbtr,
stock_qty TYPE matdoc-stock_qty,
bustm TYPE matdoc-bustm,
bwart TYPE matdoc-bwart,
xauto TYPE matdoc-xauto,
budat TYPE matdoc-budat,
cpudt TYPE matdoc-cpudt,
cputm TYPE matdoc-cputm,
yearday_budat TYPE matdoc-yearday_budat,
yearweek_budat TYPE matdoc-yearweek_budat,
yearmonth_budat TYPE matdoc-yearmonth_budat,
yearquarter_budat TYPE matdoc-yearquarter_budat,
year_budat TYPE matdoc-year_budat,
quarter_budat TYPE matdoc-quarter_budat,
month_budat TYPE matdoc-month_budat,
week_budat TYPE matdoc-week_budat,
day_budat TYPE matdoc-day_budat,
mblnr TYPE matdoc-mblnr,
ebeln TYPE matdoc-ebeln,
ebelp TYPE matdoc-ebelp,
END OF ty_matdoc.

TYPES: BEGIN OF ty_zcs_t_consqty,
bwart TYPE zcs_t_consqty-bwart,
lgort TYPE zcs_t_consqty-lgort,
sobkz TYPE zcs_t_consqty-sobkz,
xauto TYPE zcs_t_consqty-xauto,
END OF ty_zcs_t_consqty.

TYPES: BEGIN OF ty_t001,
bukrs TYPE t001-bukrs,
periv TYPE t001-periv,
END OF ty_t001.

TYPES: BEGIN OF ty_last,
matnr TYPE mara-matnr,
maktx TYPE makt-maktx,
verpr TYPE mbew-verpr,
lv_ltqty TYPE p LENGTH 10 DECIMALS 4,
lv_styqty TYPE p LENGTH 10 DECIMALS 4, "matdoc-stock_qty, "nsdm_stock_qty,
lv_qu1 TYPE p LENGTH 10 DECIMALS 4, "nsdm_stock_qty,
lv_qu2 TYPE p LENGTH 10 DECIMALS 4, "nsdm_stock_qty,
lv_qu3 TYPE p LENGTH 10 DECIMALS 4, "nsdm_stock_qty,
lv_qu4 TYPE p LENGTH 10 DECIMALS 4, "nsdm_stock_qty,
total_qty TYPE p LENGTH 10 DECIMALS 4, "nsdm_stock_qty,

END OF ty_last.

DATA: lt_mara TYPE TABLE OF ty_mara,
ls_mara TYPE ty_mara.
DATA: lt_makt TYPE TABLE OF ty_makt,
ls_makt TYPE ty_makt.
DATA: lt_mbew TYPE TABLE OF ty_mbew,
ls_mbew TYPE ty_mbew.
DATA: lt_mbewh TYPE TABLE OF ty_mbewh,
ls_mbewh TYPE ty_mbewh.
DATA: lt_matdoc TYPE TABLE OF ty_matdoc,
ls_matdoc TYPE ty_matdoc.
DATA: lt_matdoc1 TYPE TABLE OF ty_matdoc,
ls_matdoc1 TYPE ty_matdoc.
DATA: lt_matdoc2 TYPE TABLE OF ty_matdoc,
ls_matdoc2 TYPE ty_matdoc.
DATA: lt_matdoc3 TYPE TABLE OF ty_matdoc,
ls_matdoc3 TYPE ty_matdoc.
DATA: lt_zcs_t_consqty TYPE TABLE OF ty_zcs_t_consqty,
ls_zcs_t_consqty TYPE ty_zcs_t_consqty.
DATA: ls_t001 TYPE ty_t001.
DATA: lt_last TYPE TABLE OF ty_last,
ls_last TYPE ty_last.
DATA: alv_table TYPE REF TO cl_salv_table.

DATA: ls_num TYPE num VALUE '01'.
DATA: ls_num1 TYPE t009b-poper VALUE '001'.
DATA: ls_num2 TYPE t009b-poper VALUE '001'.
DATA: ls_date TYPE string.
DATA: ls_date1 TYPE dats.
DATA: ls_date2 TYPE sy-datum.
DATA: ls_date3 TYPE sy-datum.

DATA: ls_date4 TYPE sy-datum.
DATA: ls_date5 TYPE sy-datum.

DATA: ls_fisper1(3) TYPE n.
DATA: lv_styqty TYPE p LENGTH 10 DECIMALS 2.
DATA: lv_ltqty TYPE p LENGTH 10 DECIMALS 2.

DATA: ls_comp TYPE bapi0002_2-comp_code,
ls_pst TYPE bapi0002_4-posting_date,
ls_fis TYPE bapi0002_4-fiscal_year,
ls_fis1 TYPE bapi0002_4-fiscal_year,
ls_fisper TYPE bapi0002_4-fiscal_period,
lt_return TYPE bapireturn1.

METHODS: get_data,
output_data,
toolbar_data,
set_column CHANGING co_alv TYPE REF TO cl_salv_table.
* dynamic.

ENDCLASS.

CLASS lvl_class IMPLEMENTATION.

METHOD get_data.

SELECT SINGLE bukrs periv INTO ls_t001 FROM t001 WHERE
bukrs EQ po_bukrs.

SELECT matnr mtart FROM mara INTO TABLE lt_mara WHERE matnr IN so_matnr AND
mtart EQ po_mtart.

IF NOT lt_mara IS INITIAL.

SELECT matnr maktx FROM makt INTO TABLE lt_makt FOR ALL ENTRIES IN lt_mara WHERE
matnr = lt_mara-matnr.

SELECT matnr bwkey bwtar verpr FROM mbew INTO TABLE lt_mbew FOR ALL ENTRIES IN lt_mara WHERE
matnr = lt_mara-matnr AND
bwkey IN so_bwkey.


CONCATENATE po_date+0(4) po_date+4(2) ls_num INTO ls_date.

ls_date1 = ls_date.

CALL FUNCTION 'BAPI_COMPANYCODE_GET_PERIOD'
EXPORTING
companycodeid = po_bukrs
posting_date = ls_date1
IMPORTING
fiscal_year = ls_fis
fiscal_period = ls_fisper
return = lt_return.

ls_fis1 = ls_fis - 1.
ls_fisper1 = ls_fisper.

*********Consumption year calcution.

CALL FUNCTION 'FIRST_DAY_IN_PERIOD_GET'
EXPORTING
i_gjahr = ls_fis1
i_periv = ls_t001-periv
i_poper = ls_num2
IMPORTING
e_date = ls_date3.

CALL FUNCTION 'LAST_DAY_IN_PERIOD_GET'
EXPORTING
i_gjahr = ls_fis
i_periv = ls_t001-periv
i_poper = ls_fisper1
IMPORTING
e_date = ls_date2.

* *****Previous quarter

CALL FUNCTION 'FIRST_DAY_IN_PERIOD_GET'
EXPORTING
i_gjahr = ls_fis
i_periv = ls_t001-periv
i_poper = ls_num2
IMPORTING
e_date = ls_date4.




SELECT matnr bwkey lfgja lfmon lbkum salk3 verpr INTO TABLE lt_mbewh FROM mbewh FOR ALL ENTRIES IN lt_mara WHERE
matnr = lt_mara-matnr AND
bwkey IN so_bwkey AND
lfgja EQ ls_fis.
* lfgja eq ls_fis1.


SELECT matbf
werks
lgort_sid
bukrs
dmbtr
stock_qty
bustm
bwart
xauto
budat
cpudt
cputm
yearday_budat
yearweek_budat
yearmonth_budat
yearquarter_budat
year_budat
quarter_budat
month_budat
week_budat
day_budat
mblnr
ebeln
ebelp FROM matdoc INTO TABLE lt_matdoc FOR ALL ENTRIES IN lt_mara WHERE
matbf = lt_mara-matnr AND
bukrs EQ po_bukrs AND
werks IN so_bwkey AND
* bustm IN ( 'MA01' , 'MAO3' ) AND "MA01 MAO3 0 O
bwart IN ( '261' , '262' , '543' , '544' ) AND
budat BETWEEN ls_date3 AND ls_date2.


SELECT bwart lgort sobkz xauto FROM zcs_t_consqty INTO TABLE lt_zcs_t_consqty FOR ALL ENTRIES IN lt_matdoc WHERE
bwart = lt_matdoc-bwart.
ENDIF.

LOOP AT lt_matdoc INTO ls_matdoc WHERE budat BETWEEN ls_date4 AND ls_date2.
ls_matdoc1-matbf = ls_matdoc-matbf.
ls_matdoc1-stock_qty = ls_matdoc-stock_qty.
ls_matdoc1-year_budat = ls_matdoc-year_budat.
COLLECT ls_matdoc1 INTO lt_matdoc1.
CLEAR: ls_matdoc1.
ENDLOOP.

LOOP AT lt_matdoc INTO ls_matdoc WHERE budat BETWEEN ls_date3 AND ls_date4.
ls_matdoc1-matbf = ls_matdoc-matbf.
ls_matdoc1-stock_qty = ls_matdoc-stock_qty.
ls_matdoc1-year_budat = ls_matdoc-year_budat.
ls_matdoc1-yearquarter_budat = ls_matdoc-yearquarter_budat.
COLLECT ls_matdoc1 INTO lt_matdoc2.
CLEAR: ls_matdoc2.
ENDLOOP.

LOOP AT lt_matdoc INTO ls_matdoc.
READ TABLE lt_makt INTO ls_makt WITH KEY matnr = ls_matdoc-matbf.

IF sy-subrc = 0 .
ls_last-matnr = ls_matdoc-matbf.
ls_last-maktx = ls_makt-maktx.
ls_last-verpr = ls_mbew-verpr.
ENDIF.
*
LOOP AT lt_mbew INTO ls_mbew WHERE matnr = ls_matdoc-matbf.
ls_last-verpr = ls_mbew-verpr .
ENDLOOP.

LOOP AT lt_mbewh INTO ls_mbewh WHERE matnr = ls_matdoc-matbf.

ls_last-lv_ltqty = ls_mbewh-salk3 / ls_mbewh-lbkum.
ENDLOOP.


LOOP AT lt_matdoc1 INTO ls_matdoc1 WHERE matbf = ls_matdoc-matbf .
ls_last-lv_styqty = ls_last-lv_styqty + ls_matdoc1-stock_qty.
ENDLOOP.


READ TABLE lt_matdoc2 INTO ls_matdoc2 WITH KEY matbf = ls_matdoc-matbf .


DATA(lv_qua) = ls_matdoc2-yearquarter_budat+4(1).
CASE lv_qua.
WHEN '1'.
ls_last-lv_qu1 = ls_matdoc2-stock_qty.
WHEN '2'.
ls_last-lv_qu2 = ls_matdoc2-stock_qty.
WHEN '3'.
ls_last-lv_qu3 = ls_matdoc2-stock_qty.
WHEN '4'.
ls_last-lv_qu4 = ls_matdoc2-stock_qty.
ENDCASE.

ls_last-total_qty = ls_last-lv_qu1 + ls_last-lv_qu2 + ls_last-lv_qu3 + ls_last-lv_qu4.

APPEND ls_last TO lt_last.
CLEAR: ls_mara , ls_makt , ls_mbew , ls_mbewh , ls_matdoc , ls_zcs_t_consqty ,ls_matdoc1 , ls_matdoc2, ls_last.
ENDLOOP.

SORT lt_last STABLE BY matnr.

DELETE ADJACENT DUPLICATES FROM lt_last COMPARING matnr.
ENDMETHOD.

METHOD output_data.

DATA: error_message TYPE REF TO cx_salv_msg,
alv_column TYPE REF TO cl_salv_columns_table.

TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = alv_table
CHANGING
t_table = lt_last.

CATCH cx_salv_msg INTO error_message .
ENDTRY.

CALL METHOD me->set_column
CHANGING
co_alv = alv_table.

CALL METHOD toolbar_data( ).
alv_table->display( ).

* CALL METHOD me->set_top_of_page
* CHANGING
* co_alv = alv_table.

ENDMETHOD.

METHOD toolbar_data.

DATA: alv_toolbar TYPE REF TO cl_salv_functions_list.
alv_toolbar = alv_table->get_functions( ).

CALL METHOD alv_toolbar->set_all
EXPORTING
value = if_salv_c_bool_sap=>true.

ENDMETHOD.

METHOD set_column.

DATA: lo_cols TYPE REF TO cl_salv_columns.
lo_cols = alv_table->get_columns( ).

lo_cols->set_optimize( 'X' ).
DATA: lo_column TYPE REF TO cl_salv_column.

TRY .
lo_column = lo_cols->get_column( 'MATNR' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'RM CODE' ).
lo_column->set_output_length( 25 ).

lo_column = lo_cols->get_column( 'MAKTX' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'RM DESCRIPTION' ).
lo_column->set_output_length( 20 ).

lo_column = lo_cols->get_column( 'VERPR' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'CURRENT YR MOVING AVG RATE' ).
lo_column->set_output_length( 25 ).

lo_column = lo_cols->get_column( 'LV_LTQTY' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'LAST YR MOVING AVG RATE' ).
lo_column->set_output_length( 25 ).

lo_column = lo_cols->get_column( 'LV_STYQTY' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'FY 20-21 Consumption till date' ).
lo_column->set_output_length( 25 ).

lo_column = lo_cols->get_column( 'LV_QU1' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'Previous Quarter 1' ).
lo_column->set_output_length( 25 ).

lo_column = lo_cols->get_column( 'LV_QU2' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'Previous Quarter 2' ).
lo_column->set_output_length( 25 ).

lo_column = lo_cols->get_column( 'LV_QU3' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'Previous Quarter 3' ).
lo_column->set_output_length( 25 ).

lo_column = lo_cols->get_column( 'LV_QU4' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'Previous Quarter 4' ).
lo_column->set_output_length( 25 ).

lo_column = lo_cols->get_column( 'TOTAL_QTY' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'Total qty' ).
lo_column->set_output_length( 25 ).

CATCH cx_salv_not_found.
ENDTRY.

ENDMETHOD.

* METHOD dynamic.
* CALL METHOD cl_alv_table_create=>create_dynamic_table
* EXPORTING
** i_style_table =
* it_fieldcatalog = alv_table
** i_length_in_byte =
** IMPORTING
** ep_table =
** e_style_fname =
** EXCEPTIONS
** generate_subpool_dir_full = 1
** others = 2
* .
* IF sy-subrc <> 0.
** Implement suitable error handling here
* ENDIF.

* ENDMETHOD.

* METHOD set_top_of_page.
*
* DATA: lo_header TYPE REF TO cl_salv_form_layout_grid,
* lo_h_label TYPE REF TO cl_salv_form_label,
* lo_h_flow TYPE REF TO cl_salv_form_layout_flow.
*
* CREATE OBJECT lo_header.
*
* lo_h_label = lo_header->create_label( row = 1 column = 1 ).
* lo_h_label->set_text( 'Consumption report ' ).
*
* ENDMETHOD.
ENDCLASS.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR po_date.
PERFORM monat_f4.

START-OF-SELECTION.
DATA: obj TYPE REF TO lvl_class.
CREATE OBJECT obj.

CALL METHOD obj->get_data.
CALL METHOD obj->output_data.
8 REPLIES 8

divsmart
Participant

Please provide some piece of code.

vijay_hariharan
Contributor
0 Kudos

Hello,

Well instead of giving hard-coded column text in your method set_column.. you can make it dynamic based on selection screen entry.. Eg: when value is 11.2020 then CONCATENATE or use LV_text = '19' && | | && <your field text>.. and assign this to your set_long_text.. Shouldn't be too complicated.. OR if your requirement is something else.. then please specify in the original question so everyone can see the same..

Regards,
Vijay

ThorstenHoefer
Active Contributor

Hi Senthil,

check your code. You can change the column headre to the text you want.

        lo_column = lo_cols->get_column( 'LV_STYQTY' ).
lo_column->set_fixed_header_text( 'L' ).
lo_column->set_long_text( 'FY 20-21 Consumption till date' ).
lo_column->set_output_length( 25 ).
data: lv_from type n length 2,
      lv_to type n length 2.
 
lv_to = po_date+2(2).
lv_from = lv_to - 1.


lo_column->set_long_text( |FY {lv_from}-{lv_to} Consumption till date| ).

0 Kudos

Hi Hofer,

I done as you advised but not capturing dates in lv_from and lv_to. Please advice.

METHOD set_column.
DATA: lv_from TYPE n LENGTH 2,
          lv_to   TYPE n LENGTH 2.

    lv_to = po_date+2(2).
    lv_from = lv_to - 1.


    lo_cols->set_optimize( 'X' ).
    DATA: lo_column TYPE REF TO cl_salv_column.



        lo_column = lo_cols->get_column( 'LV_STYQTY' ).
        lo_column->set_fixed_header_text( 'L' ).
*        lo_column->set_long_text( 'FY 20-21 Consumption till date' ).
        lo_column->set_long_text( |FY (lv_from)-(lv_to) Consumption till date| ).
        lo_column->set_output_length( 25 ).
endmethod.

divsmart if po_date = '20211127' then lv_to = po_date+2(2) will set to "21". Please debug your code.

0 Kudos

Please replace

lo_column->set_long_text( |FY (lv_from)-(lv_to) Consumption till date| ).

with

lo_column->set_long_text( |FY { lv_from }-{ lv_to } Consumption till date| ).

divsmart
Participant

Thanks Hofer.

Working fine.

Hi,

it would be fine if you accept the answer.

Many thanks in advance

Thorsten