Sunday, September 16, 2018

Reading plant from Organization levels based on Role name

Bellow code snippet can become handy and save time if you are looking for a way how to get plant/werks assigned to a specific role. By similar code you can read of course other stuff like Shipping point ($VSTEL),  Sales  distribution channel ($VTWEG), Sales organization ($VKORG) etc. The base used FM 'PRGN_1252_READ_ORG_LEVELS' is taken out of transaction PFCG.


DATA:
   lt_level      TYPE STANDARD TABLE OF pt1252,
   lt_plant      TYPE STANDARD TABLE OF werks,  " Final plant itab
   lv_plant_low  TYPE werks,
   lv_plant_high TYPE werks.

FIELD-SYMBOLS:
   <fs_level> LIKE LINE OF lt_level,
   <fs_plant> LIKE LINE OF lt_plant.

CALL FUNCTION 'PRGN_1252_READ_ORG_LEVELS'
  EXPORTING
    activity_group    '/Your_Name_Space/Role_name' 
  TABLES
    org_levels        lt_level
  EXCEPTIONS
    no_data_available 1.

LOOP AT lt_level ASSIGNING <fs_level> WHERE varbl EQ '$WERKS'.

  IF <fs_level>-low IS NOT INITIAL AND <fs_level>-high IS INITIAL ).
    APPEND <fs_level>-low TO lt_plant.

  ELSEIF <fs_level>-low IS NOT INITIAL AND <fs_level>-high IS NOT INITIAL ).
    IF <fs_level>-low < <fs_level>-high.

      lv_plant_low  <fs_level>-low.
      lv_plant_high <fs_level>-high.

      WHILE lv_plant_low <= lv_plant_high.
        APPEND lv_plant_low TO lt_plant.
        lv_plant_low lv_plant_low + 1.
      ENDWHILE.

    ELSE.
      APPEND <fs_level>-low TO lt_plant.
    ENDIF.
  ENDIF.
ENDLOOP.