Skip to content

Latest commit

 

History

History
311 lines (255 loc) · 9.32 KB

File metadata and controls

311 lines (255 loc) · 9.32 KB

Development in Classic ABAP

The starting point is the class CL_SWF_CPWF_API_FACTORY_A4C. Once you have the API object of interfaceIF_SWF_CPWF_API, see the ABAP documentation on supported actions.

Instances can be created with the following snippet:

Sample Code:

Start a SAP Cloud Platform Workflow

TYPES: BEGIN OF ty_context,
         some_property TYPE string,
       END OF ty_context.
 
DATA(lo_cpwf_api) = cl_swf_cpwf_api_factory_a4c...=>get_api_instance( ... ).
 
DATA(ls_context) = VALUE ty_context(
  some_property = 'someValue'
).
DATA(lv_context_json) = lo_cpwf_api->get_start_context_from_data(
  iv_data = ls_context
).
 
DATA(lv_cpwf_handle) = lo_cpwf_api->start_workflow(
  iv_cp_workflow_def_id = '<SAP Workflow Service Process Definition Identifier>'
  iv_context            = lv_context_json
  iv_retention_time     = 30
  iv_callback_class     = '<Callback class for final state handling>'
).

If there is a running process instance in SAP Build Process automation which you would like to handle from within your S/4 Hana Application, you can register the instance in the following way:

Sample Code:

Register a SAP Cloud Platform Workflow

" Get a Instance for the CPWF Integration API
DATA(lo_cpwf_api) = cl_swf_cpwf_api_factory_a4c...=>get_api_instance( ... ).
 
DATA(lv_cpwf_handle) = lo_cpwf_api->register_workflow(
    iv_cp_workflow_inst_id = '<SAP Workflow Service Instance Identifier>'
    iv_retention_time     = 30
    iv_callback_class     = '<Callback class for final state handling>'
).
 
COMMIT WORK.

As shown in the above snippets, a callback class can be provided that allows processing the result of the process. For example, it is possible to read the context of the completed instance.

Sample Code:

Completion callback with reading context

METHOD if_swf_cpwf_callback~workflow_instance_completed.
  TYPES: BEGIN OF ty_context,
           some_result TYPE string,
         END OF ty_context.
 
  TRY.
      DATA(lo_cpwf_api) = cl_swf_cpwf_api_factory_a4c...=>get_api_instance( ... ).
      data(lv_context_json) = lo_cpwf_api->get_workflow_context( iv_cpwf_handle = iv_cpwf_handle ).
 
      DATA ls_context TYPE ty_context.
 
      lo_cpwf_api->get_context_data_from_json(
        EXPORTING
          iv_context = lv_context_json
       IMPORTING
          ev_data = ls_context
      ).
    CATCH cx_swf_cpwf_api INTO DATA(lx_exc).
      " Exception handling
  ENDTRY.
 
  " your business coding
ENDMETHOD.

If SAP Build Process Automationis waiting for an intermediate message event (IME), you can trigger that message by calling the corresponding API, using the following code.

Sample Code:

Raise an intermediate message event

CONSTANTS: lc_cp_workflow_def_id TYPE if_swf_cpwf_api=>cpwf_def_id      VALUE '<Your Workflow Definition ID>',
              lc_api_trigger_uid    TYPE if_swf_cpwf_api=>cpwf_trigger_uid VALUE '<Your API Trigger UID>'.

" Get a Instance for the CPWF Integration API
   DATA(lo_cpwf_api) = cl_swf_cpwf_api_factory...=>get_api_instance( ... ).

" actually raise the event
   lo_cpwf_api->api_trigger(
     EXPORTING
       iv_cpwf_handle   = lv_cpwf_handle  " the BTP process instance handle
       iv_event_def_id  = lc_api_trigger_uid
   ).
 
   COMMIT WORK.

In older releases the API trigger may not yet be supported by the API. Here intermediate message event can be raised through the following code.

Sample Code:

Raise an intermediate message event (deprecated)

TYPES: BEGIN OF ty_example_context,
         value TYPE int4,
       END OF ty_example_context.
 
" Get a Instance for the CPWF Integration API
DATA(lo_cpwf_api) = cl_swf_cpwf_api_factory_a4c...=>get_api_instance( ... ).
 
" provide relevant event context data => in this case a meaningful integer
DATA(lv_event_context_data) = VALUE ty_example_context( value = 4711 ).
DATA(lv_event_context) = lo_cpwf_api->get_context_from_data( iv_data = lv_event_context_data ).
 
" actually raise the event
lo_cpwf_api->raise_event(
  EXPORTING
    iv_cpwf_handle   = lv_cpwf_handle
    iv_event_def_id  = '<Event Definition Identifier>'
    iv_event_context = lv_event_context
).
 
COMMIT WORK.

Note:

This approach works only for "old" workflow management process definitions being deployed from SAP Business Application Studio.

It is also possible to synchronously retrieve data about the processes. It can be accessed through the interface IF_SWF_CPWF_API, which extends the core API IF_SWF_CPWF_CAPI.

For example, you can query the current lifecycle status of processes.

Sample Code:

Read Workflow Instances

" Get a Instance for the CPWF Integration API
DATA(lo_cpwf_api) = cl_swf_cpwf_api_factory...=>get_api_instance( ... ).
DATA(lo_wf_inst) = lo_cpwf_api->if_swf_cpwf_capi~get_workflow_instances_api( ).
 
DATA(lt_workflow_instance) = lo_wf_inst->query_instances(
  iv_definition_id = '<SAP Workflow Service Process Definition Identifier>'
  iv_status = if_swf_cpwf_capi_wf_insts=>cs_status-running
).

Or retrieve the current context:

Sample Code:

Read Workflow Instance Context

TYPES: BEGIN OF ty_context,
         attr1 TYPE string,
         attr2 TYPE int4,
         attr3 TYPE string,
       END OF ty_context.     
 
" Get a Instance for the CPWF Integration API
DATA(lo_cpwf_api) = cl_swf_cpwf_api_factory...=>get_api_instance( ... ). 
DATA(lo_cp_json) = lo_cpwf_api->get_json_converter( ).
DATA(lo_wf_inst_ctxt) = lo_cpwf_api->if_swf_cpwf_capi~get_wf_instance_context_api( ).
  
DATA ls_context TYPE ty_context.
lo_wf_inst_ctxt->get_instance_context( EXPORTING iv_cpwf_handle = <cpwf_handle> io_cp_json = lo_cp_json IMPORTING data = ls_context ).

As shown in the code there is also a JSON converter available to simplify the processing of the context.

To retrieve only the relevant parts of the process context, the APIs IF_SWF_CPWF_API=> GET_WORKFLOW_CONTEXT and IF_SWF_CPWF_CAPI_WF_INST_CTXT=> GET_INSTANCE_CONTEXT now support a new parameter IV_CONTEXT_PATH. The parameter accepts any JSON path expression. This JSON path expression is forwarded to the workflow API of SAP Build Process Automation. The SAP Build Process Automation Workflow Service API uses the expression to returns then only the specific section of the context instead of the entire data.

The following examples show how the IV_CONTEXT_PATH parameter can be used.

Sample Code:

Sample Context

{
    "decision_travelRequestApproved_2": {
        "outcome": "APPROVED"
    },
    "startEvent": {
        "travelid": "1",
        "createdby": "UserX",
        "createdbymail": "userx@sap.com",
        "enddate": "2025-02-28",
        "startdate": "2025-02-01",
        "destination": "Far Far Away"
    },
    "action_post_ProcessCompleted_2": {
        "result": {
            "notificationProcessed": true
        }
    },
    "custom": {
        "approver": "userx@sap.com",
        "outcome": "APPROVED",
        "destinations": [
            {
                "date": "2025-02-01",
                "location": "here"
            },
            {
                "date": "2025-02-12",
                "location": "there"
             }   
        ]
    },
    "decision_determineRecipient_1": {
        "Approver": "userx@sap.com"
    },
    "form_approveTravelRequest_1": {
        "travelID": 1,
        "requestor": "UserX",
        "description_1": "Far Far Away",
        "startDate": "2025-02-01",
        "endDate": "2025-02-28"
    }
}

Sample Code:

Get Context with JSONPath "custom"

{
    "approver": "userx@sap.com",
    "outcome": "APPROVED"
    "destinations": [
        {
            "date": "2025-02-01",
            "location": "here"
        },
        {
            "date": "2025-02-12",
            "location": "there"
        }  
    ]
}

Sample Code:

Get Context with JSONPath "custom.outcome"

"APPROVED"

Sample Code:

Get Context with JSONPath "custom.outcome.destinations[1]"

{
    "date": "2025-02-12",
    "location": "there"
}

Sample Code:

Get Context with JSONPath "custom.outcome.destinations[1].location"


"here"