ABAP : Dynamic ITAB for Ranges

Example of ABAP Code Dynamic Internal Table - Ranges
*---------------------------------------------------------------------*
*      Form  f_ranges_str

*      By Oka Wirasatha
*---------------------------------------------------------------------*


form F_RANGES_STR
  using FV_FIELD
        FV_SIGN FV_OPTION FV_LOW FV_HIGH.
  type-pools:
    ABAP.

  data:
    GR_STRUCTDESCR    type ref to CL_ABAP_STRUCTDESCR,
    GR_TABLEDESCR     type ref to CL_ABAP_TABLEDESCR,
    GR_DATADESCR      type ref to CL_ABAP_DATADESCR,
    GR_TYPEDESCR      type ref to CL_ABAP_TYPEDESCR,
    GT_COMPONENTS     type ABAP_COMPONENT_TAB,
    GW_COMPONENT      type line of ABAP_COMPONENT_TAB,
    GR_WA             type ref to DATA,
    GR_TAB            type ref to DATA,
    LV_TEXT(20).

  field-symbols: <fs_wa> type any,
                 <fs_val> type any,
                 <fs_tab> type TABLE.
* determine components of structure -> GT_COMPONENTS
  move 'SIGN' to GW_COMPONENT-NAME.
  GW_COMPONENT-TYPE ?= CL_ABAP_ELEMDESCR=>GET_C( P_LENGTH = 1 ).
  insert GW_COMPONENT into table GT_COMPONENTS.

  move 'OPTION' to GW_COMPONENT-NAME.
  GW_COMPONENT-TYPE ?= CL_ABAP_ELEMDESCR=>GET_C( P_LENGTH = 2 ).
  insert GW_COMPONENT into table GT_COMPONENTS.

  move 'LOW' to GW_COMPONENT-NAME.
  GW_COMPONENT-TYPE ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( FV_FIELD ).
  insert GW_COMPONENT into table GT_COMPONENTS.

  move 'HIGH' to GW_COMPONENT-NAME.
  GW_COMPONENT-TYPE ?= CL_ABAP_ELEMDESCR=>DESCRIBE_BY_NAME( FV_FIELD ).
  insert GW_COMPONENT into table GT_COMPONENTS.

* get structure descriptor -> GR_STRUCTDESCR
  GR_STRUCTDESCR ?= CL_ABAP_STRUCTDESCR=>CREATE( GT_COMPONENTS ).

* create work area of structure GR_STRUCTDESCR -> GR_WA
  create data GR_WA type handle GR_STRUCTDESCR.
  assign GR_WA->* to <fs_wa>.

  GR_DATADESCR ?= GR_STRUCTDESCR.
  GR_TABLEDESCR ?= CL_ABAP_TABLEDESCR=>CREATE( GR_DATADESCR ).

* Create dynmaic internal table
  create data GR_TAB type handle GR_TABLEDESCR.
  assign GR_TAB->* to <fs_tab>.

  assign component 'SIGN' of structure <fs_wa> to <fs_val>.
  = FV_SIGN.
  assign component 'OPTION' of structure <fs_wa> to <fs_val>.
  = FV_OPTION.
  assign component 'LOW' of structure <fs_wa> to <fs_val>.
  = FV_LOW.
  assign component 'HIGH' of structure <fs_wa> to <fs_val>.
  = FV_HIGH.

  insert <fs_wa> into table <fs_tab>.

  loop at T_ITEM.
    concatenate 'T_ITEM-' FV_FIELD into LV_TEXT.
    assign (LV_TEXT) to <fs_val>.
    if not <fs_val> in <fs_tab>.
      delete T_ITEM.
    endif.
  endloop.
endform.                    "f_ranges_str

Comments

Popular posts from this blog

Step by step Create SAP LIS Report

Step by Step SAP Splitting Document

New GL Manual Update FAGLFLEXA & FAGLFLEXT