Sample Expert Routine ABAP Code

  • by Sam Gassem, Senior Business Consultant/BW Lead, Rural Sourcing, Inc.
  • September 24, 2013

This sample ABAP code for an expert routine is provided by Sam Gassem, who wrote the article “What Every BW/BI Developer Needs to Know about ABAP in BW.” Sam also provided a video demo of a start and end routine.

An expert routine is a routine in transformation that is itself the transformation, In other words it contains start, end, and actual transformations. Following is sample ABAP code in an expert routine. As you can see, custom codes are written between *$*$ begin of routine - insert your code only below this line       *-*

    ... "insert your code here and *$*$ end of routine - insert your code only before this line         *-*

Also you can see <SOURCE_FIELDS> and RESULT_FIELDS are declared by the system automatically and they cannot be changed.

The code below loops at the source_package, does calculation and transformation, then populates the result_package which, in turn, puts the data being saved into an InfoProvider.

 

  METHOD expert_routine.
*=== Segments ===

    FIELD-SYMBOLS:
      <SOURCE_FIELDS>    TYPE _ty_s_SC_1.

    DATA:
      RESULT_FIELDS      TYPE _ty_s_TG_1.

*$*$ begin of routine - insert your code only below this line        *-*
    ... "insert your code here
    Data: gw_result_package TYPE _ty_s_SC_1.
    Data: gw_result_package1  TYPE _ty_s_TG_1.
    DATA: wa TYPE /BIC/AZSD_DFC00.
    DATA:
    m10          TYPE /BIC/OIZSFC_MN10,
    m11          TYPE /BIC/OIZSFC_MN11,
    m12          TYPE /BIC/OIZSFC_MN12,
    m1           TYPE /BIC/OIZSFC_MON1,
    m2           TYPE /BIC/OIZSFC_MON2,
    m3           TYPE /BIC/OIZSFC_MON3,
    m4           TYPE /BIC/OIZSFC_MON4,
    m5           TYPE /BIC/OIZSFC_MON5,
    m6           TYPE /BIC/OIZSFC_MON6,
    m7           TYPE /BIC/OIZSFC_MON7,
    m8           TYPE /BIC/OIZSFC_MON8,
    m9           TYPE /BIC/OIZSFC_MON9,
    version      TYPE  C LENGTH 6,
    fiscyear     TYPE /BI0/OIFISCVARNT,
    fiscalyear   TYPE /BI0/OIFISCYEAR,
    plant        TYPE /BI0/OIPLANT,
    material     TYPE /BI0/OIMATERIAL.

***************************************
    data: versiona     TYPE  C LENGTH 6,
          year         TYPE C length 4,
          plantA       type /BI0/OIPLANT,
          materialA    TYPE /BI0/OIMATERIAL.
    LOOP at SOURCE_PACKAGE into gw_result_package.
      version = gw_result_package-/BIC/ZVERSION.
      plantA = gw_result_package-plant.
      MaterialA = gw_result_package-Material.
      year = gw_result_package-fiscyear.
      versiona = VERSION - 1.
      if version = '01'.
        year = year - 1.
        versionA = '10'.
      endif.
      if version = '02'.
        year = year - 1.
        versionA = '11'.
      endif.
      if version = '03'.
        year = year - 1.
        versionA = '12'.
      endif.
**************************************
*Load previous version data************************
      select * from /BIC/AZSD_DFC00 into wa where
        /BIC/ZVERSION = VERSIONa and
        plant = plantA and
        FISCYEAR = year and
        Material = MaterialA.
        if sy-subrc = 0.
          gw_result_package1-plant = wa-plant.
          gw_result_package1-material = wa-material.
          gw_result_package1-FISCVARNT = 'K4'.
          gw_result_package1-fiscyear = wa-fiscyear.
          gw_result_package1-/BIC/ZVERSION = version.
          gw_result_package1-/BIC/ZSFC_MoN1 = wa-/BIC/ZSFC_MoN1.
          gw_result_package1-/BIC/ZSFC_MoN2 = wa-/BIC/ZSFC_MoN2.
          gw_result_package1-/BIC/ZSFC_MoN3 = wa-/BIC/ZSFC_MoN3.
          gw_result_package1-/BIC/ZSFC_MoN4 = wa-/BIC/ZSFC_MoN4.
          gw_result_package1-/BIC/ZSFC_MoN5 = wa-/BIC/ZSFC_MoN5.
          gw_result_package1-/BIC/ZSFC_MoN6 = wa-/BIC/ZSFC_MoN6.
          gw_result_package1-/BIC/ZSFC_MoN7 = wa-/BIC/ZSFC_MoN7.
          gw_result_package1-/BIC/ZSFC_MoN8 = wa-/BIC/ZSFC_MoN8.
          gw_result_package1-/BIC/ZSFC_MoN9 = wa-/BIC/ZSFC_MoN9.
          gw_result_package1-/BIC/ZSFC_MN10 = wa-/BIC/ZSFC_MN10.
          gw_result_package1-/BIC/ZSFC_MN11 = wa-/BIC/ZSFC_MN11.
          gw_result_package1-/BIC/ZSFC_MN12 = wa-/BIC/ZSFC_MN12.

          append gw_RESULT_PACKAGE1 to RESULT_PACKAGE.
        endif.
      ENDSELECT.
      clear gw_RESULT_PACKAGE1.
    endloop.


***************************************************
*Load current File

    LOOP at SOURCE_PACKAGE into gw_result_package.
      m10 = gw_result_package-/BIC/ZSFC_MN10.
      m11 = gw_result_package-/BIC/ZSFC_MN11.
      m12 = gw_result_package-/BIC/ZSFC_MN12.
      m1 = gw_result_package-/BIC/ZSFC_MoN1.
      m2 = gw_result_package-/BIC/ZSFC_MoN2.
      m3 = gw_result_package-/BIC/ZSFC_MoN3.
      m4 = gw_result_package-/BIC/ZSFC_MoN4.
      m5 = gw_result_package-/BIC/ZSFC_MoN5.
      m6 = gw_result_package-/BIC/ZSFC_MoN6.
      m7 = gw_result_package-/BIC/ZSFC_MoN7.
      m8 = gw_result_package-/BIC/ZSFC_MoN8.
      m9 = gw_result_package-/BIC/ZSFC_MoN9.
      version = gw_result_package-/BIC/ZVERSION.
      fiscalyear = gw_result_package-FISCYEAR.
      plant = gw_result_package-PLANT.
      material = gw_result_package-MATERIAL.

      gw_result_package1-material = material.
      gw_result_package1-plant = plant.
      gw_result_package1-FISCVARNT = 'K4'.
      gw_result_package1-fiscyear = fiscalyear.
      gw_result_package1-/BIC/ZVERSION = version.
*
      if m1 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN1 = m1.
      endif.
      if m2 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN2 = m2.
      endif.
      if m3 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN3 = m3.
      endif.
      if m4 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN4 = m4.
      endif.
      if m5 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN5 = m5.
      endif.
      if m6 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN6 = m6.
      endif.
      if m7 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN7 = m7.
      endif.
      if m8 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN8 = m8.
      endif.
      if m9 ne 0.
        gw_result_package1-/BIC/ZSFC_MoN9 = m9.
      endif.
      if m10 ne 0.
        gw_result_package1-/BIC/ZSFC_MN10 = m10.
      endif.
      if m11 ne 0.
        gw_result_package1-/BIC/ZSFC_MN11 = m11.
      endif.
      if m12 ne 0.
        gw_result_package1-/BIC/ZSFC_MN12 = m12.
      endif.

***Get previous Values if month is blanck in the file*******
      select * from /BIC/AZSD_DFC00 into wa where
        plant = gw_result_package-PLANT and
        material = gw_result_package-MATERIAL and
        fiscyear = gw_result_package-FISCYEAR and
        /BIC/ZVERSION = VERSIONa.

          if m1 = 0.
            gw_result_package1-/BIC/ZSFC_MoN1 = wa-/BIC/ZSFC_MoN1.
          endif.
          if m2 = 0.
            gw_result_package1-/BIC/ZSFC_MoN2 = wa-/BIC/ZSFC_MoN2.
          endif.
          if m3 = 0.
            gw_result_package1-/BIC/ZSFC_MoN3 = wa-/BIC/ZSFC_MoN3.
          endif.
          if m4 = 0.
            gw_result_package1-/BIC/ZSFC_MoN4 = wa-/BIC/ZSFC_MoN4.
          endif.
          if m5 = 0.
            gw_result_package1-/BIC/ZSFC_MoN5 = wa-/BIC/ZSFC_MoN5.
          endif.
          if m6 = 0.
            gw_result_package1-/BIC/ZSFC_MoN6 = wa-/BIC/ZSFC_MoN6.
          endif.
          if m7 = 0.
            gw_result_package1-/BIC/ZSFC_MoN7 = wa-/BIC/ZSFC_MoN7.
          endif.
          if m8 = 0.
            gw_result_package1-/BIC/ZSFC_MoN8 = wa-/BIC/ZSFC_MoN8.
          endif.
          if m9 = 0.
            gw_result_package1-/BIC/ZSFC_MoN9 = wa-/BIC/ZSFC_MoN9.
          endif.
          if m10 = 0.
            gw_result_package1-/BIC/ZSFC_MN10 = wa-/BIC/ZSFC_MN10.
          endif.
          if m11 = 0.
            gw_result_package1-/BIC/ZSFC_MN11 = wa-/BIC/ZSFC_MN11.
          endif.
          if m12 = 0.
            gw_result_package1-/BIC/ZSFC_MN12 = wa-/BIC/ZSFC_MN12.
          endif.
      ENDSELECT.
**********************************************************************
      append gw_RESULT_PACKAGE1 to RESULT_PACKAGE.

      clear m1.
      clear m2.
      clear m3.
      clear m4.
      clear m5.
      clear m6.
      clear m7.
      clear m8.
      clear m9.
      clear m10.
      clear m11.
      clear m12.
      CLEAR gw_RESULT_PACKAGE1.

    endloop.

*$*$ end of routine - insert your code only before this line         *-*
  ENDMETHOD.                    "expert_routine
MENTATION

Sam Gassem

Sam Gassem is senior business consultant and SAP BW lead at Rural Sourcing, Inc. (RSI). He has 13 years of experience on SAP NetWeaver BW. RSI is a leader in domestic sourcing, a cost-effective, on-shore alternative to the traditional model for IT outsourcing. RSI specializes in software development and support and maintenance for critical business applications. Its development centers are located in second- and third-tier cities across the United States. RSI was founded in 2004 by the former CIO of Baxter Healthcare with the intention of bringing jobs to areas of the United States in which historically the job market was dominated by agriculture and manufacturing.

See more by this author

You may contact the author at sgassem@gmail.com.

 

If you have comments about this article or publication, or would like to submit an article idea, please contact the editor.


Comments

No comments have been submitted on this article. 


Please log in to post a comment.

To learn more about subscription access to premium content, click here.