Application Development and Automation 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: 
Read only

collect statement does not work properly in smartforms

Former Member
0 Likes
4,517

Hi,
i used collect statement in a colum in table in smartforms under main undow.....my code is

select  vbeln  zprs_role from vbrp into corresponding fields of table it_role
  where vbeln = wa_vbrp-vbeln.

  loop at it_role into wa_role.
   collect wa_role into itab.
    endloop.
loop at itab into wa.
      role = wa-zprs_role.
    append wa to itab .
    endloop.
i also have loop in table it_tab to wa_tab.

finaly i display role in text in smartforms but it pick the last value among the 4 value and diaplay it 5 times..
but in dibugging i can see that itab have 4 value after using collect statement
the structure of itab,it_role ,it_tab is

types: begin of ty_tab,
      fkdat type vbrk-fkdat,
      knumv type vbrk-knumv,
      kawrt type konv-kawrt,
      ps_psp_pnr type vbrp-ps_psp_pnr,
      post1 type prps-post1,
      arktx type vbrp-arktx,
      posnr type vbrp-posnr,
      fkimg type vbrp-fkimg,
      netwr type vbrp-netwr,
      vbeln type vbrp-vbeln,
      zprs_role type vbrp-zprs_role,
      end of ty_tab.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
4,413

You will have to move to another table and collect.


        TYPES: BEGIN OF t_collect,
      
        zprs_role type vbrp-zprs_role,
            fkimg TYPE vbrp-fkimg,
    END OF t_collect.

  DATA: it_roles TYPE STANDARD TABLE OF t_collect, wa_roles type t_collect.
data : it_collect type TABLE OF t_collect.


loop at it_itab into wa_itab.
    move-corresponding wa_itab to wa_roles.
    APPEND wa_roles to it_roles.
  endloop.


sort it_roles by
zprs_role.

loop at it_roles into wa_roles.

    collect wa_roles into it_collect.

endloop.


21 REPLIES 21
Read only

Former Member
0 Likes
4,413

Hi Abaper,

Your question is not clear.

Answer this -

1) Give structure of table on which you are doing Collect.

Note that the table having collected data considers all non numeric fields as keys.

BR.

Read only

Former Member
0 Likes
4,414

You will have to move to another table and collect.


        TYPES: BEGIN OF t_collect,
      
        zprs_role type vbrp-zprs_role,
            fkimg TYPE vbrp-fkimg,
    END OF t_collect.

  DATA: it_roles TYPE STANDARD TABLE OF t_collect, wa_roles type t_collect.
data : it_collect type TABLE OF t_collect.


loop at it_itab into wa_itab.
    move-corresponding wa_itab to wa_roles.
    APPEND wa_roles to it_roles.
  endloop.


sort it_roles by
zprs_role.

loop at it_roles into wa_roles.

    collect wa_roles into it_collect.

endloop.


Read only

0 Likes
4,413

hi susmitha,

  this below code work fine and after collect itab contain four value among the 5 value but in table under main window in smartform how i display the ITAB value because i used loop in table it_tab into wa_tab.........

select  vbeln  zprs_role from vbrp into corresponding fields of table it_role

   where vbeln = wa_vbrp-vbeln.

   loop at it_role into wa_role.

    collect wa_role into itab.

     endloop.

after above logic ITAB contain 4 values but how i display ITAB in table under main window in smartforms becoz table have a loop IT_TAB into WA_TAB.....how i pass all the 4 values from ITAB TO WA_TAB?

Read only

0 Likes
4,413

I have not totally understood your environment.

If you want to have a summary of the quantity based on just the zprs_role, you will have to do it this way by moving it to another table.

You could try another option also which I have mentioned in the post below.

You can pass it_collect also to the smart form. (Btw, I hope you  have given,

collect wa_role into it_collect ).

Then in your smartform when you are looping at itab, also add the code to get the fkimg for that zprs_role.

loop at itab into wa_itab.

  read table it_collect into wa_collect with key zprs_role = wa_itab-zprs_role.

.....

endloop.

and then use these value wa_collect-fkimg, and wa_collect-zprs_role in your smartform.

Read only

0 Likes
4,413


Another option is to manually calculate the sum of quantity for each zprs_role. .

  (1. Making zprs_role the first field in the table type.

   2.  sort itab by zprs_rol.

   3. loop at itab into wa_itab.

          every loop calculate tot_fkimg = tot_fkimg + wa_itab-fkimg.

          at end of zprs_rol.

               move the wa_itab-fkimg = tot_fkimg.

                append wa_itab-fmimg to itbab.

                 clear tot_fkimg.

            endat.

      

       

      endloop.

Read only

0 Likes
4,413

Read only

0 Likes
4,413

Hi

you will have to use your collect statement etc before starting the loop of table so that you have records which are to be displayed.

Nabheet

Read only

0 Likes
4,413

You dont want the entire line Tarun Singh J1 to be displayed?

Or you just want the hours to be added up for every J1

ie.                 

                               rate hours

Edward Charles J1  80     9

Tarun Singh        J1 10      9

if this is what you require, then you can use the first option that I had mentioned.

or just.

Edward Charles J1  80 9

If this is what you require, then you can follow the second option of manually calculating the sum of fkimg for each zpr_role

and moving it to another table itab1.

Read only

0 Likes
4,413

i want to display :

rate hours

Edward Charles  j1  80     9

Tarun Singh              10     

Read only

0 Likes
4,413

Ok. Try this.

sort by zprs_role. "(make zprs_role the first field as we need to use at end and at new statement).

data : lv_tabix like sy-tabix.

dat: wa_tab1 like line of it_itab.

loop at it_itab into wa_tab.

  at new zprs_role.

        wa_tab1 = wa_tab.

        lv_tabix = sy-tabix.

  endat.

  tot_fkimg = wa_tab-fkimg + tot_fkimg.

   clear : wa_tab-zprs_role, wa_tab-fkimg.

   modify it_itab from wa_tab index sy-tabix TRANSPORTING zprs_role fkimg.

    at end of zpr_role.

          wa_tab1-fkimg = tot_fkimg.

          modify it_itab from wa_tab1 index lv_tabix transporting fkimg.

           clear : tot_fkimg, wa_tab1.

    endat.

   clear wa_tab.

endloop.

Read only

0 Likes
4,413

hi susmita ,

could u plz explain ur code......

sort by zprs_role. "(make zprs_role the first field as we need to use at end and at new statement).

data : lv_tabix like sy-tabix.

dat: wa_tab1 like line of it_itab.

loop at it_itab into wa_tab.

  at new zprs_role.

        wa_tab1 = wa_tab.

        lv_tabix = sy-tabix.

  endat.

  tot_fkimg = wa_tab-fkimg + tot_fkimg.

   clear : wa_tab-zprs_role, wa_tab-fkimg.

   modify it_itab from wa_tab index sy-tabix TRANSPORTING zprs_role fkimg.

    at end of zpr_role.

          wa_tab1-fkimg = tot_fkimg.

          modify it_itab from wa_tab1 index lv_tabix transporting fkimg.

           clear : tot_fkimg, wa_tab1.

    endat.

   clear wa_tab.

endloop.

thanks,

abaper

Read only

0 Likes
4,413

hi susmitha ,

after using your logic my smartforms goes fine.....here is your logic

sort by zprs_role. "(make zprs_role the first field as we need to use at end and at new statement).

data : lv_tabix like sy-tabix.

dat: wa_tab1 like line of it_itab.

loop at it_itab into wa_tab.

  at new zprs_role.

        wa_tab1 = wa_tab.

        lv_tabix = sy-tabix.

  endat.

  tot_fkimg = wa_tab-fkimg + tot_fkimg.

   clear : wa_tab-zprs_role, wa_tab-fkimg.

   modify it_itab from wa_tab index sy-tabix TRANSPORTING zprs_role fkimg.

    at end of zpr_role.

          wa_tab1-fkimg = tot_fkimg.

          modify it_itab from wa_tab1 index lv_tabix transporting fkimg.

           clear : tot_fkimg, wa_tab1.

    endat.

   clear wa_tab.

endloop.

but  I want only one J1 insteed of two

Read only

0 Likes
4,413

data : lv_tabix like sy-tabix.

data: wa_tab1 like line of it_itab, wa_tmp like line of it_itab.

sort it_itab by zprs_role.

loop at it_itab into wa_itab.
   wa_tmp = wa_itab.
   at new
zprs_role.
         wa_tab1 = wa_tmp.
         lv_tabix = sy-tabix.
   endat.
      tot_fkimg = wa_itab-fkimg + tot_fkimg.
       clear : wa_itab-fkimg.
       modify it_itab from wa_itab index sy-tabix TRANSPORTING fkimg.
    at end of
zprs_role.
      wa_tab1-fkimg = tot_fkimg.
      modify it_itab from wa_tab1 index lv_tabix transporting fkimg.
      clear : tot_fkimg, wa_tab1.
    endat.
    clear wa_itab.
endloop.

clear : wa_itab-
zprs_role .
modify it_itab from wa_itab TRANSPORTING
zprs_role where fkimg is INITIAL.

Explanation.


1. When we make zprs_role as the first field and sort the table by zprs_role, the at new event is triggered everytime the zprs_role changes, at end of event will be triggered the last time the loop runs for a particular zprs_role.

2.

at new zprs_role.

         wa_tab1 = wa_tmp.
         lv_tabix = sy-tabix.
   endat.

The first time the loop executes for a particular role, you want the role and the total quantity to be there for this record. So we are saving the line number of this row to be modified later after finding the total quantity. The contents are also moved to wa_tab1.

3.

      tot_fkimg = wa_itab-fkimg + tot_fkimg.
       clear : wa_itab-fkimg.
       modify it_itab from wa_itab index sy-tabix TRANSPORTING fkimg.

For all other records, other than the first record for each role, the quantity is used to calculate the total. The value for role and quantity is cleared.

4.

  at end of zprs_role.
      wa_tab1-fkimg = tot_fkimg.
      modify it_itab from wa_tab1 index lv_tabix transporting fkimg.
      clear : tot_fkimg, wa_tab1.
    endat.

When the loop runs for the last record for a role,  you get the total quantity.

  Update it in the first record for that role with the index and work area that was saved earlier. Clear the variable to calculate the total tot_fkimg, to calculate total for the next role.

5.

clear : wa_itab-zprs_role .
modify it_itab from wa_itab TRANSPORTING
zprs_role where fkimg is INITIAL.

Where all fkimg was cleared in the loop, clear zprs_role also.

Note : I am separately clearing zprs_role outside the loop rather than inside the loop, because the at new and at end of events will not be triggered properly if we change the values of zprs_role inside the loop.

Read only

0 Likes
4,413

Hi susmitha,

one more thing in below screen short I got a value 0 in hours column........but I don't want to display the value 0.....

Read only

0 Likes
4,412

Try giving the amount field like this in your smartform.

&fkimg(I)&

(I) addition suppresses output if value is initial.

Thanks,

Susmitha

Also I hope you have added the following statement in your program

clear : wa_itab-zprs_role .
modify it_itab from wa_itab TRANSPORTING
zprs_role where fkimg is INITIAL.

Coz, I still see 2 J1 in your output.


Read only

0 Likes
4,412

hi SUSMITA,

problem solv using your logic &fkimg(I)&

Read only

0 Likes
4,412

hi SUSMITA,

problem solv using your logic &fkimg(I)&

Read only

0 Likes
4,412

hi,

how to reduce unwanted space between line item....

Read only

0 Likes
4,412

HI SUSMITHA,

using your logic

data : lv_tabix like sy-tabix.

data: wa_tab1 like line of it_itab, wa_tmp like line of it_itab.

sort it_itab by zprs_role.

loop at it_itab into wa_itab.
   wa_tmp = wa_itab.
   at new
zprs_role.
         wa_tab1 = wa_tmp.
         lv_tabix = sy-tabix.
   endat.
      tot_fkimg = wa_itab-fkimg + tot_fkimg.
       clear : wa_itab-fkimg.
       modify it_itab from wa_itab index sy-tabix TRANSPORTING fkimg.
    at end of
zprs_role.
      wa_tab1-fkimg = tot_fkimg.
      modify it_itab from wa_tab1 index lv_tabix transporting fkimg.
      clear : tot_fkimg, wa_tab1.
    endat.
    clear wa_itab.
endloop.

clear : wa_itab-
zprs_role .
modify it_itab from wa_itab TRANSPORTING
zprs_role where fkimg is INITIAL.

every thing goes fine.............

I want quantity(1 or 2 or 3) of zprs_role beside zprs_role field in smartforms

Read only

nishantbansal91
Active Contributor
0 Likes
4,412

Hi Abaper,

Before using Collect statement your internal table must be sort on specific value.

For manually Sum instead of  using the Collect statement use the Control Event in ABAP.

Loop at it_itab.

tot_fkimg = tot_fkimg + wa_itab-fkimg.

on change of wa_itab-vbeln

     if sy-subrc ne 1. "Always check the SUBRC value ne 1.

                     

append wa to internal table.

     endif.

endloop.

if you didn't write the statement sy-subrc ne 1.

then everytime it will gone for first item.

Regards.

Nishant Bansal

Read only

Former Member
0 Likes
4,412

HI susmitha,

you r really a knowledgably person in sapa abp.....u help me a lots ....thanks for helping.....

thanks,

abaper