SAP ABAP FIELD EXIT

From SapWiki
  • NOTA: Para poder activar los Field Exits debemos tener el parámetro abap/fieldexit = YES seteado en el servidor (Transacción RZ10).
  • Crear field exit:
  1. ejecutar reporte RSMODPRF
  2. Crear módulo de función con nombre FIELD_EXIT_*
  • Ejemplo, validar campo KBSTAT en trn. VK11, VK12 & VK13
FUNCTION field_exit_kbstat.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(INPUT)
*"  EXPORTING
*"     REFERENCE(OUTPUT)
*"----------------------------------------------------------------------
  DATA wa_por_niv_sec TYPE zsd_por_niv_sec.
  DATA: w_eval LIKE dynpread-fieldvalue.
  DATA: w_eval_2 LIKE dynpread-fieldvalue.
  DATA: w_eval_3 LIKE dynpread-fieldvalue.
  DATA l_matnr TYPE mara-matnr.
  DATA l_spart TYPE mara-spart.
  DATA l_por TYPE zsd_por_niv_sec-por_01.
  DATA l_por_i TYPE konp-kbetr.
  DATA l_input(1).
  DATA l_sizec LIKE gt_t691i-sizec.
  DATA t185v TYPE t185v.
  DATA ls_t681s TYPE t681s.
  DATA l_kschl TYPE rv13a-kschl.
  DATA: lw_var(40) TYPE c.

  FIELD-SYMBOLS <fs> TYPE any.

  lw_var = '(SAPMV13A)T681S'.
  ASSIGN (lw_var) TO <fs>.
  IF sy-subrc = 0 AND <fs> IS ASSIGNED.
    MOVE <fs> TO ls_t681s.
  ENDIF.

  IMPORT t185v FROM MEMORY
              ID 'SD_COND_MEM_01'.

  CHECK sy-tcode = 'VK12' OR sy-tcode = 'VK11' OR sy-tcode = 'VK13'.
  CHECK sy-title(1) <> 'V'.  "Visualizar

  GET PARAMETER ID 'VKS' FIELD l_kschl.
  CHECK l_kschl = 'ZAB0'
     OR l_kschl = 'ZAB1'
     OR l_kschl = 'ZAB2'
     OR l_kschl = 'ZAB3'
     OR l_kschl = 'ZAB4'
     OR l_kschl = 'ZAB5'.

  MOVE input TO l_input.

  IF l_input IS INITIAL.
    MESSAGE e000(0k) WITH 'Error:'
                          'debe ingresar valor en Status de tratamiento'.
  ENDIF.

  l_por = 0.
  l_por_i = 0.
  CLEAR w_eval.

* copia de C14Z_DYNP_READ_FIELD, permite leer valores de una línea de tabla
  CALL FUNCTION 'Z_C14Z_DYNP_READ_FIELD'
    EXPORTING
      i_program      = 'SAPMV13A'
      i_dynpro       = ls_t681s-dynpronr   "'1305'
      i_fieldname    = 'KOMG-MATNR'
      i_fieldname_2  = 'KONP-KBETR'
      i_fieldname_3  = 'KOMG-SPART'
      i_flg_steploop = 'X'
      i_flg_request  = 'X'
    CHANGING
      e_value        = w_eval     "material
      e_value_2      = w_eval_2  "porcentaje
      e_value_3      = w_eval_3.  "sector


    MOVE w_eval TO l_matnr.

    CONDENSE w_eval_3.
    MOVE w_eval_3 TO l_spart.

    CONDENSE w_eval_2.
    TRANSLATE w_eval_2 USING ',.'.
    MOVE w_eval_2 TO l_por_i.

* insert some logic here

*

  IF 1 = 2 .  "dummy, asignación de valor posible a campo
    output = 'B'.
  ELSE.
    output = 'L'.
  ENDIF.

  IF l_input <> output.
    MESSAGE e000(0k) WITH 'Error en Status de tratamiento:'
                          'Solo se permite valor'
                           output.
  ENDIF.

ENDFUNCTION.

FUNCTION Z_C14Z_DYNP_READ_FIELD.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     VALUE(I_PROGRAM) LIKE  D020S-PROG
*"     VALUE(I_DYNPRO) LIKE  D020S-DNUM
*"     VALUE(I_FIELDNAME) LIKE  DYNPREAD-FIELDNAME
*"     REFERENCE(I_FIELDNAME_2) TYPE  DYNPREAD-FIELDNAME OPTIONAL
*"     REFERENCE(I_FIELDNAME_3) LIKE  DYNPREAD-FIELDNAME OPTIONAL
*"     VALUE(I_FLG_STEPLOOP) TYPE  C
*"     VALUE(I_FLG_REQUEST) TYPE  ESP1_BOOLEAN DEFAULT ESP1_FALSE
*"  EXPORTING
*"     VALUE(E_STEPL) LIKE  SY-STEPL
*"     VALUE(E_FLG_FIELD_INPUT) TYPE  ESP1_BOOLEAN
*"  CHANGING
*"     VALUE(E_VALUE) LIKE  DYNPREAD-FIELDVALUE
*"     VALUE(E_VALUE_2) LIKE  DYNPREAD-FIELDVALUE OPTIONAL
*"     VALUE(E_VALUE_3) LIKE  DYNPREAD-FIELDVALUE OPTIONAL
*"----------------------------------------------------------------------

  DATA: BEGIN OF DYNPTAB OCCURS 1.
          INCLUDE STRUCTURE DYNPREAD.
  DATA: END OF DYNPTAB.

  DATA: STEPL LIKE SY-STEPL.

* Begin Correction 05.07.2004 751985 ***********************************
  DATA: request LIKE  d020s-type.
* End Correction 05.07.2004 751985 *************************************

* Initialization
  TRANSLATE i_program   TO UPPER CASE.                   "#EC TRANSLANG
  " translate i_dynpro    to upper case.
  TRANSLATE I_FIELDNAME TO UPPER CASE.                   "#EC TRANSLANG

* Get the currently entered value
  CLEAR E_STEPL.
  clear e_value_2.
  IF I_FLG_STEPLOOP = 'X'.
*    CALL FUNCTION 'DYNP_GET_STEPL'
*         IMPORTING
*              POVSTEPL        = STEPL
*         EXCEPTIONS
*              STEPL_NOT_FOUND = 01.
*    E_STEPL = STEPL.
    move sy-STEPL to E_STEPL.
    move sy-STEPL to STEPL.
  ENDIF.

  DYNPTAB-FIELDNAME = I_FIELDNAME.
  DYNPTAB-STEPL     = STEPL.
  APPEND DYNPTAB.

* Begin Correction 05.07.2004 751985 ***********************************
* Check whether the field accepts input
  IF ( i_flg_request = 'X' ).
    request = 'A'.
  ELSE.
    CLEAR: request.
  ENDIF.                               " IF ( i_flg_request = esp1_true

  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      DYNAME               = I_PROGRAM
      DYNUMB               = I_DYNPRO
      request              = request
    TABLES
      DYNPFIELDS           = DYNPTAB
*     TRANSLATE_TO_UPPER   = ' '
    EXCEPTIONS
      INVALID_ABAPWORKAREA = 01
      INVALID_DYNPROFIELD  = 02
      INVALID_DYNPRONAME   = 03
      INVALID_DYNPRONUMMER = 04
      INVALID_REQUEST      = 05
      NO_FIELDDESCRIPTION  = 06
      UNDEFIND_ERROR       = 07.
* End Correction 05.07.2004 751985 *************************************

*  IF i_flg_steploop = 'X'.
*    READ TABLE DYNPTAB WITH KEY FIELDNAME = I_FIELDNAME STEPL = STEPL.
*  ELSE.
*    READ TABLE DYNPTAB WITH KEY FIELDNAME = I_FIELDNAME.
*  ENDIF.

*  read TABLE DYNPTAB with key FIELDNAME = 'KONP-KONWA' STEPL = STEPL.
*  if sy-subrc = 0 and dynptab-fieldvalue = '%'.
    READ TABLE DYNPTAB WITH KEY FIELDNAME = I_FIELDNAME STEPL = STEPL.
    if sy-subrc = 0.
      e_value = dynptab-fieldvalue.
    endif.
    read TABLE DYNPTAB with key FIELDNAME = I_FIELDNAME_2 STEPL = STEPL.
    if sy-subrc = 0.
      e_value_2 = dynptab-fieldvalue.
    endif.
*  endif.

    READ TABLE DYNPTAB WITH KEY FIELDNAME = I_FIELDNAME_3 STEPL = STEPL.
    if sy-subrc = 0.
      e_value_3 = dynptab-fieldvalue.
    endif.

* Begin Correction 05.07.2004 751985 ***********************************
  e_flg_field_input = dynptab-fieldinp.
* End Correction 05.07.2004 751985 *************************************

*  e_value = dynptab-fieldvalue.

ENDFUNCTION.