Implementing BAdIs

Objective

After completing this lesson, you will be able to implement BAdIs

BAdI Implementation

Further important BAdIs:

  • Besides the update BAdI there are some more available to configure and control the Workitem processing in MyInbox:
    • BAdI for Actions and Displayed Information: WF_TASK_SUPPORT (since ECC).
    • BAdI for Attachments: WF_ATTACHMENT_PROVIDER (since 1809).
    • BAdI for Comments: WF_COMMENT_PROVIDER (availability of BAdI comment attachments ABAP Platform Cloud 2102).
    • BAdI to filter forwarding in MyInbox: WF_WI_FORWARD (new one available ABAP Platform Cloud 2105 SWF_WI_FORWARD_AGENTS_GET).
  • See also the course appendix with a summary of BAdIs or this blog.

To Implement the BAdI for Task Outcomes

To handle a decision that is made on the task we need to configure and implement a BAdI that will contain the logic for doing this. The BAdI class needs to contain the same logic for completing the task that is used in the assigned BOR object dialog method. These dialog methods are all slightly different, so you need to take it on a case-by-case basis when implementing the decision BAdI. Understanding how the step works in the workflow and how the dialog method is implemented is key in this process.

To implement the BAdI for Task Outcomes:

  1. Create the BAdI configuration and class for decision handling. To do so, follow steps a through u.

    1. Remain in the back-end ZME system, enter transaction /NSE18, and press Enter on your keyboard.

    2. The enhancement spot for getting business context data is /IWWRK/ES_WF_WI_BEFORE_UPD_IB. Enter that into the Enhancement Spot field then choose the Display button:

      Screenshot of Display Enhancement Spot

      You should now see the following:

      The image shows the screenshot of Enhancement Spot Display
    3. Expand the BAdI definition so you see the following:

      Screenshot of Expand BAdI Definition
    4. Right-click the Implementations node and choose Create BAdI Implementation in the context menu.

      Screenshot of Create BAdI Implementation
    5. In the dialog box, choose the Create icon in the bottom right-hand corner.

      Screenshot of Create Icon
    6. In the Create Enhancement Implementation window, enter the details as shown below (replace 00 with your number) and choose the green check mark.

      Screenshot of Enhancement Implementation Details
    7. If you receive the Create Object Directory Entry dialog box, enter $TMP as the package and choose the Save button.

      Screenshot of Create Object Directory Entry
    8. You will be brought back to the Select or Create Enhancement Implementation dialog box. Make sure your newly created enhancement implementation is selected as shown below and choose the green check mark.

      Screenshot of Select or Create Enhancement Implementation
    9. The Create BAdI Implementation dialog box is displayed. Enter the details as shown below, and choose the green check mark.

      Possible Entries in the Create BAdI Implementation dialog box

      FieldValue
      BAdI ImplementationZ_BADI_ABSENCE_FORM
      DescriptionZ BADI Implementation Absence Form
      Implementing ClassZ_IC_ABSENCE_FORM
      Screenshot of Create BAdI Implementation
    10. In the Create Object Directory Entry dialog box, choose the Save button.

      Screenshot of Save Button

      You should now see the following:

      Screenshot of Enhancement Implementation
    11. Enter change mode by choosing the Display/Change button.

      Screenshot of Display/Change
    12. Expand the BAdI implementation you just created and double-click Filter Val..

      Screenshot of Filter Values
    13. On the right-hand side, you will see the filter details view displayed. Choose the Create Filter Combination button (if it is not enabled, choose the Display/Change button again).

      Screenshot of Create Filter Combination
    14. In the dialog box, select both WORKFLOW_ID and STEP_ID by holding down the Shift button on your keyboard and choosing the gray box next to each. Then choose the green check box.

      Screenshot of Choose Filter

      You should now see the following:

      Screenshot of Filter Values
    15. Double-click in the Value 1 column of the STEP_ID row. You should see the following dialog box:

      Screenshot of Change Filter Value
    16. Enter 48 in the Value 1 field (48 is the step ID for Approve absence notification) and set Comparator 1 to "=". The filter should now look as follows:

      Screenshot of Change Filter Value Filled
    17. Choose Continue (the green check mark).

    18. Now set the WORKFLOW_ID filter by double-clicking the Value 1 setting and entering a value of WSXXXXXXXX (where the Xs are replaced with your assigned number) and set Comparator 1 to =.

    19. Choose Continue (the green check mark).Your filters should now be set as shown below:

      Screenshot of Filter Values Changed
    20. Activate your BAdI by choosing the Activate button.

      Screenshot of Activate BAdI
    21. In the Inactive Objects for FIORI window, choose the green check mark.

      Screenshot of Inactive Objects for FIORI

      Note

      You might see a warning telling you that the method that holds the actual implementation code has not been implemented. This will be done in the following section.

      Congratulations. You have created a BAdI that can be called to access the business context data of the absence approval notification step.

  2. Implement BAdI class for decision handling.

    The BAdI class implementation for decision handling is going to be dependent on the workflow, the task, and the BOR object dialog method that the task is bound to. You will need to examine all of those elements to determine the necessary implementation. In any case, you will be making use of the SAP_WAPI* business workflow functions.

    1. In your enhancement implementation, switch to the Enh. Implementation Elements tab and double-click the Implementing Class node.

      Screenshot of Implementing Class
    2. Double-click the method on the right-hand side.

      Screenshot of Method
    3. In the Create Method Implementation dialog box, choose Yes.

      Screenshot of Create Method Implementation
    4. Switch to change mode and add the required code, overwriting the existing code in the method.

      Screenshot of Change Mode
    5. Overwrite the method and endmethod lines in the existing code. Here is the code being implemented in the method:

      Code Snippet
      123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
      DATA ls_object TYPE swr_obj_2. DATA lv_objtype TYPE swr_struct-object_typ. DATA lv_objkey TYPE SWR_STRUCT-OBJECT_KEY. DATA lv_retcode TYPE sy-subrc. DATA lt_container TYPE TABLE OF swr_cont. DATA ls_container_line TYPE swr_cont. DATA FORMNUMBER TYPE SWXFORMABS-FORMNUMBER. DATA ls_formabs TYPE swxformabs. "Access the workflow data CALL FUNCTION 'SAP_WAPI_GET_OBJECTS' EXPORTING workitem_id = is_wi_details-wi_id IMPORTING leading_object_2 = ls_object. "Get the formnumber which is the key to the absence table MOVE ls_object-instid TO formnumber. "Select the details of the absence from the table SWXFORMABS select single * from swxformabs into ls_formabs where formnumber = formnumber. "Read the workflow's container data CALL FUNCTION 'SAP_WAPI_READ_CONTAINER' EXPORTING workitem_id = is_wi_details-wi_id IMPORTING return_code = lv_retcode TABLES simple_container = lt_container. " Check which decision was selected and set the data " values appropriately CASE iv_decision_key. WHEN 0001. "Approved ls_container_line-value = 'A'. ls_formabs-procstate = 'A'. WHEN 0002. "Rejected ls_container_line-value = 'R'. ls_formabs-procstate = 'R'. ENDCASE. "_WI_RESULT is what the workflow keys off to determine "which path to follow - Approve or Reject path ls_container_line-element = '_WI_RESULT'. "Modify the workflow's container data - we are updating the row that "holds _WI_RESULT which will be in the second row of the table MODIFY lt_container INDEX 2 FROM ls_container_line TRANSPORTING value. "Write the updated container back to the workflow engine CALL FUNCTION 'SAP_WAPI_WRITE_CONTAINER' EXPORTING WORKITEM_ID = is_wi_details-wi_id DO_COMMIT = 'X' IMPORTING RETURN_CODE = lv_retcode TABLES SIMPLE_CONTAINER = lt_container. IF lv_retcode NE 0. "Handle error ENDIF. "Update the Absence table with the updated data ls_formabs-approvdate = sy-datum. ls_formabs-approvby = sy-uname. update swxformabs from ls_formabs. "Complete the task CALL FUNCTION 'SAP_WAPI_WORKITEM_COMPLETE' EXPORTING WORKITEM_ID = is_wi_details-wi_id. "This task requires a confirm to be fully completed "and start the next step in the workflow CALL FUNCTION 'SAP_WAPI_WORKITEM_CONFIRM' EXPORTING WORKITEM_ID = is_wi_details-wi_id.

    6. Save and activate your changes.

      Screenshot of Activate Code
    7. In the Inactive Objects for Fiori dialog box, choose the green check mark.

      Screenshot of My Inbox Tile
  3. Test the scenario: approve from SAP Fiori Approve Requests.

    1. Launch the SAP Fiori launchpad by going to Google Chrome and choosing the Launchpad quick link. If prompted, enter your logon credentials.

    2. Choose the My Inbox tile we created in an earlier step.

    3. Choose the Approve icon.

      Screenshot of Approve Icon
    4. Make a comment and choose Submit.

    5. You should receive a successful message and the workflow item should be removed from the list. If not, clear the browser history and close, then reopen the launchpad.

How to Implement a BAdI for the My Inbox Tile

In this demonstration, the trainer will demonstrate the steps of the following exercise. So, for the steps and data of this demonstration, refer to the exercise Implement a BAdI for the My Inbox Tile.

Implement a BAdI for the My Inbox Tile

Business Scenario

As workflow expert, you are required to adapt the possible actions in My Inbox for a specific Task. In this exercise, you will implement a BAdI to add further applications to that launchpad.

Note

To successfully perform this exercise, it is required that you have completed exercise Create a Workflow.

Note

In this exercise, when a value or an object name includes ##, replace ## with the number that your instructor assigned to you.

Task 1: Create a new Customer-Specific Task

Steps

  1. Create a new customer-specific task.

    Use the following data:

    FieldValue
    AbbreviationDECISION##
    NameGeneric decision task##

    1. If not already there, log on to the SAP S/4HANA back-end server (S4D).

    2. Start Transaction SWDD.

    3. Open your Workflow.

      See the ID in the exercise Create a Workflow.

    4. Go to your decision task.

    5. Create a customer-specific task based on the generic Decision Task (Copy Task from Template).

      Decision Task
    6. Enter the Abbreviation and Name from the table above.

      Abbreviation and Name
    7. Save as local object and note down the Task-ID.

      Save as Local ObjectTask ID: ___________________________________________________

    8. Save the Task.

      Save Task
    9. Choose Agent Assignment for task.

      Agent Assignment for task
    10. Define the task as General Task and choose Transfer.

      Define the task as General Task and choose Transfer
    11. Save the Workflow.

    12. Activate the Workflow.

Task 2: Test the Workflow

Steps

  1. Test the Workflow.

    1. Open the test environment.

      Test the Workflow
    2. Execute the Workflow.

      Execute the Workflow

Task 3: Implement the BAdI-Builder

Steps

  1. Create a first enhancement implementation.

    Use the following data:

    FieldValue
    Enhancement ImplementationZ_##_BIT602_MyInbox
    Short TextBIT602_MyInbox Enhancement ##

    1. Execute transaction SE18 on the back-end server (S4D)

    2. Display the enhancement spot WF_INBOX_TASK_SUPPORTS.

      enhancement spot WF_INBOX_TASK_SUPPORTS
    3. Open Create BAdI Implementation.

      Create BAdI Implementation
    4. Choose Create Enhancement Implementation (F8).

      Create Enhancement Implementation (F8)
    5. Enter the data from the table above.

      Create Enhancement Implementation window
    6. Save as local Object.

      Local Object
  2. Create a first enhancement implementation.

    Use the following data:

    FieldValue
    BAdI ImplementationZ_##_BADI_MYINBOX
    DescriptionBAdI MyInbox ##
    Implementing ClassZ_##_BADI_MYINBOX

    1. Select new Enhancement Implementation.

      Enhancement Implementation
    2. See the steps above.

    3. Save as local Object.

  3. Change and activate the implementing class.

    Use the following code:

    Code Snippet
    12345678
    FIELD-SYMBOLS: <fs_task> TYPE if_wf_task_support=>t_task_supports. READ TABLE ch_tasks_supports WITH TABLE KEY task = 'YOURTASKID' ASSIGNING <fs_task>. IF <fs_task> IS ASSIGNED. <fs_task>-addattachments = abap_false. ENDIF.

    1. Double-click on Implementing Class

    2. Switch to Edit.

    3. Remove the supported action for adding attachments for your Task-ID by implementing method IF_WF_TASK_SUPPORT~GET_MULTIPLE_TASK_SUPPORT.

    4. Use the code, displayed above.

    5. Activate your Implementation.

    6. Test your Implementation.

Task 4: Test your Implementation

Steps

  1. Test the Workflow.

    1. Open the test environment.

      Test environment
    2. Execute the Workflow.

      Execute the Workflow
    1. Go to MyInbox

      The upload link for attachments in My Inbox for all workitems based on your task ID is no longer enabled.