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

value getting null after loop at it into wa

Former Member
0 Likes
1,864

Hi Experts,

select bukrs

          kunnr

          budat

          wrbtr

     belnr

     shkzg

     from bsad

     into table it_bsad

     where bukrs in s_bukrs and kunnr in s_kunnr and budat in s_date.

   select bukrs

        kunnr

        budat

        wrbtr

        belnr

        shkzg

     into table it_bsid

     from bsid

*    for all entries in it_bsad

     where bukrs in s_bukrs and kunnr in s_kunnr and budat in s_date.

break-point.

it_final = it_bsad.

append lines of it_bsid to it_final.

loop at it_final into wa_final.

   if wa_final-shkzg = 'S'.

     wa_final-dr = wa_final-wrbtr.

     else.

       wa_final-cr = wa_final-wrbtr.

       endif.

wa_final-balance = wa_final-balance + wa_final-dr - wa_final-cr.

       modify it_final from wa_Final.

endloop.

I am trying to calculate the balance in the above code. In debugger, after crossing the loop the value of wa_final-balance becomes 0. why? I am sure its easy. I am lacking somewhere. Please guide.

Regards

Purnand

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,824

Hello,

when your loop processing then it comes 0. you tell this..i think your next row in internal table hold field balance = 0. for that reason , when loop start it takes 0.

Jst E.G:  dr       cr        balance

              100     0            100       =>   this is your internal table....

               30     0               0

     when loop processng it takes 1st row....then endloop. next it takes 2nd row...see balance = 0.

     i think for that reason it takes 0.        

loop at it_final into wa_final.

  if wa_final-shkzg = 'S'.

    wa_final-dr = wa_final-wrbtr.

    else.                                                      DAta: bal type p.

      wa_final-cr = wa_final-wrbtr.

      endif.

   wa_final-balance = bal + wa_final-dr - wa_final-cr.

bal = wa_final-balance.

      modify it_final from wa_Final.

  

endloop.

i think your problem will be solved.........if not then let me know....

Thanks

Sabyasachi

Hi Experts,

select bukrs

          kunnr

          budat

          wrbtr

     belnr

     shkzg

     from bsad

     into table it_bsad

     where bukrs in s_bukrs and kunnr in s_kunnr and budat in s_date.

   select bukrs

        kunnr

        budat

        wrbtr

        belnr

        shkzg

     into table it_bsid

     from bsid

*    for all entries in it_bsad

     where bukrs in s_bukrs and kunnr in s_kunnr and budat in s_date.

break-point.

it_final = it_bsad.

append lines of it_bsid to it_final.

loop at it_final into wa_final.

   if wa_final-shkzg = 'S'.

     wa_final-dr = wa_final-wrbtr.

     else.

       wa_final-cr = wa_final-wrbtr.

       endif.

wa_final-balance = wa_final-balance + wa_final-dr - wa_final-cr.

       modify it_final from wa_Final.

endloop.

I am trying to calculate the balance in the above code. In debugger, after crossing the loop the value of wa_final-balance becomes 0. why? I am sure its easy. I am lacking somewhere. Please guide.

Regards

Purnand

11 REPLIES 11
Read only

Amarpreet
Active Participant
0 Likes
1,824

Im not sure what u are trying to do here , but when u use FOR ALL ENTRIES in the second select shouldn't there be a condition for it in the where addition ?

Read only

Former Member
0 Likes
1,824

hi purnand

I think you are missing transporting in modify staement

try this.

loop at it_final into wa_final.

   if wa_final-shkzg = 'S'.

     a = sy-tabix.

     wa_final-dr = wa_final-wrbtr.

     else.

       wa_final-cr = wa_final-wrbtr.

         a = sy-tabix.

       endif.

wa_final-balance = wa_final-balance + wa_final-dr - wa_final-cr.

       modify it_final from wa_Final transporting balance index a.

clear a.

clear wa_final.

endloop.

regards

vaibhav

Read only

Former Member
0 Likes
1,824

Hi Purnand,

Use below code.

loop at it_final into wa_final.

   if wa_final-shkzg = 'S'.

     wa_final-dr = wa_final-wrbtr.

     else.

       wa_final-cr = wa_final-wrbtr.

       endif.

wa_final-balance = wa_final-balance + wa_final-dr - wa_final-cr.

       modify it_final from wa_Final index sy-tabix.

endloop.

Above will work.

Thanks

Tarak

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,824

First sort your internal table by bukrs, kunnr, budat.

At each iteration of the loop wa_final is copied from the internal table, so _final-balance is always initial before calculation.

  • Add a work field for storing current balance
  • Initialize this field AT NEW  kunnr
  • use this field for calculation of new balance and THEN copy it to wa_final (wa_balance = wa_balance + wa_final-dr - wa_final-cr. wa_final-balance = wa_balance.)
  • NO - As you are in a LOOP SY-TABIX is not required
  • NO - This TRANSPORTING option miss the CR/DB fields

Other solution, keep wa_final-balance = wa_final-dr - wa_final-cr and sum the column, no need of the new work field.

Regards,

Raymond

Read only

Former Member
0 Likes
1,824

Hi Purnand,

U are getting the Balance value 0 because of logic.

  wa_final-balance = wa_final-balance + wa_final-dr - wa_final-cr.

When u debug, plz check for 2 things

1. The value of wa_final-dr  and wa_final-cr.

2. The value of wa_final-balance for the last record of the loop.

I'm hope U'll get the answer and able to proceed.

Thanks

Vivek

Read only

Former Member
0 Likes
1,824

Declare a variable outside the loop with a TYPE of  wa_final-balance say TOTAL_BALANCE TYPE wa_final-balance. Now inside the loop just replace the below code

"wa_final-balance = wa_final-balance + wa_final-dr - wa_final-cr"

with

TOTAL_BALANCE = TOTAL_BALANCE + wa_final-dr - wa_final-cr.

wa_final-balance = TOTAL_BALANCE.

Thanks,

Supratik

Read only

Former Member
0 Likes
1,825

Hello,

when your loop processing then it comes 0. you tell this..i think your next row in internal table hold field balance = 0. for that reason , when loop start it takes 0.

Jst E.G:  dr       cr        balance

              100     0            100       =>   this is your internal table....

               30     0               0

     when loop processng it takes 1st row....then endloop. next it takes 2nd row...see balance = 0.

     i think for that reason it takes 0.        

loop at it_final into wa_final.

  if wa_final-shkzg = 'S'.

    wa_final-dr = wa_final-wrbtr.

    else.                                                      DAta: bal type p.

      wa_final-cr = wa_final-wrbtr.

      endif.

   wa_final-balance = bal + wa_final-dr - wa_final-cr.

bal = wa_final-balance.

      modify it_final from wa_Final.

  

endloop.

i think your problem will be solved.........if not then let me know....

Thanks

Sabyasachi

Read only

Former Member
0 Likes
1,824

Dear Purnand,

Please check the below code which is working ok.

  


TABLES: BSID.
SELECTION-SCREEN: BEGIN OF BLOCK B1.
  SELECT-OPTIONS: S_BUKRS FOR BSID-BUKRS,
                  S_KUNNR FOR BSID-KUNNR,
                  S_DATE FOR BSID-BUDAT.
SELECTION-SCREEN: END OF BLOCK B1.

DATA: BEGIN OF IT_BSAD OCCURS 0,
  BUKRS LIKE BSAD-BUKRS,
  KUNNR LIKE BSAD-KUNNR,
  BUDAT LIKE BSAD-BUDAT,
  WRBTR LIKE BSAD-WRBTR,
  BELNR LIKE BSAD-BELNR,
  SHKZG LIKE BSAD-SHKZG,
END OF IT_BSAD.
DATA: BEGIN OF IT_BSID OCCURS 0,
  BUKRS LIKE BSAD-BUKRS,
  KUNNR LIKE BSAD-KUNNR,
  BUDAT LIKE BSAD-BUDAT,
  WRBTR LIKE BSAD-WRBTR,
  BELNR LIKE BSAD-BELNR,
  SHKZG LIKE BSAD-SHKZG,
END OF IT_BSID.
DATA: BEGIN OF IT_FINAL OCCURS 0,
  BUKRS LIKE BSAD-BUKRS,
  KUNNR LIKE BSAD-KUNNR,
  BUDAT LIKE BSAD-BUDAT,
  WRBTR LIKE BSAD-WRBTR,
  BELNR LIKE BSAD-BELNR,
  SHKZG LIKE BSAD-SHKZG,
DR(15) TYPE P DECIMALS 2,
CR(15) TYPE P DECIMALS 2,
BALANCE(15) TYPE P DECIMALS 2,
END OF IT_FINAL.
DATA: WA_FINAL LIKE IT_FINAL.

select bukrs

          kunnr

          budat

          wrbtr

     belnr

     shkzg

     from bsad

     into table it_bsad

     where bukrs in s_bukrs and kunnr in s_kunnr and budat in s_date.


   select bukrs

        kunnr

        budat

        wrbtr

        belnr

        shkzg

     into table it_bsid

     from bsid

*    for all entries in it_bsad

     where bukrs in s_bukrs and kunnr in s_kunnr and budat in s_date.



break-point.

it_final[] = it_bsad[].

append lines of it_bsid to it_final.




loop at it_final into wa_final.

   if wa_final-shkzg = 'S'.

     wa_final-dr = wa_final-wrbtr.

     else.

       wa_final-cr = wa_final-wrbtr.

       endif.



wa_final-balance = wa_final-balance + wa_final-dr - wa_final-cr.


       modify it_final from wa_Final.


endloop.


BREAK-POINT.

Read only

0 Likes
1,824

Hi,

Please use transporting addtion in modify statement and transport only the relevant fields.

Regards,

Kartik

Read only

0 Likes
1,824

Hi,

Use field symbol for work are...

Regards,

Aneesh

Read only

Former Member
0 Likes
1,824

Thank you experts for your suggestions. I am overwhelmed.