Friday, 1 January 2021

How to extend transaction FAGLL03H with custom fields

This blog has been written to assist developers asked to extend the standard report of financials line item browser transaction FAGLL03H with additional custom fields.

Introduction

Usually, a business user may require additional fields in financial line items browser which, as powerful as it may be on its own with dynamic selections, is not enough to display information derived from other functional domains like material master data-related fields. In the real-world example that follows, material fashion grade code and text were required to be added in a SAP client with S/4HANA and retail industry functions activated.

Solution

First of all, while searching SAP Knowledge Base there are quite a few articles, most notably in note 2219887 which in turn points to note 2512883 , yet it is not always clear how to populate values to added custom fields. This is a step-by-step implementation of what should be done to achieve this. It is a two-part configuration, one affecting the existing data model with custom fields and a second to program code to populate custom fields.

Part one: Data Model configuration

Step 1: Execute transaction to view columns

A good idea is to execute first financial line items browser (FAGLL03H transaction) in order to have a feel of what is required. Your Financials analyst or consultant may do this for you also. For obvious reasons, description columns have been removed to keep anonymity, although the images are not from a productive environment.

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Financials line items browser

Step 2: Identifying metadata of fields to be added

Using display transaction for retail industry material (MM43), we find that fashion grade field is located at material master basic data.

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Locate required field

Thus, two columns should be added to the report, namely

Table Field Description Context Include in report
MARA  FASHGRD Fashion grade code Foreign key Yes
T6WFG  FASHGRD  Fashion grade code  Unique key in check table  No 
T6WFGT  SPRAS   Language Key   Part of unique key in text table  No 
T6WFGT  VTEXT  Fashion grade description  Part of unique key in text table  Yes 

Step 3: Identifying structures to extend

By following instructions in note 2512883, we should add our custom fields in append structures, as depicted below:

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Add fields to data model

Step 4: Add custom fields

Using SE11/Data Type/Display, create your fields in append structures of both above structures, as instructed by note.

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Append structure to standard one

Step 5: Adding custom fields to append structure

Create required fields as identified in step 2 to append structure.

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Append structure fields

Bear in mind that, according to note, custom fields in append structures should be created for both standard structures since an append structure may not be shared.

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Note warning

Step 6: Generate all views

As a final step in configuration, generation of all views should be done using transaction HDBVIEWS

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Generation of views

This should be executed manually in each system after deployment, so in the context of project management, it should be added as a separate step in the technical cutover plan following transport of relevant requests.

Part two: ABAP code


Now that fields have been added and views generated, they are visible in financial line items browser and if selected columns will be added to the layout, although with blank values. The only thing that remains is to program code accordingly in order to populate the fields. For this, standard enhancement spot FAGL_LIB is provided and may be accessed via transaction SE18.

Step 7: Create a custom business add-in (BAdI) implementation

There are already two implementations by SAP. Create your own by right-clicking on left-hand pane at Implementations node.

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Create custom implementation

Assign appropriate names for enhancement and BAdI implementations and implementing class. Implementing class inherits all methods from standard enhancement. You may accept proposal to model your implementation after an already existing one or create a new from scratch.

Step 8: Select method to populate data

Your aim should be to populate data in method SELECT_DATA

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Available methods

Step 9: Analyze method signature

By double-clicking on the standard class interface IF_FAGL_LIB, we navigate to all methods provided and their respective parameters. More specifically, regarding the method SELECT_DATA most parameters are imported into the method with the exception of tables CT_DATA and CT_MESSAGE.  CT_DATA contains data as populated by standard transaction FAGLL03H and its content may be impacted by our code. This table has a changing type meaning it is imported, may be changed and exported back to the calling program.

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
SELECT_DATA parameters

Step 10: Program code in method

The only differentiation with other SAP-provided extensions is that table content is dynamic and not static, for example any column may contain any type of field, as defined by user options. Therefore special statements will be used in ABAP to retrieve the metadata and the sequence of each column in the layout depending on user preferences. Using code in method SELECT_DATA of already existing implementation FAGL_LIB_ARCHIVE_VIA_INDEX as model, our method should look like this:

  METHOD if_fagl_lib~select_data.

    FIELD-SYMBOLS: <fs> TYPE any.
    FIELD-SYMBOLS <ls_data>                 TYPE any.
    FIELD-SYMBOLS <ld_data>                 TYPE any.

    DATA: ls_component LIKE LINE OF it_component.
    DATA lr_data                  TYPE REF TO data.
    DATA lo_descr                 TYPE REF TO cl_abap_typedescr.
    DATA lo_str_descr_in          TYPE REF TO cl_abap_structdescr.
    DATA ls_abap_comp_descr       TYPE abap_compdescr.

    DATA: lv_matnr               TYPE mara-matnr,
          lv_fashgrd             TYPE mara-fashgrd,
          lv_text                TYPE t6wfgt-vtext.

    CREATE DATA lr_data LIKE LINE OF ct_data.
    ASSIGN lr_data->* TO <ls_data>.

* Get structure description of data table
    CALL METHOD cl_abap_structdescr=>describe_by_data
      EXPORTING
        p_data      = <ls_data>
      RECEIVING
        p_descr_ref = lo_descr.

    lo_str_descr_in ?= lo_descr.

This set of statements is used to retrieve user-defined layout metadata using standard ABAP classes CL_ABAP_TYPEDESCR and CL_ABAP_STRUCTDESCR. Next, a table iteration in content is required to update relevant columns as follows:

    LOOP AT ct_data ASSIGNING <ls_data>.

* Go over the components of the structure
      LOOP AT lo_str_descr_in->components
        INTO ls_abap_comp_descr.

        " take into account only for material
        IF ls_abap_comp_descr-name = 'MATNR'.
        ELSE.
          CONTINUE.
        ENDIF.

For each row in the report, we should examine each component that corresponds to a column in the layout. In this case, both fashion grade and text fields depend on whether material is selected by the user as a column to be depicted, so we ignore all other fields in the metadata table. After assuring that the material column is encountered, we assign its content value to a field and we derive required fields for fashion grade and text, as found in step 2

* Get a field symbol with the same type as the component
        DO.
          " populate fashion grade
          ASSIGN COMPONENT ls_abap_comp_descr-name
            OF STRUCTURE <ls_data>
            TO <ld_data>.
          IF sy-subrc NE 0.
            EXIT.
          ENDIF.

          lv_matnr = <ld_data>.

          CLEAR lv_fashgrd.
          SELECT SINGLE fashgrd FROM mara INTO lv_fashgrd
              WHERE matnr = lv_matnr.
          IF sy-subrc IS INITIAL.

            ls_abap_comp_descr-name = 'ZZFASHGRD'.
            ASSIGN COMPONENT ls_abap_comp_descr-name
             OF STRUCTURE <ls_data>
                      TO <ld_data>.
            IF sy-subrc NE 0.
              EXIT.
            ENDIF.

            <ld_data> = lv_fashgrd.

            CLEAR lv_text.
            SELECT SINGLE vtext INTO lv_text
               FROM t6wfgt
              WHERE fashgrd = lv_fashgrd
                AND spras  = sy-langu.
            IF sy-subrc IS INITIAL.
              ls_abap_comp_descr-name = 'ZZFASHGRD_VTEXT'.
              ASSIGN COMPONENT ls_abap_comp_descr-name
                       OF STRUCTURE <ls_data>
                   TO <ld_data>.

              <ld_data> = lv_text.
            ENDIF.
          ENDIF.
          EXIT.
        ENDDO.


      ENDLOOP.

    ENDLOOP.

  ENDMETHOD.
 
Notice that a DO loop is used as a block to include statements with data retrieval for the required fields. This is done because we may have additional custom fields to populate that may depend semantically on other columns in the layout.

Step 11: Execute line items browser to confirm results

Now, when our custom fields are selected by the user, the layout should include the additional columns populated by our code as shown below:

SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Guides, SAP ABAP Study Material, SAP ABAP Prep
Financials line items browser with populated columns

No comments:

Post a Comment