Wednesday, 3 May 2023

Enabling F4 search help and customizing them for fields in Purchase Requisition (ME51N/52N) & Purchase orders (ME21N/22N)

Today, I would like to discuss a scenario that I recently came across. As an ABAP consultant, finding and enhancing standard transactions is the need for the business and achieving the same is a crucial task.

When I got the requirement to assign F4 help to a field in PR and PO, I started scratching my head looking for resources online to achieve the same.

So to begin with, lets see how these standard transactions for creation/change of PR and PO look like.

We’ll pick the requisitioner field (AFNAM) for this instance.

Here is the PR creation screen below

SAP ABAP, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP
AFNAM in PR creation screen

and here below comes the PO creation screen.

SAP ABAP, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP
AFNAM in PO creation screen

Now if you notice in both the above screens, we don’t have any search help assignment to this field and in the same way many other fields.

Now, in a scenario wherein validation and correctness of such fields are necessary, its crucial to avoid the errors from the end user side to a minimal extent possible. This can be achieved with a search help.

Steps to achieve Search help for PR creation/change:


Assignment of search help involves 2 steps:

1. Enable search help so that the F4 event is recognized.
2. Handle the search help to populate list of possible values.

Enable search help so that the F4 event is recognized.

In SE38, Open the Report LMEGUICJM and navigate to the method GET_FIELDS_FOR_F4.

Create implicit enhancement at the end of this method and add the code below:

CONSTANTS: lc_me51n TYPE syst_tcode VALUE 'ME51N',  " Constant for T-Code ME51N.
           lc_me52n TYPE syst_tcode VALUE 'ME52N'.   " Constant for T-Code ME52N.

* Check if the T-Code is ME51N or ME52N or ME53N
IF sy-tcode = lc_me51n OR sy-tcode = lc_me52n.
  lmac_upd_then_ins 'AFNAM' 'X'.                   " Enable the F4 help for the requisitioner field in PR.
ENDIF.

Activate this enhancement.

Now execute T-code ME51N and you should see the screen below.

SAP ABAP, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP
F4 help enabled for PR screen

As you can see, the F4 help is now enabled and this event can be handled further.

Handle the search help to populate list of possible values.

Create a Function Module which can be used in both the PR and PO transactions to handle the F4 help for this field.

Parameters for the function Module are as below.

FUNCTION z_afnam_search_help.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IV_PROGRAM) TYPE  SY-CPROG
*"     REFERENCE(IV_DYNNR) TYPE  SY-DYNNR
*"     REFERENCE(IV_AFNAM) TYPE  EBAN-AFNAM OPTIONAL
*"  EXPORTING
*"     REFERENCE(EV_AFNAM) TYPE  EBAN-AFNAM
*"----------------------------------------------------------------------

Code to be written inside this FM is as follows.

  DATA: lv_value TYPE char10.
  DATA : lv_flag(1).
  DATA: BEGIN OF lt_dynpread OCCURS 10.
          INCLUDE STRUCTURE dynpread.
  DATA: END OF lt_dynpread.

* Search help table structure. This will vary w.r.t usecase.
  DATA: BEGIN OF lt_f4help OCCURS 0,
          afnam TYPE afnam,
        END OF lt_f4help.
  DATA: t_return LIKE ddshretval OCCURS 0 WITH HEADER LINE.

  CLEAR : lv_flag.

  IF iv_afnam IS INITIAL.
    REFRESH lt_dynpread.
    CLEAR lt_dynpread.
    lt_dynpread-fieldname = 'MEREQ3211GRID-AFNAM'.
    APPEND lt_dynpread.

    CALL FUNCTION 'DYNP_VALUES_READ'
      EXPORTING
        dyname     = iv_program
        dynumb     = iv_dynnr
      TABLES
        dynpfields = lt_dynpread
      EXCEPTIONS
        OTHERS     = 1.

    IF sy-subrc EQ 0.
      READ TABLE lt_dynpread INDEX 1.
      lv_value = lt_dynpread-fieldvalue.
    ELSE.
      lv_flag = 'X'.
    ENDIF.
  ENDIF.

  IF lv_flag = 'X'.
* Below SELECT code is to pupulate the table for search help. This may vary respectively. 
    SELECT bname FROM usr01
    INTO TABLE lt_f4help
    WHERE langu = sy-langu.
    SORT lt_f4help.
    CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
      EXPORTING
        retfield   = 'AFNAM'
        value_org  = 'S'
      TABLES
        value_tab  = lt_f4help
        return_tab = t_return[]
      EXCEPTIONS
        OTHERS     = 1.
    READ TABLE t_return INDEX 1.
    IF sy-subrc = 0.
      READ TABLE lt_f4help WITH KEY afnam
      = t_return-fieldval BINARY SEARCH.
      ev_afnam = lt_f4help-afnam.
    ENDIF.
  ENDIF.

Activate this Function Module. This will be used in the enhancement to handle F4 help.

Create Implicit enhancement to handle the F4 event.

In SE38, Open the Report LMEGUICJM and navigate to the method HANDLE_F4.

Create implicit enhancement at the end of this method and add the code below:

DATA : lv_afnam TYPE afnam.
CONSTANTS: lc_afnam TYPE lvc_fname VALUE 'AFNAM'.
FIELD-SYMBOLS: <fs_event_handled> TYPE char01.

* The case statement below is in continuation to the case statement in the HANDLE_F4 method
CASE e_fieldname.
  WHEN lc_afnam.                              " Requisitioner field.
* Call the FM to populate search help values and get the value selected.
    CALL FUNCTION 'Z_AFNAM_SEARCH_HELP'
      EXPORTING
        iv_program = prog
        iv_dynnr   = dynpro
      IMPORTING
        ev_afnam   = lv_afnam.
    IF sy-subrc IS INITIAL AND l_ch EQ mmpur_yes.
      ls_modi-value = lv_afnam.
    ENDIF.

    IF NOT ls_modi IS INITIAL.
* Append Data.
      ASSIGN er_event_data->m_data->* TO <fs_t_modi>.
      ls_modi-row_id = es_row_no-row_id.
      ls_modi-fieldname = e_fieldname.
      APPEND ls_modi TO <fs_t_modi>.

* Check the Event handled Flag to indicate the F4 event has been handled.
      ASSIGN er_event_data->m_event_handled TO <fs_event_handled>.
      IF <fs_event_handled> IS ASSIGNED.
        <fs_event_handled> = abap_true.
      ENDIF.
    ENDIF.
ENDCASE.

Activate this enhancement.

Now execute T-Code ME51N and click on F4 help on the Requisitioner field. You can see the F4 help values in the list.

SAP ABAP, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP
F4 help for AFNAM in PR Screen

Hence, we have achieved the implementation of Search help for PR Transactions. Same process can be followed for any other field where the Search help is not enabled by standard SAP.

Steps to achieve Search help for PO creation/change:


Similar to PR, assignment of search help involves 2 steps:

1. Enable search help so that the F4 event is recognized.
2. Handle the search help to populate list of possible values.

Enable search help so that the F4 event is recognized.

Here, the process is a little different as compared to how it is done for the PR screens.

Go to T-code ME21N and find the technical information for the field by using F1 help.

SAP ABAP, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP
F1 information for AFNAM in PO screen

Enter the Program name and screen number in SE51 and go to the flow logic screen,

In the PROCESS ON VALUE-REQUEST event, create an insert and add the below code.

FIELD mepo1211-afnam MODULE event_pov_list.

Activate the screen and generate the program SAPLMEGUI.

This will enable the F4 event and the screen in ME21N will look as follows.

SAP ABAP, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP
F4 help enabled for PO screens

As you can see, the F4 help is now enabled and this event can be handled further.

Handle the search help to populate list of possible values.

In SE38, Open the Report LMEGUICIM and navigate to the method POV_LIST.

Create implicit enhancement at the end of this method and add the code below:

DATA : lv_afnam TYPE afnam.                     " Requiaitioner value.

CASE im_metafield.
* Check in Type group MMMFD to get the metafield constant value. For AFNAM field, the metafield value is 
* declared in constant MMFD_PREQ_NAME.
  WHEN mmmfd_preq_name.
* Call the Function module to execute F4 help and get the value selected.
    CALL FUNCTION 'Z_AFNAM_SEARCH_HELP'
      EXPORTING
        iv_program = prog
        iv_dynnr   = dynpro
      IMPORTING
        ev_afnam   = lv_afnam.
    IF sy-subrc IS INITIAL.
      mepo1211-afnam = lv_afnam.                " Return value from F4 help.
    ENDIF.
ENDCASE.

Activate the enhancement.

Now execute T-Code ME21N and click on F4 help on the Requisitioner field. You can see the F4 help values in the list.

SAP ABAP, SAP ABAP Career, SAP ABAP Skills, SAP ABAP Jobs, SAP ABAP Prep, SAP ABAP Preparation, SAP ABAP Guides, SAP ABAP Learning, SAP ABAP
F4 help for AFNAM in PO Screen

Hence, we have achieved the implementation of Search help for PO Transactions. Same process can be followed for any other field where the Search help is not enabled by standard SAP.

No comments:

Post a Comment