Skip to Content

Create and Debug a BAdI Implementation in S/4HANA Cloud ABAP Environment

Create and Debug a BAdI Implementation in ABAP Development Tools (ADT)
You will learn
  • How to create and implement a simple BAdI
  • Analyze errors using ABAP Cross Trace
  • How to inspect the BAdI implementing class in ABAP Debugger
julieplummer20Julie PlummerFebruary 17, 2023
Created by
julieplummer20
February 1, 2023
Contributors
julieplummer20

Prerequisites

  • You have a license for SAP S/4HANA Cloud; have a developer user in this system and have created an ABAP Cloud Project
  • You have installed SAP ABAP Development Tools (ADT), latest version, and have created an ABAP Cloud project for your SAP S/4HANA Cloud System in it
  • You are familiar with the concept of extensions to the SAP standard and with BAdIs in ADT in particular. If not, work through the Tutorial: Implement a Business Add-in (BAdI) To Check a Purchase Requisition. This tutorial also contains links to more information about BAdIs
  • You have created a BAdI enhancement implementation ZEI_PR_ITEM.
    (abap-s4hanacloud-procurement-purchasereq-checks), steps 1-4
  • You have opened the Fiori app Create Purchase Requisition
  • You are familiar with the concept of debugging ABAP in ADT. If not, see the links below
  • You have assigned the following roles to your user: SAP_BR_DEVELOPER, SAP_BR_PURCHASER

Throughout this tutorial, objects name include a suffix, such as ###. Always replace this with your group number or initials.

The administrator receives an welcome e-mail after provisioning. This e-mail includes the system URL. By removing /ui you can log into the SAP S/4HANA Cloud ABAP Environment system. Further information can be found Developer Extensibility: Connect to the ABAP System.

step0-overview-debug
  • Step 1
    1. Open an enhancement implementation ZEI_PR_ITEM.

    2. Choose Add BAdI implementation

      step1a-add-badi-impl
    3. Add the following, then choose Next:

      • BAdI Definition: MM_PUR_S4_PR_MODIFY_ITEM Modify Purchase Requisition Item (Add by clicking on Browse)
      • BAdI Implementation Name: ZIMP_PR_ITEM

        step1b-badi-impl-info

    Your BAdI implementation appears in a new editor.

    step1c-badi-impl-editor

    Ignore the error. You will fix this in the next step.

    Log in to complete tutorial
  • Step 2
    1. Choose Implementing Class.

    2. Enter the following, then choose Next.

      • Name: ZIC_PR_ITEM
      • Description: Modify PR Item for Debugger
      • (Added automatically): Interfaces: if_mm_pur_s4_pr_modify_item
    3. Choose the transport request, then choose Finish.

      The class appears in a new editor with skeleton code.

      ABAP
      Copy
      CLASS ZIC_PR_ITEM DEFINITION
        PUBLIC
        FINAL
        CREATE PUBLIC .
      
        PUBLIC SECTION.
      
          INTERFACES if_badi_interface .
          INTERFACES if_mm_pur_s4_pr_modify_item .
        PROTECTED SECTION.
        PRIVATE SECTION.
      ENDCLASS
      
      
    4. Format, save, and activate the class ( Shift+F1, Ctrl+S, Ctrl+F3 ).

    5. Go back to your BAdI implementation ZIMP_PR_ITEM and activate it too.

    The error will disappear.

    Log in to complete tutorial
  • Step 3
    1. Add the following code to the method implementation if_mm_pur_s4_pr_modify_item~modify_item:

      The BAdI returns an warning message if the doc type is standard. Since this triggers the BAdI almost every time, this is useful for tracing and debugging purposes.

      ```ABAP
      CLASS ZIC_PR_ITEM IMPLEMENTATION.
      
      
        METHOD if_mm_pur_s4_pr_modify_item~modify_item.
      
          DATA: ls_message LIKE LINE OF messages.
      
          " If <very common scenario occurs>, display Warning
          IF purchaserequisition-PURCHASINGDOCUMENTTYPE EQ 'NB'.
            ls_message-messageid = 'SampleID'.             "Message ID
            ls_message-messagetype = 'W'.                   "Type of Message
            ls_message-messagenumber = '001'.               "Message Number
            ls_message-messagevariable1 = 'PH1'.           "Place holder
            APPEND ls_message TO messages.
          ENDIF.
      
        ENDMETHOD.
      ENDCLASS.
      
      ```
      
    2. Format, save, and activate the class ( Shift+F1, Ctrl+S, Ctrl+F3 ).

    3. Check that yours is the implementation that will be called:

      step5d-impl-called
    Log in to complete tutorial
  • Step 4

    First you will add the relevant view to your ABAP perspective.

    1. In ADT, search for the view ABAP Cross Trace using the magnifying glass; then add it by clicking it.

      step4a-add-cross-trace-view
    2. The view appears. Now you will create the trace configuration. Select your system and choose Create Configuration… from the context menu.

      step4b-create-configuration

    3. Enter the following information, then choose OK.

      Field Name Value
      Activate Trace Yes
      Deactivate at … 40
      Description Trace Purch Req - Modify Item
      Component *Choose ABAP-BAdIs > BAdI Calls; deselect all other traces.
      | Trace Level | (4) Verbose
      step4d-trace-config

    The active trace appears.

    step4e-trace-active
    Log in to complete tutorial
  • Step 5

    You will now execute the app while the trace is active.

    1. In the app Manage Purchase Requisitions - Professional, choose an existing purchase requisition and navigate to the detail, then to the Items tab.

      step5a-pur-req-items
    2. Choose Edit.

    3. Trigger the BAdI, e.g. by changing the quantity; notice the message ID, BAdI.

      step5c-error
    Log in to complete tutorial
  • Step 6
    1. Back in ADT, deactivate the trace from the context menu, then choose Refresh.

      step5d-deactivate-trace
    2. In the tab Trace Results, navigate through the trace records; filter by MODIFY_ITEM.

    3. Note the relevant BAdI implementation class associated with IF_MM_PUR_S4_PR_MODIFY_ITEM~MODIFY_ITEM is ZIC_PR_ITEM; note the MESSAGEID = BAdI.

      step6b-find-messageId

    You can now edit this class in ADT.

    step6c-class-in-adt
    Log in to complete tutorial
  • Step 7

    Now, you can check the developer extension (BAdI) using ABAP Debugger.

    1. In the backend system, search for your implementing class ZIC_PR_ITEM.

      step4a-find-class
    2. Set a breakpoint.

      Position the cursor within the ruler (left bar) of the source editor at the line
      IF purchaserequisition-PURCHASINGDOCUMENTTYPE EQ 'NB'., then choose Toggle Breakpoint from the context menu.

      step4b-set-breakpoint
    3. Go back to the Manage Purchase Requisitions - Professional app and create a new purchase requisition (PR).

      step4c-pr-tile
    4. Now that the breakpoint is set, the system suggests you change to the ABAP Debugger perspective. Choose Switch.

      step5-switch-perspective
    5. The perspective appears, showing: The call stack; your code; any breakpoint(s) you have set.

      step7d-debugger-perspective
    6. You can now step through the code as normal (using F5, F6, F7). When you are done, choose Terminate (F8) from the main toolbar. You will return to the ABAP perspective.

      step7c-terminate
    Log in to complete tutorial
  • Step 8

    Once set, this condition is evaluated at runtime whenever the source code position of the breakpoint is reached. If the condition is fulfilled (evaluates to true), the debugger will stop execution at the breakpoint.

    1. Place your cursor on your breakpoint and choose Breakpoint Properties from the context menu.

      step8a-breakpoint-properties
    2. Add the condition PURCHASEREQUISITIONITEM~ORDEREDQUANTITY=9, then choose Apply.

      step8b-breakpoint-condition
    3. Again, run the app Manage Purchase Requisitions - Professional, create a new PR, switch to the Debugger when prompted.

    The Debugger has stopped where the ORDEREDQUANTITY = 9.

    step8c-condition-9
    Log in to complete tutorial
  • Step 9

    Once a watchpoint is set, the Debugger stops as soon as the value of a watched variable has changed. Furthermore, you can specify conditions for watchpoints.

    You can only set a watchpoint for a variable during a running ABAP debug session.

    1. In the class ZIC_PR_ITEM, position the cursor on the relevant field.

      step9a-set-watchpoint
      step9b-watchpoint-created
    2. To set a condition for the watchpoint: open the Breakpoints view in the Debugger perspective, select the watchpoint, then enter a condition.

      step9c-set-watchpoint-condition

    The ABAP Debugger stops as soon as the value of the variable is changed after a debug step and the specified condition for the watchpoint is fulfilled.

    Log in to complete tutorial
  • Step 10
    Log in to complete tutorial
  • Step 11
    Log in to complete tutorial
Back to top