abap count similar row values - abap

IF ls_th_sum_lab_ndt IS not INITIAL.
ls_th_sum_lab_ndt-ndt_flag = 'X'.
ls_th_sum_lab_ndt-zwc_cat = 'NDT'.
if there are more than 2 zwc_cat that equal to NDT change the other one to MH
APPEND ls_th_sum_lab_ndt TO gt_th_sum_lab_ndt.
ENDIF.
Basically what I want to do:
Update ls_th_sum_lab_ndt-zwc_cat=MH if ls_th_sum_lab_ndt-zwc_cat="NDT" >2 set second row to MH
Thank You in Advance!

Try this:
DATA: lv_count type i,
lv_index type sy-tabix.
...
LOOP AT gt_th_sum_lab_ndt INTO ls_th_sum_lab_ndt WHERE zwc_cat = 'NDT'.
lv_count = lv_count + 1.
if lv_count >= 2.
lv_index = sy-tabix.
exit.
endif.
ENDLOOP.
CHECK lv_count >= 2.
READ TABLE gt_th_sum_lab_ndt INTO ls_th_sum_lab_ndt INDEX lv_index.
ls_th_sum_lab_ndt-zwc_cat = 'MH'.
MODIFY gt_th_sum_lab_ndt FROM ls_th_sum_lab_ndt INDEX lv_index.
Hope it helps.

Related

Displaying multiple table in same alv screen

Hello Sorry for asking the silly question since I am very new to abap programming.
I am trying to display multiple display in the same ALV screen. I have created the custom container and named properly still I am not able to get the display screen.
Can anyone please check my codes below and let me know what's the mistake I have did.
Report Z1_TEST3 NO STANDARD PAGE HEADING.
TABLES: LIKP, LIPS, VEKP, VEPO.
TYPES: BEGIN OF ty_likp,
vbeln TYPE likp-vbeln,
vkorg TYPE likp-vkorg,
vstel TYPE likp-vstel,
END OF ty_likp,
BEGIN OF ty_vekp,
vbeln TYPE lips-vbeln,
posnr TYPE lips-posnr,
matnr TYPE lips-matnr,
bpmng TYPE lips-bpmng,
vrkme TYPE lips-vrkme,
werks TYPE lips-werks,
lgort TYPE lips-lgort,
END OF ty_vekp,
BEGIN OF ty_vepo,
vbeln TYPE lips-vbeln,
xchar TYPE vepo-xchar,
vpmat TYPE lips-vpmat,
brgew TYPE lips-brgew,
ntgew TYPE lips-ntgew,
gewei TYPE lips-gewei,
END OF ty_vepo.
DATA: wa_likp TYPE ty_likp,
wa_vekp TYPE ty_vekp,
wa_vepp TYPE ty_vepo,
it_likp TYPE STANDARD TABLE OF ty_likp,
it_vekp TYPE STANDARD TABLE OF ty_vekp,
it_vepo TYPE STANDARD TABLE OF ty_vepo.
DATA: wa_fcat_likp TYPE lvc_s_fcat,
wa_fcat_vekp TYPE lvc_s_fcat,
wa_fcat_vepo TYPE lvc_s_fcat,
it_fcat_likp TYPE STANDARD TABLE OF lvc_s_fcat,
it_fcat_vekp TYPE STANDARD TABLE OF lvc_s_fcat,
it_fcat_vepo TYPE STANDARD TABLE OF lvc_s_fcat.
DATA: ob_custom1 TYPE REF TO cl_gui_custom_container,
ob_custom2 TYPE REF TO cl_gui_custom_container,
ob_custom3 TYPE REF TO cl_gui_custom_container,
ob_grid1 TYPE REF TO cl_gui_alv_grid,
ob_grid2 TYPE REF TO cl_gui_alv_grid,
ob_grid3 TYPE REF TO cl_gui_alv_grid.
INITIALIZATION.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS s_vbeln FOR likp-vbeln.
SELECTION-SCREEN END OF BLOCK b1.
CLASS delivery DEFINITION.
PUBLIC SECTION.
METHODS: get_likp,
get_vekp,
get_vepo,
fieldcat_likp,
fieldcat_vekp,
fieldcat_vepo.
ENDCLASS.
CLASS delivery IMPLEMENTATION.
METHOD get_likp.
IF s_vbeln[] IS NOT INITIAL.
SELECT vbeln vkorg vstel
FROM likp INTO TABLE it_likp
WHERE vbeln IN s_vbeln.
IF sy-subrc = 0.
SORT it_vekp BY vbeln.
ELSE.
MESSAGE 'Doesn''t exist' TYPE 'I'.
ENDIF.
ELSE.
MESSAGE 'Please select a valid number' TYPE 'I'.
ENDIF.
ENDMETHOD.
METHOD get_vekp.
IF it_vekp IS NOT INITIAL.
SELECT vbeln posnr matnr bpmng werks lgort
FROM lips INTO TABLE it_vekp
FOR ALL ENTRIES IN it_likp
WHERE vbeln = it_likp-vbeln.
IF sy-subrc = 0.
SORT it_vekp BY vbeln.
CALL METHOD: fieldcat_likp,
fieldcat_vekp,
fieldcat_vepo.
CALL SCREEN 9000.
ELSE.
MESSAGE 'Doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.
ENDMETHOD.
METHOD get_vepo.
IF it_vepo IS NOT INITIAL.
SELECT vbeln xchar vpmat brgew ntgew gewei
FROM lips INTO TABLE it_vepo
FOR ALL ENTRIES IN it_likp
WHERE vbeln = it_likp-vbeln.
IF sy-subrc = 0.
SORT it_vepo BY vbeln.
CALL METHOD: fieldcat_likp,
fieldcat_vekp,
fieldcat_vepo.
CALL SCREEN 9000.
ELSE.
MESSAGE 'Doesn''t exist' TYPE 'I'.
ENDIF.
ENDIF.
ENDMETHOD.
METHOD fieldcat_likp.
CLEAR wa_fcat_likp.
REFRESH it_fcat_likp.
DATA: lv_row TYPE i VALUE 0.
lv_row = 1 + lv_row.
wa_fcat_likp-row_pos = lv_row.
wa_fcat_likp-fieldname = 'VBELN'.
wa_fcat_likp-tabname = 'IT_LIKP'.
wa_fcat_likp-reptext = 'Lieferung'.
wa_fcat_likp-col_opt = 'X'.
APPEND wa_fcat_likp TO it_fcat_likp.
CLEAR wa_fcat_likp.
lv_row = 1 + lv_row.
wa_fcat_likp-row_pos = lv_row.
wa_fcat_likp-fieldname = 'VKORG'.
wa_fcat_likp-tabname = 'IT_LIKP'.
wa_fcat_likp-reptext = 'Verkaufsorganisation'.
wa_fcat_likp-col_opt = 'X'.
APPEND wa_fcat_likp TO it_fcat_likp.
CLEAR wa_fcat_likp.
lv_row = 1 + lv_row.
wa_fcat_likp-row_pos = lv_row.
wa_fcat_likp-fieldname = 'VSTEL'.
wa_fcat_likp-tabname = 'IT_LIKP'.
wa_fcat_likp-reptext = 'Versandstelle'.
wa_fcat_likp-col_opt = 'X'.
APPEND wa_fcat_likp TO it_fcat_likp.
CLEAR wa_fcat_likp.
ENDMETHOD.
METHOD fieldcat_vekp.
CLEAR wa_fcat_vekp.
REFRESH it_fcat_vekp.
DATA: lv_col TYPE i VALUE 0.
lv_col = 1 + lv_col.
wa_fcat_vekp-col_pos = lv_col.
wa_fcat_vekp-fieldname = 'POSNR'.
wa_fcat_vekp-tabname = 'IT_VEKP'.
wa_fcat_vekp-reptext = 'Position'.
wa_fcat_vekp-col_opt = 'X'.
APPEND wa_fcat_vekp TO it_fcat_vekp.
CLEAR wa_fcat_vekp.
lv_col = 1 + lv_col.
wa_fcat_vekp-col_pos = lv_col.
wa_fcat_vekp-fieldname = 'MATNR'.
wa_fcat_vekp-tabname = 'IT_VEKP'.
wa_fcat_vekp-reptext = 'Materialnummer'.
wa_fcat_vekp-col_opt = 'X'.
APPEND wa_fcat_vekp TO it_fcat_vekp.
CLEAR wa_fcat_vekp.
lv_col = 1 + lv_col.
wa_fcat_vekp-col_pos = lv_col.
wa_fcat_vekp-fieldname = 'BPMNG'.
wa_fcat_vekp-tabname = 'IT_VEKP'.
wa_fcat_vekp-reptext = 'Menge'.
wa_fcat_vekp-col_opt = 'X'.
APPEND wa_fcat_vekp TO it_fcat_vekp.
CLEAR wa_fcat_vekp.
lv_col = 1 + lv_col.
wa_fcat_vekp-col_pos = lv_col.
wa_fcat_vekp-fieldname = 'VRKMG'.
wa_fcat_vekp-tabname = 'IT_VEKP'.
wa_fcat_vekp-reptext = 'Mengeneinheit'.
wa_fcat_vekp-col_opt = 'X'.
APPEND wa_fcat_vekp TO it_fcat_vekp.
CLEAR wa_fcat_vekp.
lv_col = 1 + lv_col.
wa_fcat_vekp-col_pos = lv_col.
wa_fcat_vekp-fieldname = 'WERKS'.
wa_fcat_vekp-tabname = 'IT_VEKP'.
wa_fcat_vekp-reptext = 'Werks'.
wa_fcat_vekp-col_opt = 'X'.
APPEND wa_fcat_vekp TO it_fcat_vekp.
CLEAR wa_fcat_vekp.
lv_col = 1 + lv_col.
wa_fcat_vekp-col_pos = lv_col.
wa_fcat_vekp-fieldname = 'LGORT'.
wa_fcat_vekp-tabname = 'IT_VEKP'.
wa_fcat_vekp-reptext = 'Lagerort'.
wa_fcat_vekp-col_opt = 'X'.
APPEND wa_fcat_vekp TO it_fcat_vekp.
CLEAR wa_fcat_vekp.
ENDMETHOD.
METHOD fieldcat_vepo.
CLEAR wa_fcat_vepo.
REFRESH it_fcat_vepo.
DATA: lv_col TYPE i VALUE 0.
lv_col = 1 + lv_col.
wa_fcat_vepo-col_pos = lv_col.
wa_fcat_vepo-fieldname = 'XCHAR'.
wa_fcat_vepo-tabname = 'IT_VEPO'.
wa_fcat_vepo-reptext = 'HU#'.
wa_fcat_vepo-col_opt = 'X'.
APPEND wa_fcat_vepo TO it_fcat_vepo.
CLEAR wa_fcat_vepo.
lv_col = 1 + lv_col.
wa_fcat_vepo-col_pos = lv_col.
wa_fcat_vepo-fieldname = 'VPMAT'.
wa_fcat_vepo-tabname = 'IT_VEPO'.
wa_fcat_vepo-reptext = 'Verpackungsmaterial'.
wa_fcat_vepo-col_opt = 'X'.
APPEND wa_fcat_vepo TO it_fcat_vepo.
CLEAR wa_fcat_vepo.
lv_col = 1 + lv_col.
wa_fcat_vepo-col_pos = lv_col.
wa_fcat_vepo-fieldname = 'BRGEW'.
wa_fcat_vepo-tabname = 'IT_VEPO'.
wa_fcat_vepo-reptext = 'Bruttogewicht'.
wa_fcat_vepo-col_opt = 'X'.
APPEND wa_fcat_vepo TO it_fcat_vepo.
CLEAR wa_fcat_vepo.
lv_col = 1 + lv_col.
wa_fcat_vepo-col_pos = lv_col.
wa_fcat_vepo-fieldname = 'NTGEW'.
wa_fcat_vepo-tabname = 'IT_VEPO'.
wa_fcat_vepo-reptext = 'Nettogewicht'.
wa_fcat_vepo-col_opt = 'X'.
APPEND wa_fcat_vepo TO it_fcat_vepo.
CLEAR wa_fcat_vepo.
lv_col = 1 + lv_col.
wa_fcat_vepo-col_pos = lv_col.
wa_fcat_vepo-fieldname = 'GEWEI'.
wa_fcat_vepo-tabname = 'IT_VEPO'.
wa_fcat_vepo-reptext = 'Gewichtseinheit'.
wa_fcat_vepo-col_opt = 'X'.
APPEND wa_fcat_vepo TO it_fcat_vepo.
CLEAR wa_fcat_vepo.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: delivery TYPE REF TO delivery.
CREATE OBJECT delivery.
CALL METHOD: delivery->get_likp,
delivery->get_vekp,
delivery->get_vepo.
MODULE status_9000 OUTPUT.
SET PF-STATUS 'GUI_9000'.
SET TITLEBAR 'TITLE_9000'.
CREATE OBJECT ob_custom1
EXPORTING
container_name = 'CONTAINER1'.
CREATE OBJECT ob_custom2
EXPORTING
container_name = 'CONTAINER2'.
CREATE OBJECT ob_custom3
EXPORTING
container_name = 'CONTAINER3'.
CREATE OBJECT ob_grid1
EXPORTING
i_parent = ob_custom1.
CREATE OBJECT ob_grid2
EXPORTING
i_parent = ob_custom2.
CREATE OBJECT ob_grid3
EXPORTING
i_parent = ob_custom3.
CALL METHOD ob_grid1->set_table_for_first_display
CHANGING
it_fieldcatalog = it_fcat_likp
it_outtab = it_likp.
CALL METHOD ob_grid2->set_table_for_first_display
CHANGING
it_fieldcatalog = it_fcat_vekp
it_outtab = it_vekp.
CALL METHOD ob_grid3->set_table_for_first_display
CHANGING
it_fieldcatalog = it_fcat_vepo
it_outtab = it_vepo.
ENDMODULE.
MODULE user_command_9000 INPUT.
IF sy-ucomm = 'BACK'
OR sy-ucomm = 'EXIT'
OR sy-ucomm = 'CANCEL'.
FREE: ob_grid1, ob_grid2,ob_grid3, ob_custom1, ob_custom2,ob_custom3.
REFRESH: it_likp, it_vekp,it_vepo.
LEAVE TO SCREEN 0.
ENDIF.
ENDMODULE.
You can't display several GUI controls (ALV in your case) in one container. But you can split a container into several ones by using the "splitter container" (search the many examples in the web which refer to the class CL_GUI_SPLITTER_CONTAINER).

Assign values to dynamic structure

Need idea on the below codes, on how to simplify. Codes below works good but is there a way I could enhance or shorten the codes making it dynamic?
TYPES: BEGIN OF lty_dates,
yesterday TYPE string,
today TYPE string,
tomorrow TYPE string,
END OF lty_dates.
DATA: it_table TYPE TABLE OF lty_dates.
DO 3 TIMES.
CASE lv_count.
WHEN 1.
it_table[ 1 ]-zyesterday = 'Result Yesterday'.
it_table[ 2 ]-zyesterday = 'Result Yesterday'.
it_table[ 3 ]-zyesterday = 'Result Yesterday'.
WHEN 2.
it_table[ 1 ]-ztoday = 'Result Today'.
it_table[ 2 ]-ztoday = 'Result today'.
it_table[ 3 ]-ztoday = 'Result Today'.
WHEN 3.
it_table[ 1 ]-ztommorrow = 'Result Tomorrow'.
it_table[ 2 ]-ztommorrow = 'Result Tomorrow'.
it_table[ 3 ]-ztommorrow = 'Result Tomorrow'.
ENDCASE.
lv_count = lv_count + 1.
ENDDO.
My idea is something like the below pseudocodes. I would not want to perform CASE multiple times specially instances if fields for it_table would reach 100 (fields).
DO 3 TIMES.
ASSIGN 'ZTODAY' TO <dynamic_fieldname>.
it_table[ 1 ]-<dynamic_fieldname> = <dynamic_result>.
it_table[ 2 ]-<dynamic_fieldname> = <dynamic_result>.
it_table[ 3 ]-<dynamic_fieldname> = <dynamic_result>.
ENDDO.
Please help or light me up on this.
You could use the command ASSIGN COMPONENT compname OF STRUCTURE structure TO <field_symbol>.
TYPES: BEGIN OF lty_dates,
yesterday TYPE string,
today TYPE string,
tomorrow TYPE string,
END OF lty_dates.
TYPES: BEGIN OF lty_result ,
fieldname TYPE fieldname,
result TYPE string,
END OF lty_result .
FIELD-SYMBOLS <data> TYPE any .
DATA: lt_table TYPE TABLE OF lty_dates,
lt_result TYPE TABLE OF lty_result.
lt_result = VALUE #( ( fieldname = 'yesterday'
result = 'Result Yesterday' )
( fieldname = 'today'
result = 'Result Today' )
( fieldname = 'tomorrow'
result = 'Result Tomorrow' ) ).
lt_table = VALUE #( ( ) ( ) ( ) ). " 3 empty lines
LOOP AT lt_table ASSIGNING FIELD-SYMBOL(<table>) .
LOOP AT lt_result ASSIGNING FIELD-SYMBOL(<result>) .
ASSIGN COMPONENT <result>-fieldname OF STRUCTURE <table> TO <data> .
<data> = <result>-result.
ENDLOOP .
ENDLOOP .
Actually, your code creates this result table:
YESTERDAY TODAY TOMORROW
Result Yesterday Result Today Result Tomorrow
Result Yesterday Result today Result Tomorrow
Result Yesterday Result Today Result Tomorrow
Why not to use macro for this? Macro can perfectly fulfill your needs:
FIELD-SYMBOLS: <fvalue> TYPE ANY.
DEFINE put_values.
ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fvalue>.
<fvalue> = &3.
END-OF-DEFINITION.
it_table = VALUE #( ( ) ( ) ( ) ).
LOOP AT it_table ASSIGNING FIELD-SYMBOL(<fs_tab>).
put_values 'yesterday' <fs_tab> 'Result Yesterday'.
put_values 'today' <fs_tab> 'Result Today'.
put_values 'tomorrow' <fs_tab> 'Result Tomorrow'.
ENDLOOP.
This will work if the number if the loop iterations are equal to lines of itab (as in your code).

How to return value to user from a Search Help Exit

I have a Search Help with many fields to be displayed to the user in order to apply values. I want to get 3 fields APOFASI, SKOPOS, KATDANL from the user.
In the CALLCONTROL-STEP = SELECT in the exit FM, I want to get these values in variables and then to make some selects and find another field APOFASISAP.
I am trying to pass back to the selection fields of the search help the field APOFASSISAP, but the APOFASI field seems to be blank.
The code is:
TYPES: BEGIN OF ty_apofasisap_tr,
apofasisap_tr TYPE zglk_sap_afopasi,
END OF ty_apofasisap_tr.
DATA: it_apofasisap_tr TYPE TABLE OF ty_apofasisap_tr,
wa_apofasisap_tr LIKE LINE OF it_apofasisap_tr.
DATA: lv_apofasi TYPE zglk_kyanr_ap,
lv_skopos TYPE zskopos,
lv_katdanl TYPE zsl_cat_dan,
lv_apofasisap_arx TYPE zglk_sap_afopasi.
lv_apofasi = wa_shlp_selopt-low.
ls_result-apofasi = ''.
IF lv_apofasi <> ''.
wa_shlp_selopt-low = ''.
MODIFY shlp-selopt FROM wa_shlp_selopt INDEX sy-tabix.
ENDIF.
READ TABLE shlp-selopt INTO wa_shlp_selopt WITH KEY shlpfield = 'SKOPOS'.
lv_skopos = wa_shlp_selopt-low.
READ TABLE shlp-selopt INTO wa_shlp_selopt WITH KEY shlpfield = 'KATDANL'.
lv_katdanl = wa_shlp_selopt-low.
SELECT SINGLE apofasisap INTO lv_apofasisap_arx
FROM zsl_glk_apof
WHERE apofasi = lv_apofasi.
SELECT * FROM zsl_glk_apof_tr
WHERE apofasisap_trp = lv_apofasisap_arx.
wa_apofasisap_tr-apofasisap_tr = zsl_glk_apof_tr-apofasisap_tr.
APPEND wa_apofasisap_tr TO it_apofasisap_tr.
ENDSELECT.
wa_shlp_selopt-shlpname = 'ZAPOF_TROP'.
wa_shlp_selopt-shlpfield = 'APOFASISAP'.
wa_shlp_selopt-sign = 'I'.
wa_shlp_selopt-option = 'EQ'.
wa_shlp_selopt-low = wa_apofasisap_tr-apofasisap_tr.
APPEND wa_shlp_selopt TO shlp-selopt.
This code does not replace/add the values to the appropriate fields.
Can someone help on this?
PS. Let me add another code that I wrote with the help of internet. It is in the STEP of DISPLAY.
IF callcontrol-step = 'DISP'.
TYPES: BEGIN OF ls_result,
apofasi LIKE zsl_glk_apof-apofasi,
apofasidate LIKE zsl_glk_apof-apofasidate,
apofasisap LIKE zsl_glk_apof-apofasisap,
apofasi_trp_x LIKE zsl_glk_apof-apofasi_trp_x,
apofasi_tr_x LIKE zsl_glk_apof-apofasi_tr_x,
fek LIKE zsl_glk_apof-fek,
katdanl LIKE zsl_glk_apof-katdanl,
reference LIKE zsl_glk_apof-reference,
skopos LIKE zsl_glk_apof-skopos,
thema LIKE zsl_glk_apof-thema,
type_desc LIKE zsl_glk_apof-type_desc,
ya_first LIKE zsl_glk_apof-ya_first,
END OF ls_result.
DATA: lt_result TYPE STANDARD TABLE OF ls_result.
CLEAR: lt_result, record_tab, record_tab[].
* Read the value that user gave
READ TABLE shlp-selopt INTO wa_shlp_selopt
WITH KEY shlpfield = 'APOFASI'.
lv_apofasi = wa_shlp_selopt-low.
IF lv_apofasi <> ''.
* Clear this value
wa_shlp_selopt-low = ''.
MODIFY shlp-selopt FROM wa_shlp_selopt INDEX sy-tabix.
ENDIF.
* Find the number starting APOFASISAP from the APOFASI text
SELECT SINGLE apofasisap INTO lv_apofasisap_arx
FROM zsl_glk_apof
WHERE apofasi = lv_apofasi.
IF sy-subrc = 0.
* Find the APOFASISAPs which changes the starting one and display the
*fields
SELECT a~apofasi a~apofasidate a~apofasisap
a~apofasi_tr_x a~apofasi_trp_x
a~thema a~fek a~reference a~ya_first
INTO CORRESPONDING FIELDS OF TABLE lt_result
FROM zsl_glk_apof AS a INNER JOIN zsl_glk_apof_tr AS b
ON a~apofasisap = b~apofasisap_tr
WHERE b~apofasisap_trp = lv_apofasisap_arx.
IF sy-subrc = 0.
*Pass them to display the result.
CALL FUNCTION 'F4UT_RESULTS_MAP'
EXPORTING
* SOURCE_STRUCTURE =
apply_restrictions = 'X'
TABLES
shlp_tab = shlp_tab
record_tab = record_tab
source_tab = lt_result
CHANGING
shlp = shlp
callcontrol = callcontrol
EXCEPTIONS
illegal_structure = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
FREE lt_result.
ENDIF.
It says NO VALUES FOR THIS SELECTION. The table lt_result contains 11 records.
Thanks again.
I agree with the forespeakers, that provided code is a mess, from which nothing can be figured out.
First of all, use DISP event in SHELP FM exit, not SELECT.
Secondly, you fill the wrong table (or do not show us right filling in the snippet above). In order to pass values back to user you should modify a RECORD_TAB table.
Your code should look something like this (joined selections from zsl_glk_apof and zsl_glk_apof_tr tables):
CHECK callcontrol-step = 'DISP'.
DATA: ls_selopt TYPE ddshselopt. "loc str for shlp-selopt
DATA: BEGIN OF ls_record.
INCLUDE STRUCTURE seahlpres.
DATA: END OF ls_record.
DATA: lv_glk_apof_tr TYPE zsl_glk_apof_tr-apofasisap_tr.
LOOP AT shlp-selopt INTO ls_selopt.
* finding SKOPOS and KATDANL values
CASE ls_selopt-shlpfield.
WHEN 'SKOPOS'.
lv_skopos = ls_selopt-low.
WHEN 'KATDANL'.
lv_katdanl = ls_selopt-low.
WHEN OTHERS.
ENDCASE.
* doing smth
* finding some stuff from Z-tables
SELECT SINGLE tr~apofasisap_tr
FROM zsl_glk_apof AS ap
JOIN zsl_glk_apof_tr AS tr
ON ap~apofasisap = tr~apofasisap_trp
INTO lv_glk_apof_tr
WHERE ap~apofasi = lv_apofasi.
ENDLOOP.
* modify record_tab with the found value lv_glk_apof_tr
LOOP AT record_tab INTO ls_record.
ls_record-string+25(5) = lv_glk_apof_tr. "field shift should be based on your values
ENDLOOP.
Hello and thanks all for your valuable help.
Finally I did it as follow:
if callcontrol-step = 'SELECT'.
get parameter id 'ZAP' field p_apofasi.
get parameter id 'ZSK' field p_skopos.
get parameter id 'ZKATDANL' field p_katdanl.
select single apofasisap into lv_apofasisap_arx
from zsl_glk_apof
where apofasi = p_apofasi and
katdanl = p_katdanl and
skopos = p_skopos.
select * into corresponding fields of table it_apofasisap_tr
from zsl_glk_apof_tr
where apofasisap_trp = lv_apofasisap_arx.
* Display the results to the user
types: begin of ls_result,
apofasi like zsl_glk_apof-apofasi,
apofasidate like zsl_glk_apof-apofasidate,
apofasisap like zsl_glk_apof-apofasisap,
apofasi_trp_x like zsl_glk_apof-apofasi_trp_x,
apofasi_tr_x like zsl_glk_apof-apofasi_tr_x,
katdanl like zsl_glk_apof-katdanl,
skopos like zsl_glk_apof-skopos,
type_desc like zsl_glk_apof-type_desc,
end of ls_result.
data: lt_result type standard table of ls_result.
clear: lt_result, record_tab, record_tab[].
* Fill all the fields of the Search-Help with the data according to the
*selections of the user.
select apofasi apofasidate apofasisap
apofasi_tr_x apofasi_trp_x
katdanl skopos type_desc
into corresponding fields of table lt_result
from zsl_glk_apof
for all entries in it_apofasisap_tr
where apofasisap = it_apofasisap_tr-apofasisap_tr and
apofasi_tr_x = 'X'. "lv_apofasi_tr_x.
call function 'F4UT_RESULTS_MAP'
tables
shlp_tab = shlp_tab
record_tab = record_tab
source_tab = lt_result
changing
shlp = shlp
callcontrol = callcontrol
exceptions
illegal_structure = 1
others = 2.
if sy-subrc = 0.
callcontrol-step = 'DISP'.
else.
callcontrol-step = 'EXIT'.
endif.
endif.
Regards
Elias

Modify an itab during looping another itab

I want to do the following
Loop at itab into wa_itab.
wa_itab2-field1 = wa_itab-field1.
wa_itab2-field2 = wa_itab-field2.
wa_itab2-field3 = wa_itab-field3.
wa_itab2-field4 = wa_itab-field4.
*how to insert in itab2 if fields 1,2 & 3 do not exist or update itab2 if fields 1,2 & 3 exist?
EndLoop.
Let me explain more.
I have itab with fields bukrs, kunnr, date, action.
The itab2 with fields bukrs, kunnr, name1 (from kna1),date01 (jan), action01, date02 (Feb), action02 ... date12 (Dec), action12.
During loop of itab I want if the itab2 has a record with bukrs and kunnr then it will update date and action according to the month of itab-date, if it do not exist (bukrs, kunnr) then insert a record to itab2.
I hope that this explains what I want.
Thanks in advance
Elias
PS. Here is the program
*&---------------------------------------------------------------------*
*& Report Z_COLLECTORS_ACTIONS
*&
*&---------------------------------------------------------------------*
*& Description
*&
*&---------------------------------------------------------------------*
*& Change log:
*& Date Author Action
*&
*&---------------------------------------------------------------------*
REPORT z_collectors_actions.
TABLES: zcollectoraction, kna1, t001.
TYPE-POOLS : slis.
TYPES: BEGIN OF ty_totalcollectoractions,
bukrs TYPE zcollectoraction-bukrs,
kunnr TYPE zcollectoraction-kunnr,
name1 TYPE kna1-name1,
date01 TYPE zcollectoraction-dat,
date02 TYPE zcollectoraction-dat,
date03 TYPE zcollectoraction-dat,
date04 TYPE zcollectoraction-dat,
date05 TYPE zcollectoraction-dat,
date06 TYPE zcollectoraction-dat,
date07 TYPE zcollectoraction-dat,
date08 TYPE zcollectoraction-dat,
date09 TYPE zcollectoraction-dat,
date10 TYPE zcollectoraction-dat,
date11 TYPE zcollectoraction-dat,
date12 TYPE zcollectoraction-dat,
action01 TYPE zcollectoraction-action,
action02 TYPE zcollectoraction-action,
action03 TYPE zcollectoraction-action,
action04 TYPE zcollectoraction-action,
action05 TYPE zcollectoraction-action,
action06 TYPE zcollectoraction-action,
action07 TYPE zcollectoraction-action,
action08 TYPE zcollectoraction-action,
action09 TYPE zcollectoraction-action,
action10 TYPE zcollectoraction-action,
action11 TYPE zcollectoraction-action,
action12 TYPE zcollectoraction-action,
END OF ty_totalcollectoractions.
DATA: wa_collectoraction LIKE zcollectoraction,
it_collectoraction LIKE TABLE OF zcollectoraction,
wa_totalcollectoractions TYPE ty_totalcollectoractions,
it_totalcollectoractions TYPE TABLE OF ty_totalcollectoractions WITH KEY kunnr.
DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
field_catalog TYPE lvc_t_fcat,
gd_layout TYPE slis_layout_alv,
gd_repid LIKE sy-repid,
g_save TYPE c VALUE 'X',
g_variant TYPE disvariant,
gx_variant TYPE disvariant,
g_exit TYPE c.
SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-001.
PARAMETERS: variant LIKE disvariant-variant.
PARAMETERS p_bukrs TYPE bukrs OBLIGATORY.
SELECT-OPTIONS: so_kunnr FOR kna1-kunnr OBLIGATORY,
so_date FOR sy-datum OBLIGATORY.
SELECTION-SCREEN END OF BLOCK bl1.
INITIALIZATION.
gx_variant-report = sy-repid.
CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
EXPORTING
i_save = g_save
CHANGING
cs_variant = gx_variant
EXCEPTIONS
not_found = 2.
IF sy-subrc = 0.
variant = gx_variant-variant.
ENDIF.
START-OF-SELECTION.
SELECT bukrs kunnr dat MAX( time ) AS time
FROM zcollectoraction INTO CORRESPONDING FIELDS OF TABLE it_collectoraction
WHERE bukrs = p_bukrs AND
kunnr IN so_kunnr AND
dat IN so_date
GROUP BY bukrs kunnr dat.
LOOP AT it_collectoraction INTO wa_collectoraction.
SELECT SINGLE * FROM zcollectoraction INTO CORRESPONDING FIELDS OF wa_collectoraction
WHERE bukrs = wa_collectoraction-bukrs AND
kunnr = wa_collectoraction-kunnr AND
dat = wa_collectoraction-dat AND
time = wa_collectoraction-time.
MODIFY it_collectoraction FROM wa_collectoraction.
wa_totalcollectoractions-bukrs = wa_collectoraction-bukrs.
wa_totalcollectoractions-kunnr = wa_collectoraction-kunnr.
SELECT SINGLE name1 FROM kna1 INTO wa_totalcollectoractions-name1
WHERE kunnr = wa_collectoraction-kunnr.
DATA lv_month TYPE n LENGTH 2.
lv_month = wa_collectoraction-dat+4(2).
CASE lv_month.
WHEN '01'.
wa_totalcollectoractions-date01 = wa_collectoraction-dat.
wa_totalcollectoractions-action01 = wa_collectoraction-action.
WHEN '02'.
wa_totalcollectoractions-date02 = wa_collectoraction-dat.
wa_totalcollectoractions-action02 = wa_collectoraction-action.
WHEN '03'.
wa_totalcollectoractions-date03 = wa_collectoraction-dat.
wa_totalcollectoractions-action03 = wa_collectoraction-action.
WHEN '04'.
wa_totalcollectoractions-date04 = wa_collectoraction-dat.
wa_totalcollectoractions-action04 = wa_collectoraction-action.
WHEN '05'.
wa_totalcollectoractions-date05 = wa_collectoraction-dat.
wa_totalcollectoractions-action05 = wa_collectoraction-action.
WHEN '06'.
wa_totalcollectoractions-date06 = wa_collectoraction-dat.
wa_totalcollectoractions-action06 = wa_collectoraction-action.
WHEN '07'.
wa_totalcollectoractions-date07 = wa_collectoraction-dat.
wa_totalcollectoractions-action07 = wa_collectoraction-action.
WHEN '08'.
wa_totalcollectoractions-date08 = wa_collectoraction-dat.
wa_totalcollectoractions-action08 = wa_collectoraction-action.
WHEN '09'.
wa_totalcollectoractions-date09 = wa_collectoraction-dat.
wa_totalcollectoractions-action09 = wa_collectoraction-action.
WHEN '10'.
wa_totalcollectoractions-date10 = wa_collectoraction-dat.
wa_totalcollectoractions-action10 = wa_collectoraction-action.
WHEN '11'.
wa_totalcollectoractions-date11 = wa_collectoraction-dat.
wa_totalcollectoractions-action11 = wa_collectoraction-action.
WHEN '12'.
wa_totalcollectoractions-date12 = wa_collectoraction-dat.
wa_totalcollectoractions-action12 = wa_collectoraction-action.
WHEN OTHERS.
ENDCASE.
READ TABLE it_totalcollectoractions INTO wa_totalcollectoractions
WITH TABLE KEY kunnr = wa_collectoraction-kunnr.
IF sy-subrc = 0.
MODIFY it_totalcollectoractions INDEX sy-tabix FROM wa_totalcollectoractions.
ELSE.
INSERT wa_totalcollectoractions INTO TABLE it_totalcollectoractions.
ENDIF.
ENDLOOP.
PERFORM build_fieldcatalog_agreggate.
PERFORM display_alv_report.
*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM display_alv_report .
gd_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = gd_repid
i_callback_top_of_page = 'TOP-OF-PAGE' "see FORM
i_callback_user_command = 'USER_COMMAND'
it_fieldcat = fieldcatalog[]
i_save = 'X'
is_variant = g_variant
TABLES
t_outtab = it_totalcollectoractions
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " DISPLAY_ALV_REPORT
*-------------------------------------------------------------------*
* Form TOP-OF-PAGE *
*-------------------------------------------------------------------*
* ALV Report Header *
*-------------------------------------------------------------------*
FORM top-of-page.
*ALV Header declarations
DATA: t_header TYPE slis_t_listheader,
wa_header TYPE slis_listheader,
t_line LIKE wa_header-info,
ld_lines TYPE i,
ld_linesc(10) TYPE c,
lv_butxt TYPE butxt.
SELECT SINGLE butxt INTO lv_butxt FROM t001
WHERE bukrs = p_bukrs.
* Title
wa_header-typ = 'H'.
wa_header-info = 'Collectors'' Action Report'.
APPEND wa_header TO t_header.
CLEAR wa_header.
* Date
wa_header-typ = 'S'.
wa_header-key = 'Date: '.
CONCATENATE sy-datum+6(2) '.'
sy-datum+4(2) '.'
sy-datum(4) INTO wa_header-info. "todays date
APPEND wa_header TO t_header.
CLEAR: wa_header.
* Bukrs
wa_header-typ = 'S'.
wa_header-key = 'Company: '.
CONCATENATE p_bukrs lv_butxt INTO wa_header-info SEPARATED BY space.
APPEND wa_header TO t_header.
CLEAR wa_header.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = t_header.
ENDFORM. "top-of-page
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCATALOG_AGREGGATE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM build_fieldcatalog_agreggate .
fieldcatalog-fieldname = 'KUNNR'.
fieldcatalog-seltext_m = 'Customer ID'.
fieldcatalog-col_pos = 0.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'NAME1'.
fieldcatalog-seltext_m = 'Customer Name'.
fieldcatalog-col_pos = 1.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION01'.
fieldcatalog-seltext_m = 'January'.
fieldcatalog-col_pos = 2.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION02'.
fieldcatalog-seltext_m = 'February'.
fieldcatalog-col_pos = 3.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION03'.
fieldcatalog-seltext_m = 'March'.
fieldcatalog-col_pos = 4.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION04'.
fieldcatalog-seltext_m = 'April'.
fieldcatalog-col_pos = 5.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION05'.
fieldcatalog-seltext_m = 'May'.
fieldcatalog-col_pos = 6.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION06'.
fieldcatalog-seltext_m = 'June'.
fieldcatalog-col_pos = 7.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION07'.
fieldcatalog-seltext_m = 'July'.
fieldcatalog-col_pos = 8.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION08'.
fieldcatalog-seltext_m = 'August'.
fieldcatalog-col_pos = 9.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION09'.
fieldcatalog-seltext_m = 'September'.
fieldcatalog-col_pos = 10.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION10'.
fieldcatalog-seltext_m = 'Octomber'.
fieldcatalog-col_pos = 11.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION11'.
fieldcatalog-seltext_m = 'Noveber'.
fieldcatalog-col_pos = 10.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
fieldcatalog-fieldname = 'ACTION12'.
fieldcatalog-seltext_m = 'December'.
fieldcatalog-col_pos = 11.
APPEND fieldcatalog TO fieldcatalog.
CLEAR fieldcatalog.
ENDFORM. " BUILD_FIELDCATALOG_AGREGGATE
If itab is not too big, then you can do like below.
clear: itab2[].
Loop at itab into wa_itab.
wa_itab2-field1 = wa_itab-field1.
wa_itab2-field2 = wa_itab-field2.
wa_itab2-field3 = wa_itab-field3.
wa_itab2-field4 = wa_itab-field4.
...
read table itab2 into data(ls_itab2) with key field1 = wa_itab2-field1
field2 = wa_itab2-field2
field3 = wa_itab2-field3
...
binary search.
if sy-subrc = 0 .
" modify the line
modify itab2 from wa_itab2 index sy-tabix.
else.
" append the line
append wa_itab2 to itab2.
sort itab2 ascending by field1 field2 field3 ... . "the key fields.
endif.
EndLoop.
FIELD-SYMBOLS: <fs_itab2> TYPE wa_itab2.
SORT itab2 BY bukrs kunnr.
LOOP AT itab INTO wa_itab.
***CHECK IF DATES EXIST***
READ TABLE itab2 ASSIGNING <fs_itab2> WITH KEY bukrs = wa_itab-bukrs
kunnr = wa_itab-kunnr
BINARY SEARCH.
if sy-subrc = 0 . "if the record are founded
<fs_itab2>-field1 = wa_itab-field1.
<fs_itab2>-field2 = wa_itab-field2.
<fs_itab2>-field3 = wa_itab-field3.
...
else.
APPEND INITIAL LINE TO itab2 ASSIGNING <fs_itab2>.
IF <fs_itab2> IS ASSIGNED.
<fs_itab2>-field1 = wa_itab-field1.
<fs_itab2>-field2 = wa_itab-field2.
<fs_itab2>-field3 = wa_itab-field3.
...
ENDIF.
ENDLOOP.
Thats is all with field symbols.
You do not need looping here, MOVE-CORRESPONDING do this for internal tables like a charm:
MOVE-CORRESPONDING src_tab TO target.
Here is the sample where MWST price conditions are selected into two itabs, then the currently valid ones are made invalid, then couple of additional invalid conditions with new key are added, and finally the second itab lt_upd_konh is updated, so that all MWST conditions in it gets invalidated with adding extra ones.
SELECT * FROM konh INTO TABLE #DATA(lt_konh) WHERE kschl = 'MWST'.
DATA(lt_upd_konh) = lt_konh.
LOOP AT lt_konh ASSIGNING FIELD-SYMBOL(<fs_konh>) WHERE datbi > sy-datum.
<fs_konh>-datbi = sy-datum - 1.
ENDLOOP.
<fs_konh>-knumh = 1000.
APPEND <fs_konh> TO lt_konh.
<fs_konh>-knumh = 1001.
APPEND <fs_konh> TO lt_konh.
<fs_konh>-knumh = 1002.
APPEND <fs_konh> TO lt_konh.
MOVE-CORRESPONDING lt_konh TO lt_upd_konh.

How to define a 2-Column Array A = 1, B = 2... ZZZ =?

I need to create a 2 column array in ABAP so that a program can look up a record item (defined by the letters A - ZZZ) and then return the number associated with it.
For example:
A = 1
B = 2
C = 3
...
Z = 26
AA = 27
AB = 28
...
AZ =
BA =
...
BZ =
CA =
...
...
ZZZ =
Please can you suggest how I can code this.
Is there a better option than writing an array?
Thanks.
you don't need to lookup the value in a table. this can be calculated:
parameters: p_input(3) type c value 'AAA'.
data: len type i value 0,
multiplier type i value 1,
result type i value 0,
idx type i.
* how many characters are there?
len = strlen( p_input ).
idx = len.
* compute the value for every char starting at the end
* in 'ABC' the C is multiplied with 1, the B with 26 and the A with 26^2
do len times.
* p_input+idx(1) should be the actual character and we look it up in sy-abcde
search p_input+idx(1) in SY-ABCDE.
* if p_input+idx(1) was A then sy-fdpos should now be set to 0 that is s why we add 1
compute result = result + ( sy-fdpos + 1 ) * multiplier.
idx = idx - 1.
multiplier = multiplier * 26.
enddo.
write: / result.
i didn't test the program and it has pretty sure some syntax errors. but the algorithm behind it should work.
perhaps I'm misunderstanding, but don't you want something like this?
type: begin of t_lookup,
rec_key type string,
value type i,
end of t_lookup.
data: it_lookup type hashed table of t_lookup with unique key rec_key.
then once it's populated, read it back
read table it_lookup with key rec_key = [value] assigning <s>.
if sy-subrc eq 0.
" got something
else.
" didn't
endif.
unfortunately, arrays don't exist in ABAP, but a hashed table is designed for this kind of lookup (fast access, unique keys).
DATA: STR TYPE STRING, I TYPE I, J TYPE I, K TYPE I, CH TYPE C, RES
TYPE INT2, FLAG TYPE I.
PARAMETERS: S(3).
START-OF-SELECTION.
I = STRLEN( S ).
STR = S.
DO I TIMES.
I = I - 1.
CH = S.
IF CH CO '1234567890.' OR CH CN SY-ABCDE.
FLAG = 0.
EXIT.
ELSE.
FLAG = 1.
ENDIF.
SEARCH SY-ABCDE FOR CH.
J = I.
K = 1.
WHILE J > 0.
K = K * 26.
J = J - 1.
ENDWHILE.
K = K * ( SY-FDPOS + 1 ).
RES = RES + K.
REPLACE SUBSTRING CH IN S WITH ''.
ENDDO.
* RES = RES + SY-FDPOS.
IF FLAG = 0.
MESSAGE 'String is not valid.' TYPE 'S'.
ELSE.
WRITE: /, RES .
ENDIF.
Use this code after executing.
I did a similar implementation some time back.
Check this it it works for you.
DATA:
lv_char TYPE char1,
lv_len TYPE i,
lv_len_minus_1 TYPE i,
lv_partial_index1 TYPE i,
lv_partial_index2 TYPE i,
lv_number TYPE i,
result_tab TYPE match_result_tab,
lv_col_index_substr TYPE string,
lv_result TYPE i.
FIELD-SYMBOLS:
<match> LIKE LINE OF result_tab.
lv_len = strlen( iv_col_index ) .
lv_char = iv_col_index(1).
FIND FIRST OCCURRENCE OF lv_char IN co_char RESULTS result_tab.
READ TABLE result_tab ASSIGNING <match> INDEX 1.
lv_number = <match>-offset .
lv_number = lv_number + 1 .
IF lv_len EQ 1.
ev_col = ( ( 26 ** ( lv_len - 1 ) ) * lv_number ) .
ELSE.
lv_len_minus_1 = lv_len - 1.
lv_col_index_substr = iv_col_index+1(lv_len_minus_1) .
CALL METHOD get_col_index
EXPORTING
iv_col_index = lv_col_index_substr
IMPORTING
ev_col = lv_partial_index2.
lv_partial_index1 = ( ( 26 ** ( lv_len - 1 ) ) * lv_number ) + lv_partial_index2 .
ev_col = lv_partial_index1 .
ENDIF.
Here The algorithm uses a recursive logic to determine the column index in numbers.
This is not my algorithm but have adapted to be used in ABAP.
The original algorithm is used in Open Excel, cant find any links right now.