Saturday, August 27, 2016

How to write a custom SAP EM activity function to compare analyze a parameter value


Comparing a parameter value

USE: You may need to compare an existing parameter value with another value. E.g. You have an amount parameter that you want to check is greater than 100,000. Enter the Input parameter 1 as SO_NETVALUE (your parameter name), C as a control parameter (limited to I and C for Info and Control parameters), the value 100000 against which to check against and the Operation >.

It will then go to the EH and return the parameter value for SO_NETVALUE and if it is > 100000 then it returns a 0 otherwise it returns 4 stating that the parameter has a value less than 100000.

The code

FUNCTION zem_compare_param.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(I_EAI) TYPE  /SAPTRX/EM_ACTIVITY_INTERFACE
*"  EXPORTING
*"     REFERENCE(E_RETURN_CODE) LIKE  SY-SUBRC
*"----------------------------------------------------------------------
*Parameter-1  Input Param Nam  Input Parameter Name
*Parameter-2  Input Param Typ  Input Parameter Type
*Parameter-3  Input Param Val  Input Parameter Value
*Parameter-4  Operation        Operation (=, <>, >, <)
  CONSTANTS: c_true  TYPE c VALUE 'X',
             c_false TYPE c VALUE space.
  DATA:    lf_parameter1 TYPE /saptrx/activity_param_id,
           lf_parameter2 TYPE /saptrx/activity_param_id,
           lf_parameter3 TYPE /saptrx/activity_param_id,
           lf_parameter4 TYPE /saptrx/activity_param_id,
           l_eai  TYPE /saptrx/em_activity_interface,
           rc          LIKE sy-subrc,
           lv_found,
           ls_cntrl    TYPE /saptrx/eh_cntrl_dyn,
           ls_info     TYPE /saptrx/eh_info_dyn,
           l_eh_data   TYPE /saptrx/eh_data,
           l_eh_flags  TYPE /saptrx/eh_access_flags.
*"----------------------------------------------------------------------
*"  If in "Simulate" mode, you should bypass processing for any function
*"  modules that perform updates either locally or in a remote system.
*  CHECK i_eai-simulate_mode IS INITIAL.
  DATA: l_param                TYPE /saptrx/function_param,
        l_arg_1                TYPE /saptrx/msg_var,
        l_arg_2                TYPE /saptrx/msg_var.
  break tivie.
  e_return_code = 4.
*** CHECK IF THE INTERFACE IS NOT INITIAL.
  CHECK NOT i_eai-eh_model IS INITIAL.
* Get Parameter Values
  lf_parameter1 = i_eai-task-param_1.
  SHIFT lf_parameter1 LEFT DELETING LEADING space.
  lf_parameter2 = i_eai-task-param_2.
  SHIFT lf_parameter2 LEFT DELETING LEADING space.
  lf_parameter3 = i_eai-task-param_3.
  SHIFT lf_parameter3 LEFT DELETING LEADING space.
  lf_parameter4 = i_eai-task-param_4.
  SHIFT lf_parameter4 LEFT DELETING LEADING space.
  CASE lf_parameter2.
    WHEN 'C'.   "Control Parameter
      l_eh_flags-cntrl   = c_true.
    WHEN 'I'.   "Info Parameter
      l_eh_flags-info   = c_true.
  ENDCASE.
* Get EH Data
  l_eh_data = i_eai-eh_model->eh_data( l_eh_flags ).
  clear lv_found.
  LOOP AT l_eh_data-cntrl INTO ls_cntrl
   WHERE param_name = lf_parameter1.
    lv_found = 'X'.
    PERFORM return_param USING ''
                               ls_cntrl-param_value
                               lf_parameter3
                               lf_parameter4
                         CHANGING rc.
  ENDLOOP.
  LOOP AT l_eh_data-info INTO ls_info
    WHERE param_name = lf_parameter1.
    lv_found = 'X'.
    PERFORM return_param USING ls_info-param_value
                               ''
                               lf_parameter3
                               lf_parameter4
                         CHANGING rc.
  ENDLOOP.
  check lv_found = 'X'.
  CHECK rc = 0.
  e_return_code = 0.
ENDFUNCTION.

*----------------------------------------------------------------------*
***INCLUDE LZSAPEM_OTCF01 .
*&---------------------------------------------------------------------*
*&      Form  RETURN_PARAM
*&---------------------------------------------------------------------*
*      -->P_INFO_VALUE  Value of the current parameter
*      -->P_CNTRL_VALUE  Value of the current parameter
*      -->P_PARAM3  Value against which to compare
*      -->P_PARAM4  Operator
*      <--P_rc  Return Code
*----------------------------------------------------------------------*
FORM return_param  USING    p_info_value  TYPE /saptrx/paramvalue_255
                            p_cntrl_value TYPE /saptrx/paramvalue_60
                            p_param3 TYPE /saptrx/activity_param_id
                            p_param4 TYPE /saptrx/activity_param_id
                   CHANGING p_rc LIKE sy-subrc.
  DATA: p_value(255) TYPE c,
        p_val_num1(15) TYPE p,
        p_val_num2(15) TYPE p.
  IF NOT p_info_value IS INITIAL.
    p_value = p_info_value.
  ENDIF.
  IF NOT p_cntrl_value IS INITIAL.
    p_value = p_cntrl_value.
  ENDIF.
  p_rc = 4.
  CASE p_param4.
    WHEN '=' OR 'EQ' OR ' '.
      IF p_value = p_param3.
        p_rc = 0.
      ENDIF.
    WHEN '>' OR 'GT'.
      IF p_value CO '0123456789. '.
        p_val_num1 = p_value.
      ENDIF.
      IF p_param3 CO '0123456789. '.
        p_val_num2 = p_param3.
      ENDIF.
      CHECK NOT p_val_num1 IS INITIAL AND NOT p_val_num2 IS INITIAL.
      IF p_val_num1 > p_val_num2.                         "#EC PORTABLE
        p_rc = 0.
      ENDIF.
    WHEN '<' OR 'LT'.
      IF p_value CO '0123456789. '.
        p_val_num1 = p_value.
      ENDIF.
      IF p_param3 CO '0123456789. '.
        p_val_num2 = p_param3.
      ENDIF.
      CHECK NOT p_val_num1 IS INITIAL AND NOT p_val_num2 IS INITIAL.
      IF p_val_num1 < p_val_num2.                         "#EC PORTABLE
        p_rc = 0.
      ENDIF.
    WHEN '<>' OR 'NE'.
      IF p_value <> p_param3.
        p_rc = 0.
      ENDIF.
    WHEN '>=' OR 'GE'.
      IF p_value CO '0123456789. '.
        p_val_num1 = p_value.
      ENDIF.
      IF p_param3 CO '0123456789. '.
        p_val_num2 = p_param3.
      ENDIF.
      CHECK NOT p_val_num1 IS INITIAL AND NOT p_val_num2 IS INITIAL.
      IF p_val_num1 >= p_val_num2.                        "#EC PORTABLE
        p_rc = 0.
      ENDIF.
    WHEN '<=' OR 'LE'.
      IF p_value CO '0123456789. '.
        p_val_num1 = p_value.
      ENDIF.
      IF p_param3 CO '0123456789. '.
        p_val_num2 = p_param3.
      ENDIF.
      CHECK NOT p_val_num1 IS INITIAL AND NOT p_val_num2 IS INITIAL.
      IF p_val_num1 <= p_val_num2.                        "#EC PORTABLE
        p_rc = 0.
      ENDIF.
    WHEN 'CA'.
      IF p_value CA p_param3.
        p_rc = 0.
      ENDIF.
    WHEN 'CP'.
      IF p_value CP p_param3.
        p_rc = 0.
      ENDIF.
    WHEN 'CN'.
      IF p_value CN p_param3.
        p_rc = 0.
      ENDIF.
    WHEN 'CO'.
      IF p_value CO p_param3.
        p_rc = 0.
      ENDIF.
    WHEN 'NA'.
      IF p_value NA p_param3.
        p_rc = 0.
      ENDIF.
    WHEN 'CS'.
      IF p_value CS p_param3.
        p_rc = 0.
      ENDIF.
    WHEN 'NP'.
      IF p_value NP p_param3.
        p_rc = 0.
      ENDIF.
    WHEN OTHERS.
      IF p_value = p_param3.
        p_rc = 0.
      ENDIF.
  ENDCASE.
ENDFORM.                    " RETURN_PARAM