Enterprise Resource Planning Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
MartinKnechtel
Product and Topic Expert
Product and Topic Expert
1,561

#saveThePlanet requires drilling into the details: Carbon Footprint Category Breakdown is released. Read my Blog article on how to extend your applications easily with Key User and Developer Extensibility for #SAPS4HANA

Introduction

SAP S/4HANA provides features that allow to extend existing applications with carbon footprint data. Our team’s previous Blog post described how to extend existing applications with carbon footprints, on the example of Manage Purchase Order. This is meanwhile standard functionality of Purchase Requisition and Purchase Order in current SAP S/4HANA releases. Nevertheless, the Blog post is still relevant for on premises installations not yet on the newest release and furthermore also for applications not yet enabled with footprints.

Today we bring it to the next level with our latest SAP S/4HANA Cloud 2408 release which includes the latest Sustainability Integration Component. It is enabled by Scope Item 5IM “Product Footprint Management” since several years, specifically since SAP S/4HANA Cloud 2108 and SAP S/4HANA 2021. We introduced new CDS views as listed here including:

  • List of greenhouse gas scopes I_GHGScope.
    A GHG scope defines where emissions come from, e.g. from your own factory, your suppliers, or your consumers to name some examples from Scope 1, 2 and 3.
  • List of greenhouse gas categories I_GHGCategory.
    A GHG category further subdivides the greenhouse gas scopes, for example Scope 3 into category 3.06 “business travel”, 3.12 “end-of-life treatment of sold products” etc.
  • Master Data Footprint Breakdown by GHG Category I_CO2eqFprntAvgGHGCat.
    This one is best explained stepwise:
    • SAP S/4HANA contains master data records, specifying for example a product from a plant, or a product from a plant delivered by a specific supplier.
    • The master data footprint is a record that relates to an existing master data record. It adds footprint information to this master data record.
    • The Breakdown by GHG Category splits the master data footprint into quantity components per GHG category.
  • Transactional Data Footprint Breakdown by GHG Category I_PFMTransDataCO2eqFprntGHGCat. The transactional data footprint is related to a transactional data record. For example, it computes the overall CO2e sum for a given purchase order for the sum of the list of items ordered.

Prerequisites

Learning Recommendation

For an introduction into the topic of footprints, see the training Climate Action with SAP Sustainability Footprint Management. It starts with an overview of Climate Action offerings by SAP and then focuses on SAP Sustainability Footprint Management hands-on and explains master data, transactional data, footprint computation etc.

Before you continue with the content below, I recommend making yourself familiar with Key User Extensibility and Developer Extensibility if you are not familiar already. I recommend Learning Journey “Practicing Clean Core Extensibility For SAP S/4HANA Cloud”, especially the chapters

User Roles

Make sure you have the following business roles assigned in your system:

  • SAP_UI_FLEX_KEY_USER
  • SAP_BR_PURCHASER
  • SAP_BR_EXTENSIBILITY_SPEC
  • SAP_BR_ADMIN_SUS_PFM

Key User Extensibility

Create Custom Field to Display the CO2e Category Breakdown in Your Purchase Requisition Item

Custom Fields is a tool for Key User Extensibility. Using this tool, a key user can create and edit custom fields for SAP tables, Core Data Service (CDS) views or OData services and expose them to the UI.

I now extend Purchase Requisition Item. It already shows the CO2e footprint, and I additionally insert GHG category breakdown by adding a Custom Field. The steps required are:

  • Select Adapt UI from your profile icon menu top right.
    1.png
  • Select Create Custom Fields
    MartinKnechtel_1-1723723305071.png
  • The list of Custom Fields is pre-filtered with the Business Context for Purchase Requisition Item, as you can see below. Create your new field here of Field Type: Text. Select something meaningful for label and identifier. The Business Context is prefilled with: Procurement: Purchase Requisition Item (MM_PURREQN_ITEM). The result should look like the screenshot below.
    3.png
  • Position the newly created field to the place you want it to be. In the screenshot below I dragged it into the General Information section.
    4.png

Create Custom Logic

As you saw in step 3 in the screenshot above, the category breakdown is placed now in the UI, but it does not yet show any value. Now I add the logic to define the text value. Go to application Custom Logic, add a new implementation in the Business Context and Extension Point as shown in the screenshot below.

MartinKnechtel_0-1723724060099.png

For the code, you can take the example below as a first draft. It composes the text for the GHG breakdown by calling our Calculation API, looping through the returned components, and doing basic error handling. It is good practice to limit the code to your user at first to try things out without interfering with others, adapt this in line 1 before you proceed.

 

 

 

IF purchaserequisitionitem-createdbyuser = 'CB9980004180'. "Martin Knechtel
    " get footprint calculation API
    TRY.
        purchaserequisitionitemchange-yy1_mk_pr_co2category_pri = 'CO2e category breakdown coming from my Key User Extensibility'. "our custom field receives a first initial assignment, helps you debugging
        CATCH cx_sy_conversion_overflow.
            INSERT VALUE #(
                messageid        = 'MEPO'
                messagetype      = 'E'
                messagenumber    = 250
                messagevariable1 = 'CO2e category breakdown exceeds size of Custom Field.'
            ) INTO TABLE messages.
    ENDTRY.
    TRY.
        DATA(lo_calculation_api) = cl_supfm_tdfp_calc_api_factory=>get_factory( )->get_calculation_api( ).
        CATCH cx_supfm_tdfp_calc_api_factory INTO DATA(lx_supfm_tdfp_calc_api_factory).
            INSERT VALUE #(
              messageid        = 'MEPO'
              messagetype      = 'E'
              messagenumber    = 250
              messagevariable1 = lx_supfm_tdfp_calc_api_factory->get_text( )
            ) INTO TABLE messages.
            RETURN.
    ENDTRY.
    TRY.
        " calculate the CO2e Footprint for the purchase requisition item
        DATA(ls_calculation_result_ghg) = lo_calculation_api->calculate_ghg_with_log(
                      VALUE #( ( 
Product       = purchaserequisitionitem-material "e.g. 'BUTTER100'
Plant         = purchaserequisitionitem-plant    "e.g. '1010' 
Quantity      = purchaserequisitionitem-orderedquantity "e.g. '3'
UnitOfMeasure = purchaserequisitionitem-purchaseorderquantityunit "e.g. 'KG'
                               ) ) ).

      " in case a footprint for the item was successfully calculated, loop over the components
      DATA responseString TYPE string.
      IF ls_calculation_result_ghg-has_result = abap_true.
          LOOP AT ls_calculation_result_ghg-pfmfootprintcomponents REFERENCE INTO DATA(component).
            DATA(quantityRounded) = round( val = component->pfmfootprintquantity
                                           dec = 3 ).
            responsestring = |{ responsestring }{ component->pfmfootprintqtycomponenttype }: { quantityRounded } { component->pfmfootprintunit } /  |.
          ENDLOOP.
      ELSE.
        responsestring = |No CO2e scope categories found. Log: { ls_calculation_result_ghg-calculation_log->get_calculation_log_json(  ) } Input: | &&
            |{ purchaserequisitionitem-material },{ purchaserequisitionitem-plant },{ purchaserequisitionitem-orderedquantity },{ purchaserequisitionitem-purchaseorderquantityunit }|.
      ENDIF.
      responsestring = |{ responsestring } at { cl_abap_context_info=>get_system_date(  ) }{ cl_abap_context_info=>get_system_time(  ) }|.
      purchaserequisitionitemchange-yy1_mk_pr_co2category_pri = responsestring.
    CATCH cx_supfm_tdfp_calc_api INTO DATA(lx_supfm_tdfp_calc_api).
      INSERT VALUE #(
        messageid        = 'MEPO'
        messagetype      = 'E'
        messagenumber    = 250
        messagevariable1 = lx_supfm_tdfp_calc_api->get_text( )
      ) INTO TABLE messages.
    ENDTRY.
ENDIF.

 

 

 

Returning to your Purchase Requisition Item you should now see the CO2e category breakdown similarly to the screenshot below.

MartinKnechtel_1-1723724234844.png

 

Developer Extensibility

Above I introduced Custom Logic for the ABAP code to prepare your text to be shown in the UI. This was done with Key User Extensibility tools. Now I switch into Developer Extensibilty. Preparing code with ADT is more comfortable if the Custom Logic above gets extensive. Debugging is possible. You can also encapsulate part of the logic and call it from the extension point Custom Logic above. An example is shown in the screenshot below. Similarly to the code example above, it calculates a transaction data footprint from a master data footprint and prepares the calculated GHG component breakdown to a string. Here it prints it to the console, but I could also assign it to some custom field, as shown above.

MartinKnechtel_2-1723724276753.png

In case there is no master data footprint found for the given product “BUTTER100”, an error with message number 4 is returned. As shown in the screenshot, the CM_SUPFM_TDFP_LOGGER message class was opened to retrieve the message text from the message number. This might indicate to the developer that no test data was created yet on the system and afterwards he can proceed debugging and developing. With that, I walked you through an end to end example for extending your application with sustainability features with Key User Extensibility and Developer Extensibility.

Disclaimer: I describe the work of many here and I am only one of the contributors in a great and dedicated team. We keep on rocking to deliver you the latest and greatest sustainability integration into SAP S/4HANA.