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

Dynamic abap

Former Member
0 Likes
963

I need to dynamically calculate amount field based on input parameter.

For example,

if financial period = 03

I need to generate a calculate statement that will add three fields in an internal table

itab-field1 + itab-field2 + itab-field3 = Total amount

Else if financial period = 04

I need to generate a calculate statement that will add three fields in an internal table

itab-field1 + itab-field2 + itab-field3 + itab-field4 = Total amount

How can i generate abap statement dynamically in the program that will do calculation dynamically and perform above calculation.

Thanks in advance .

Sid.

5 REPLIES 5
Read only

Former Member
0 Likes
824

You do not need dynamic code for this - use a DO VARYING loop. See the following sample code:


REPORT  test1.

DATA: l_struc LIKE glt0,
      l_val   LIKE glt0-tsl01,
      l_tot   LIKE glt0-tsl01.

PARAMETERS p_period(2) TYPE n.

START-OF-SELECTION.

  SELECT * FROM glt0 INTO l_struc UP TO 1 ROWS
    where tsl02 <> 0.
  ENDSELECT.

  DO p_period TIMES VARYING l_val FROM l_struc-tsl01 NEXT l_struc-tsl02.
    l_tot = l_tot + l_val.
    write: l_val.
  ENDDO.

  WRITE: / l_tot.

Andrew

Read only

Former Member
0 Likes
824

You can use field symbols.

Here is a sample code which populates amount for a fiscal period range. Hope this helps -

IF S_MONAT-LOW > 1.

l_cnt = 01.

while l_cnt le l_cnt1.

CONCATENATE K_FLD L_CNT K_S INTO L_FLDNM.

condense l_fldnm no-gaps.

assign (l_fldnm) to <fs>.

it_prev_bal-dmbtr = it_prev_bal-dmbtr + <fs>.

clear l_fldnm.

concatenate K_FLD l_cnt k_h into l_fldnm.

condense l_fldnm no-gaps.

assign (l_fldnm) to <fs>.

  • <fs> = <fs> * -1.

IT_PREV_BAL-DMBTR = IT_PREV_BAL-DMBTR - <FS>.

l_cnt = l_cnt + 1.

endwhile. " while l_cnt le l_cnt1

endif. " if p_monat > 1

L_CNT1 has TO VALUE of the period. And we are comparing in the loop if from value is less than or equal to TO VALUE, which will help to extract all fields for each period. This is just a piece of code which you will have to alter as per your requirement.

Read only

Former Member
0 Likes
824

Hi

In this case you can use if statement and elseif statement and check the value of financial period.Write two sum statement.One for if and other for elseif.Both if and elseif sholud be included with in the loop of the internal table.I think this will help you.

Reward if useful.

Regards

Shibin

Read only

0 Likes
824

*&---------------------------------------------------------------------*

*& Report  ZGL_FINANCE

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT ZGL_FINANCE.

TABLES : faglflext.

type-pools : slis.

TYPES : BEGIN OF ST1,

         tsl01 TYPE faglflext-tsl01,

         tsl02 TYPE faglflext-tsl02,

         tsl03 TYPE faglflext-tsl03,

         tsl04 TYPE faglflext-tsl04,

         tsl05 TYPE faglflext-tsl05,

         tsl06 TYPE faglflext-tsl06,

        RACCT TYPE faglflext-RACCT,

         prctr TYPE faglflext-prctr,

         HSL01 TYPE faglflext-hsl01,

         HSL02 TYPE faglflext-hsl02,

         HSL03 TYPE faglflext-hsl03,

         HSL04 TYPE faglflext-hsl04,

         HSL05 TYPE faglflext-hsl05,

         HSL06 TYPE faglflext-hsl06,

         total type kwert,

         total1 TYPE kwert,

END OF st1.

data : it1 TYPE TABLE OF st1,

       wa1 TYPE st1.

data : it_final TYPE TABLE OF sT1,

       wa_final TYPE st1.

data : it_fcat TYPE slis_t_fieldcat_alv,

       wa_fcat TYPE slis_fieldcat_alv.

data : lo_alv TYPE slis_layout_alv.

PERFORM fcat.

SELECTION-SCREEN : begin of block b1 WITH FRAME TITLE text-001.

   select-OPTIONS : s_bukrs for faglflext-rbukrs,

                    s_year for faglflext-ryear,

                    s_prctr  for faglflext-prctr,

                    s_racct for faglflext-racct.

   SELECTION-SCREEN : end of block b1.

START-OF-SELECTION.

select TSL01

   tsl02

   tsl03

   tsl04

   tsl05

   tsl05

   RACCT

   PRCTR

   HSL01

   HSL02

   HSL03

   HSL04

   HSL05

   from faglflext INTO TABLE it1 UP TO 10 ROWS where rbukrs in s_bukrs and ryear in s_year and prctr in s_prctr and racct in s_racct .

  LOOP AT IT1 INTO WA1.

wa_final = wa1.

wa_final-total = wa1-TSL01 + wa1-tsl02 + wa1-tsl03 + wa1-tsl04 + wa1-tsl05 .

WA_FINAL-TOTAL1 = WA1-HSL01 + WA1-hsl02 + wa1-hsl03.

append wa_final to it_final.

CLEAR : WA1, WA_FINAL.

  ENDLOOP.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

  EXPORTING

*   I_INTERFACE_CHECK                 = ' '

*   I_BYPASSING_BUFFER                = ' '

*   I_BUFFER_ACTIVE                   = ' '

    I_CALLBACK_PROGRAM                = 'sy-repid'

*   I_CALLBACK_PF_STATUS_SET          = ' '

*   I_CALLBACK_USER_COMMAND           = ' '

*   I_CALLBACK_TOP_OF_PAGE            = ' '

*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '

*   I_CALLBACK_HTML_END_OF_LIST       = ' '

*   I_STRUCTURE_NAME                  =

*   I_BACKGROUND_ID                   = ' '

*   I_GRID_TITLE                      =

*   I_GRID_SETTINGS                   =

    IS_LAYOUT                         = lo_alv

    IT_FIELDCAT                       = it_fcat

*   IT_EXCLUDING                      =

*   IT_SPECIAL_GROUPS                 =

*   IT_SORT                           =

*   IT_FILTER                         =

*   IS_SEL_HIDE                       =

*   I_DEFAULT                         = 'X'

*   I_SAVE                            = ' '

*   IS_VARIANT                        =

*   IT_EVENTS                         =

*   IT_EVENT_EXIT                     =

*   IS_PRINT                          =

*   IS_REPREP_ID                      =

*   I_SCREEN_START_COLUMN             = 0

*   I_SCREEN_START_LINE               = 0

*   I_SCREEN_END_COLUMN               = 0

*   I_SCREEN_END_LINE                 = 0

*   I_HTML_HEIGHT_TOP                 = 0

*   I_HTML_HEIGHT_END                 = 0

*   IT_ALV_GRAPHICS                   =

*   IT_HYPERLINK                      =

*   IT_ADD_FIELDCAT                   =

*   IT_EXCEPT_QINFO                   =

*   IR_SALV_FULLSCREEN_ADAPTER        =

* IMPORTING

*   E_EXIT_CAUSED_BY_CALLER           =

*   ES_EXIT_CAUSED_BY_USER            =

   TABLES

     T_OUTTAB                          = it_final

* EXCEPTIONS

*   PROGRAM_ERROR                     = 1

*   OTHERS                            = 2

           .

IF SY-SUBRC <> 0.

* Implement suitable error handling here

ENDIF.

*&---------------------------------------------------------------------*

*&      Form  FCAT

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

*  -->  p1        text

*  <--  p2        text

*----------------------------------------------------------------------*

FORM FCAT .

* WA_FCAT-COL_POS  = '8'.

   WA_FCAT-FIELDNAME = 'TSL01'.

   WA_FCAT-SELTEXT_M = 'TSL01'.

   APPEND WA_FCAT TO IT_FCAT.

WA_FCAT-FIELDNAME = 'TSL02'.

   WA_FCAT-SELTEXT_M = 'TSL02'.

   APPEND WA_FCAT TO IT_FCAT.

   WA_FCAT-FIELDNAME = 'TSL03'.

   WA_FCAT-SELTEXT_M = 'TSL03'.

   APPEND WA_FCAT TO IT_FCAT.

   WA_FCAT-FIELDNAME = 'TSL04'.

   WA_FCAT-SELTEXT_M = 'TSL04'.

   APPEND WA_FCAT TO IT_FCAT.

   WA_FCAT-FIELDNAME = 'TSL05'.

   WA_FCAT-SELTEXT_M = 'TSL05'.

   APPEND WA_FCAT TO IT_FCAT.

   WA_FCAT-FIELDNAME = 'TSL06'.

   WA_FCAT-SELTEXT_M = 'TSL06'.

   APPEND WA_FCAT TO IT_FCAT.

WA_FCAT-FIELDNAME = 'RACCT'.

   WA_FCAT-SELTEXT_M = 'RACCT'.

   APPEND WA_FCAT TO IT_FCAT.

WA_FCAT-FIELDNAME = 'PRCTR'.

   WA_FCAT-SELTEXT_M = 'PRCTR'.

   APPEND WA_FCAT TO IT_FCAT.

WA_FCAT-FIELDNAME = 'HSL01'.

   WA_FCAT-SELTEXT_M = 'HSL01'.

   APPEND WA_FCAT TO IT_FCAT.

WA_FCAT-FIELDNAME = 'HSL02'.

   WA_FCAT-SELTEXT_M = 'HSL02'.

   APPEND WA_FCAT TO IT_FCAT.

WA_FCAT-FIELDNAME = 'TOTAL1'.

   WA_FCAT-SELTEXT_M = 'TOTAL1'.

   wa_fcat-do_sum = 'X'.

   APPEND WA_FCAT TO IT_FCAT.

WA_FCAT-FIELDNAME = 'TOTAL'.

   WA_FCAT-SELTEXT_M = 'TOTAL'.

   WA_FCAT-DO_SUM = 'X'.

   APPEND WA_FCAT TO IT_FCAT.

please check this is complete sum alv

rgds

Ashwani sharma

ENDFORM.                    " FCAT

Read only

Former Member
0 Likes
824

This message was moderated.