SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Create and Implement the Workflow Handler Class

Create and implement the workflow handler class to trigger the workflow and handle callbacks from SAP Build Process Automation to receive workflow completion notifications and outcome.

Overview

🎓 beginner 15 min. SAP BTP ABAP EnvironmentIntermediateABAP DevelopmentSAP Business Technology PlatformLicensen

You will learn

  • How to create a workflow handler class.
  • How to enable it to receive callbacks.
  • How to call RAP Façade for SBPA workflow trigger.
  • Methods to use to get trigger data and process callbacks.
Ruchi Dobriyal R Ruchi Dobriyal November 22, 2024
Created by September 20, 2024
Contributors

Prerequisites

Prerequisites

Steps

Intro

Once you have created a basic RAP Travel Application by following steps in tutorial Generate a RAP Business Service with the wizard. You would now create a new class in the application package to handle workflow related implementations i.e., method to create workflow trigger and method to handle callbacks to receive outcome of workflow from SAP Build Process Automation once the process is completed.

In case of Travel Approval Workflow, the callback applies the accept / reject decision of the workflow to the Accept Travel /Reject Travel statuses of the related travel instance.

Step 1 Create a workflow handler class

The workflow handler class will contain all the method implementations, constants, interface data types to exchange information with SAP Build Process Automation for workflow.

To create the class, follow the below steps:

  1. Right click on your Package and select New > ABAP Class

    ABAPClass
    ABAPClass

  2. Enter the below values to create the class.

    Name: ZCL_R_RAP_WORKFLOW_####

    Description: Workflow Handler Class

  3. Click Add to enter Interface and search for callback Interface IF_SWF_CPWF_CALLBACK.

    Click OK once found.

    Interface
    Interface

  4. Click Next. Select the transport request for your application and click Finish.

  5. Class is now created. You can see that the interface addition came with the method workflow_instance_completed which would be triggered during callback post completion of workflow in SAP Build Process Automation.

    In the later steps we would implement this method to receive workflow decision/notification.

    CallbackClass
    CallbackClass

    Thus to enable a class to be callback class we need to add Interface IF_SWF_CPWF_CALLBACK to it.

    IF_SWF_CPWF_CALLBACK is the interface required to handle callbacks. The method WORKFLOW_INSTANCE_COMPLETED of this interface is triggered in background once the Action Workflow Completion Notification is triggered in SAP Build Process Automation Post Approval Form is acted upon by the Approver (Accepted/Rejected).

    To troubleshoot/debug this method you would use the Background workflow user SAP_WFRT.

Enrich the class definition with Constants and Data Types and Methods

Define the class artifacts required for workflow processing.

  1. Define Context (Interface) Data type in the private section of the class. Insert the below code.

    This should be same as the data type context structure declare in SAP Build Process Automation.

    ABAP
    Interface data type for information exchange.
      TYPES: BEGIN OF context,
             travelid      TYPE string,
             agency_name   TYPE string,
             booking_fee   TYPE string, 
             Currency_code TYPE string,
             Customer_name TYPE string,
             Total_price   TYPE string,
           END OF context,
           BEGIN OF type_context,
             travel_context TYPE context,
           END OF type_context.

    Since the Datatype identifier name created in SAP Build Process Automation is TravelContext thus we have also declared the structure name as travel_context. How the name conversion works when we convert from ABAP data to JSON format will be explained in detail as we go through the tutorial.

    JJSONFormat
    JJSONFormat

    Please note, Context (Interface Data) must have the same field data types and names on both sides of the Interface. Context is case sensitive, so any deviation would lead to error.

    alt text
    alt text
    HTTP request for use case START_WI failed (HTTP code 422, Reason 'Unprocessable Entity')

  2. Get the workflow definition ID from SAP Build Process Automation.

    a. Login to SAP Build Process Automation.

    b. Navigate to Monitoring > Processes and Workflows

    workflows
    workflows

    c. Choose your project name from the drop down > Select the project name.

    Copy the entire text in ID highlighted below, that is your Workflow Definition ID.

    WorkflowDefinition
    WorkflowDefinition

  3. Define Constants in the private section of the class. Insert the below code.

    ABAP
        Constants for workflow,
        CONSTANTS:
           BEGIN OF travel_status, 
             open     TYPE c LENGTH 1 VALUE 'O', "Open,
             accepted TYPE c LENGTH 1 VALUE 'A', "Accepted,
             rejected TYPE c LENGTH 1 VALUE 'X', "Rejected,
             waiting  TYPE c LENGTH 1 VALUE 'W', "Awaiting Approval,
           END OF travel_status,
           travel_wf_defid      TYPE if_swf_cpwf_api=>cpwf_def_id_long  VALUE 'euXX-XXXX.XXXX-XXXXX-XXXX-XXXXXXX-					XXXX.rap110travelworkflow.travelApprovalProcessing', " Replace this value with your workflow definition id.
           wf_retention_days    TYPE if_swf_cpwf_api=>retention_time VALUE '30',
           callback_class       TYPE if_swf_cpwf_api=>callback_classname VALUE  'ZCL_R_RAP_WORKFLOW_XXXX', " Replace this with your callback class
           consumer             TYPE string VALUE 'DEFAULT'.

    Do not forget to replace the values for workflow definition ID & Callback class values for your implementation.

    Constant NameUseValues
    Travel_statusTo store the allowed value of the overall status of a Travel instancePossible values can be Open (O), Approved (A), rejected (R) and Awaiting Approval (W).
    wf_retention_daysRetention time is the time we keep the database entry post completion of Workflow instance. After the retention days are passed the database entry will be deleted automatically.
    In case the retention time is set to 0 the entry would not be deleted automatically but would require an API call for deletionSet the retention days to 30
    callback_classClass we created with callback interface to be used while Registering the SAP Build Process Automation Workflow using RAP FacadeSet the Value to ZCL_R_RAP_WORKFLOW_XXXX
    ConsumerTells the information on where to trigger the workflow.Set the Value to DEFAULT
    In the Communication Arrangement for SAP Build Process Automation we have set the consumer type (Workflow) as defaultAs we need to execute the workflow in SBPA
    travel_wf_defidIs the Definition ID of the workflow we created in SBPASet the value to the Workflow definition ID retrieved from SAP Build Process Automation.

    RAPWorkflow
    RAPWorkflow

  4. Define Method to trigger workflow in public section.

    The method interface will consist of all the travel_context fields declared in the datatype.

    It is declared in the public section of the class so that it can be called from the behaviour definition class of the RAP Business Object.

    RAPBusinessObject
    RAPBusinessObject

  5. Click on Quick Assist available on the left side of the newly defined method > Click Add implementation for trigger workflow

    TriggerWorkflow
    TriggerWorkflow

  6. This will create the method in the implementation section of the class as shown below.

    ImplementationClass
    ImplementationClass

  7. Activate

    alt text
    alt text
    the class.

Step 2 Implement methods to trigger workflow in the Class Implementation Section.
+
Step 3 Implement methods to handle callback in the Class Implementation Section.
+
Step 4 Test yourself
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 4
1. Create a workflow handler class 2. Implement methods to trigger workflow in the Class Implementation Section. 3. Implement methods to handle callback in the Class Implementation Section. 4. Test yourself

Learn more →