Workflow – Reading Container Values Using FM and Class

Hello Everyone!👋

Reading Workflow Container Values Using FM and Class

If you’ve worked with workflows, you’ve likely encountered situations where you need to access container values within your program.

Most of the time, we use the function module SAP_WAPI_READ_CONTAINER for this.
However, there’s an alternative approach you can also retrieve container values using the class CL_SWF_RUN_WORKITEM_CONTEXT.

CLASS lcl_workflow_container DEFINITION.
  PUBLIC SECTION.
    METHODS: get_value_using_fm
      IMPORTING iv_wi_id        TYPE sww_wiid
                iv_element_name TYPE swfdname
      EXPORTING ev_value        TYPE any.
    METHODS: get_value_using_method
      IMPORTING iv_wi_id        TYPE sww_wiid
                iv_element_name TYPE swfdname
      EXPORTING ev_value        TYPE any
      RAISING   cx_swf_cnt_container.
ENDCLASS.
CLASS lcl_workflow_container IMPLEMENTATION.
  METHOD get_value_using_fm.
    TYPES: tt_swr_cont TYPE TABLE OF swr_cont WITH DEFAULT KEY.
    DATA(lv_return_code) = VALUE sy-subrc( ).
    DATA(lt_container_values) = VALUE tt_swr_cont( ).
    CALL FUNCTION 'SAP_WAPI_READ_CONTAINER'
      EXPORTING
        workitem_id      = iv_wi_id
      IMPORTING
        return_code      = lv_return_code
      TABLES
        simple_container = lt_container_values.
    IF lv_return_code <> 0.
      RETURN.
    ENDIF.
    ev_value = VALUE #( lt_container_values[ element = iv_element_name ]-value OPTIONAL ).
  ENDMETHOD.
  METHOD get_value_using_method.
    TRY.
        DATA(lo_context) = cl_swf_run_workitem_context=>get_instance( iv_wi_id ).
        DATA(lo_container) = lo_context->if_wapi_workitem_context~get_wf_container( ).
        lo_container->get( EXPORTING name = iv_element_name
        IMPORTING value = ev_value ).
      CATCH cx_swf_cnt_container INTO DATA(container_error).
        RAISE EXCEPTION container_error.
      CATCH cx_root INTO DATA(diaper).
        RAISE EXCEPTION NEW cx_swf_cnt_container( previous = diaper ).
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA lv_wi_id TYPE sww_wiid VALUE '453180'.
  DATA lv_element_name TYPE swfdname VALUE '_WF_INITIATOR'.
  DATA(lv_wf_initiator_fm) = VALUE xubname( ).
  DATA(lv_wf_initiator_mehd) = VALUE xubname( ).
  DATA(lo_wf_container) = NEW lcl_workflow_container( ).
  "Get Element value using FM
  lo_wf_container->get_value_using_fm(
    EXPORTING
     iv_wi_id = lv_wi_id "Workitem ID
     iv_element_name = lv_element_name "Element Name
   IMPORTING
     ev_value = lv_wf_initiator_fm "Value
  ).
  "Get Element value using Method
  lo_wf_container->get_value_using_method(
   EXPORTING
      iv_wi_id = lv_wi_id "Workitem ID
      iv_element_name = lv_element_name "Element Name
   IMPORTING
    ev_value = lv_wf_initiator_mehd "Value
  ).

Comments

Popular posts from this blog

Fiori URL Generation and Navigation Using CL_LSAPI_MANAGER

RAP - Real Time UI Updates Using Event-Driven Side Effects

ABAP - Create CSV Files Easily Using CL_CSV_FACTORY