Skip to Content

Implement a Field Control Using a Business Add-in (`BAdI`)

Hide Prices for a Specified User Using a Business Add-in (`BAdI`) for a Purchase Requisition
You will learn
  • How to logon to SAP S/4HANA Cloud ABAP Environment
  • How to create an ABAP package
  • How to find relevant existing BAdI enhancement spots for your line of business, in this case Materials-Management-Purchasing
  • How to hide a field, Price, by changing the status of a field control
julieplummer20Julie PlummerNovember 24, 2022
Created by
julieplummer20
September 28, 2021
Contributors
julieplummer20
maximilianone

Prerequisites

  • You have a license for SAP S/4HANA Cloud and have a developer user in this system
  • 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 particular. If not, see the More Information section at the end of this tutorial

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.

A Business Add-In (BAdI) enables you to implement enhancements to standard SAP applications without modifying the original code.

In this case, the BAdI is implemented in the SAP Fiori app as follows:

  1. Use an existing enhancement spot MM_PUR_S4_PR_FLDCNTRL_SIMPLE, with an existing BADI definition MM_PUR_S4_PR_FLDCNTRL_SIMPLE
  2. Create a container ( enhancement implementation ) in the enhancement spot
  3. Create a BADI implementation for the BADI definition.
step0-badi-creation-enhancement-process

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

  • Step 1
    1. Open ADT, select File > New > Other.

      logon
    2. Search ABAP Cloud Project, select it and choose Next >.

      logon
    3. Select SAP S/4HANA Cloud ABAP Environment, enter the ABAP service instance URL and choose Next.

      logon
    4. To log on, choose Open Logon Page in Browser, then choose Next.

      logon

      .

      logon

    5. Check your ABAP service instance connection and choose Finish.

      logon

    Your project is available in the Project Explorer.

    logon
  • Step 2

    First, create a package for your development objects, or use an existing package (for example, if you have already created a BAdI.)

    1. Choose your project, then choose New > ABAP Package from the context menu.

      step1a-package-new
    2. Enter the following.

      • Zxx_MM_PUR_S4_BADI
      • BADIs for MM Purchasing
    3. Optional: Choose Add to favorite packages, then choose Next

      step1b-package-name
    4. Choose Create a new request, enter a meaningful description, e.g. Test BADIs MM-PURCHASING then choose Finish.

  • Step 3
    1. First make sure that the released APIs are displayed by application component: In your project, navigate to Released Objects. The tree should show the Released Objects sorted by Application Component.

      step2b-released-objects
    2. If not, choose Configure Tree from the context menu, then select Application Component from the left side ( Available Tree Levels ) and add it to the right side ( Selected Tree Levels ). Move it to the top. Then choose Finish.

      step2a-tree-configure

      .

      step2b-application-component-add

    3. In your project, drill down to Released Objects > MM-PUR-VM > Enhancements > Enhancement Spots > MM_PUR_S4_PR_FLDCNTRL_SIMPLE and open it by double-clicking.

      step2c-enhancement-spot-choose-field-control

    The BAdI enhancement spot appears in a new editor, showing you the available BAdI definitions (1).

    To help you create your own enhancements, example classes are provided (2).

    step2d-enhancement-spot-editor

    A complete searchable table of available BAdIs and the Fiori apps to which they pertain is available here:
    SAP Help Portal: Adaptation of App Behavior

  • Step 4

    Next, you need a container within the enhancement spot for your BADI implementations. This is known as an enhancement implementation.

    1. Select your package Zxx_MM_PUR_S4_BADI and choose New > Other ABAP Object from the context menu.

      step3a-badi-implem-create
    2. Filter by BAdI, choose BAdI Enhancement Implementation, then choose Next.

    3. Add the following and choose Next.

      • Name: Zxx_BADI_FIELD_CONTROL_PO
      • Description: Field control: Change price to hidden
      • Enhancement Spot: MM_PUR_S4_PR_FLDCNTRL_SIMPLE
      step3c-BAdi-enh-impl-editor-hide-price
    4. Choose the transport request, then choose Finish.

    Your BAdI enhancement implementation appears in a new editor. It implements the enhancement spot MM_PUR_S4_PR_FLDCNTRL_SIMPLE.

    step3d-badi-enh-impl-editor-field-control
  • Step 5
    1. Choose Add BAdI.

      step4a-badi-impl-add
    2. Add the following, then choose Next:

      • BAdI Definition: MM_PUR_S4_PR_FLDCNTRL_SIMPLE (Add by clicking on Browse)
      • BAdI Implementation Name: ZXX_BADI_FLDCONTROL_PO_IMPL
      step4b-badi-impl-name

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

  • Step 6
    1. Choose Implementing Class.

      step5a-implementing-class-create
    2. Add the following, then choose Next.

      • Name: ZXX_CL_FLDCONTROL_PO
      • Description: Implement hide price for given user
      • Interfaces: IF_MM_PUR_S4_PR_FLDCNTRL (added automatically)
      step5b-implementing-class-name-hide-price
    3. Choose the transport request, then choose Finish.

      The class appears in a new editor with skeleton code.

      step5c-class-editor
    4. Format, save, and activate the class ( Shift+F1, Ctrl+S, Ctrl+F3 ).

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

    The error will disappear.

  • Step 7
    1. Add the following code to the method implementation if_mm_pur_s4_pr_fldcntrl~modify_fieldcontrols.

      ABAP
      Copy
      * Disclaimer:
      * Any software coding or code lines / strings ("Code") provided are only examples
      * and are not intended for use in a productive system environment. The Code is only
      * intended to better explain and visualize the syntax and phrasing rules for certain
      * SAP coding. SAP does not warrant the correctness or completeness of the Code
      * provided herein and SAP shall not be liable for errors or damages cause by use of
      * the Code, except where such damages were caused by SAP with intent or with gross
      * negligence.
      *------------------------------------------------------------------------------------
      *   Value for FIELDSTATUS can be set to anyone of the following
      *   '-' Field is Hidden
      *   '*' Field is only Displayed, which means no entry is possible
      *   '+' Field is Mandatory, which means an entry must be made
      *   '.' Field is Optional, which means an entry may be made
      *------------------------------------------------------------------------------------
      
      **    Sample Code
      **price hidden when created user is 'CB9980000609'.
      *
          IF purchaserequisitionitem-createdbyuser  EQ 'CB9980000609'.
            READ TABLE fieldselection_table ASSIGNING <fs> WITH KEY field = 'PURCHASEREQUISITIONPRICE'.
            IF sy-subrc EQ 0.
              <fs>-fieldstatus = '-'.
            ENDIF.
          ENDIF.
      **
      **Alternative sample code 1:
      ** price hidden when requisitioner is a specific person
      **
      * enter in field purchase requisitioner = HIDEPRICE
      *    IF purchaserequisitionitem-purreqnrequestor  EQ 'HIDEPRICE'.
      *      READ TABLE fieldselection_table ASSIGNING <fs> WITH KEY field = 'PURCHASEREQUISITIONPRICE'.
      *      IF sy-subrc EQ 0.
      *        <fs>-fieldstatus = '-'.
      *      ENDIF.
      *    ENDIF.
      
      **
      **Alternative sample code 1:
      ** hide price for all users
      **
      * READ TABLE fieldselection_table ASSIGNING <fs> WITH KEY field = 'PURCHASEREQUISITIONPRICE'.
      *    IF sy-subrc EQ 0.
      *      <fs>-fieldstatus = '-'.
      *    ENDIF.
      
      
      
      
    2. Format, save, and activate ( Shift+F1, Ctrl+S, Ctrl+F3 ) your code.

    Check that yours is the implementation that will be called:

    step5d-impl-called
  • Step 8

    Which BAdIs can be used for developer extensions?

  • Step 9
Back to top