Home > SAP CRM > SAP CRM – Event Handler

SAP CRM – Event Handler

October 28th, 2009 Leave a comment Go to comments

The event handler is a standard tool in the processing of a business transaction, for communication between the different objects that influence each other. It is used to call function modules, named callbacks that are accessed when triggering an event at a certain time.Lots of SAP standard functionalities are implemented using SAP CRM event handler.

Use transaction code: CRMV_EVENT to view or create event handler.

For example, if there is a requirement that the program needs to populate a zfield in structure orderadm_i after calculating the price using IPC.

Step 1. We can define an event as showed in below image.

event_handler

Object: PRIDOC,Event: AFTET_CHANGE means if the pricing document has been changed, the callback function module will be called automatically.

The callback function module needs to be added in table CRMC_FUNC_ASSIGN. There is no maintenance view for this table, but you can directly add a new entry in SE16.

Step 2. Define the call back function module.

The function module interface is fixed and slight different according to the call back setting in event handler definition. You can copy the interface of standard function module which has the same technical setting.

Step 3. Test your custom event handler

Set a session break point in your event handler function. When changing the order product, quantity, If the price document changed, the function module should be called accordingly.

Below is a code sample of a custom event handler:

function z_populate_sales_rep.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IV_HEADER_GUID) TYPE  CRMT_OBJECT_GUID
*"     REFERENCE(IV_OBJECT_GUID) TYPE  CRMT_OBJECT_GUID
*"     REFERENCE(IV_OBJECT_NAME) TYPE  CRMT_OBJECT_NAME
*"     REFERENCE(IV_EVENT_EXETIME) TYPE  CRMT_EVENT_EXETIME
*"     REFERENCE(IV_EVENT) TYPE  CRMT_EVENT
*"     REFERENCE(IV_ATTRIBUT) TYPE  CRMT_EVENT
*"     REFERENCE(IV_STRVAL_OLD) TYPE  ANY OPTIONAL
*"     REFERENCE(IV_STRVAL_NEW) TYPE  ANY OPTIONAL
*"     REFERENCE(IV_RCODE_STATUS) TYPE  SY-SUBRC
*"----------------------------------------------------------------------
 
  include crm_object_names_con.
 
  constants:
        lc_zsr1   type prct_cond_type value 'ZSR1',
        lc_zsr2   type prct_cond_type value 'ZSR2',
        lc_zsr3   type prct_cond_type value 'ZSR3',
        lc_zsr4   type prct_cond_type value 'ZSR4',
        lc_zsr5   type prct_cond_type value 'ZSR5',
        lc_prefix type char8      value 'CNCCRMPR'.
 
  constants:
        lc_asop_order_types     type ztxa_xref-varname value 'ASOP_ORDER_TYPES',
        lc_asop_serv_cont_types type ztxa_xref-varname value 'ASOP_SERV_CONT_TYPES'.
 
  data: lt_header_guid       type crmt_object_guid_tab,
        ls_header_guid       type crmt_object_guid,
        lt_orderadm_h        type crmt_orderadm_h_wrkt,
        ls_orderadm_h        type crmt_orderadm_h_wrk,
        lt_requested_objects type crmt_object_name_tab,
        ls_requested_object  type crmt_object_name,
        lt_orderadm_i        type crmt_orderadm_i_wrkt,
        ls_orderadm_i        type crmt_orderadm_i_wrk,
        lt_pridoc            type crmt_pric_cond_t,
        ls_pridoc            type crmt_pric_cond,
        ls_prct_cond_du      type prct_cond_du,
        lt_input_field_names type crmt_input_field_names_tab,
        ls_fieldname         type crmt_input_field_names,
        ls_orderadm_i_com    type crmt_orderadm_i_com,
        lt_doc_links         type crmt_doc_flow_extdt,
        lt_doc_flow_field_names type crmt_input_field_tab.
 
  data: lt_xref_asop_order_types type table of ztxa_xref,
        ls_xref_asop_order_type  type ztxa_xref,
        lt_xref_asop_serv_cont_types type table of ztxa_xref,
        ls_xref_asop_serv_cont_type type ztxa_xref.
 
  data: lv_tname  type tabname16,
        lv_soldto type crmt_sold_to_party,
        lv_bpguid type bu_partner_guid_bapi,
        lv_bpnum  type bu_partner.
 
* get project order type
  call function 'ZXA_GET_XREF_VALUES'
    exporting
      varname               = lc_asop_order_types
    tables
      xref_tab              = lt_xref_asop_order_types
    exceptions
      no_values_found       = 1
      invalid_variable_name = 2
      others                = 3.
 
* get service order type
  call function 'ZXA_GET_XREF_VALUES'
    exporting
      varname               = lc_asop_serv_cont_types
    tables
      xref_tab              = lt_xref_asop_serv_cont_types
    exceptions
      no_values_found       = 1
      invalid_variable_name = 2
      others                = 3.
 
* first read the admin item
  call function 'CRM_ORDERADM_I_READ_OW'
    exporting
      iv_guid           = iv_object_guid
    importing
      es_orderadm_i_wrk = ls_orderadm_i
    exceptions
      item_not_found    = 1
      others            = 2.
 
  check sy-subrc = 0.
 
  move-corresponding ls_orderadm_i to ls_orderadm_i_com.
 
  ls_header_guid = iv_header_guid.
  insert ls_header_guid into table lt_header_guid.
 
  insert gc_object_name-orderadm_h into table lt_requested_objects.
  insert gc_object_name-pridoc into table lt_requested_objects.
 
* read the price document information
  call function 'CRM_ORDER_READ'
    exporting
      it_header_guid       = lt_header_guid
      it_requested_objects = lt_requested_objects
    importing
      et_orderadm_h        = lt_orderadm_h
      et_pridoc            = lt_pridoc
    exceptions
      document_not_found   = 1
      error_occurred       = 2
      document_locked      = 3
      no_change_authority  = 4
      no_display_authority = 5
      no_change_allowed    = 6
      others               = 7.
 
  check sy-subrc = 0.
 
  read table lt_orderadm_h into ls_orderadm_h index 1.
 
* check process type is not project order
  read table lt_xref_asop_order_types into ls_xref_asop_order_type with key value1 = ls_orderadm_h-process_type.
  check sy-subrc <> 0.
 
* check process type is not service contract
  read table lt_xref_asop_serv_cont_types into ls_xref_asop_serv_cont_type with key value1 = ls_orderadm_h-process_type.
  check sy-subrc <> 0.
 
  read table lt_pridoc into ls_pridoc index 1.
 
  if ls_orderadm_i_com-zz_salesrep_1 is initial and
     ls_orderadm_i_com-zz_salesrep_2 is initial and
     ls_orderadm_i_com-zz_salesrep_3 is initial and
     ls_orderadm_i_com-zz_salesrep_4 is initial and
     ls_orderadm_i_com-zz_salesrep_5 is initial and
     ls_orderadm_i_com-zz_pct_1 is initial and
     ls_orderadm_i_com-zz_pct_2 is initial and
     ls_orderadm_i_com-zz_pct_3 is initial and
     ls_orderadm_i_com-zz_pct_4 is initial and
     ls_orderadm_i_com-zz_pct_5 is initial.
* loop all the condition type belongs to current item line
    loop at ls_pridoc-pric_cond into ls_prct_cond_du where kposn = iv_object_guid.
 
* get the cusotmer guid
      clear: lv_soldto,
             lv_tname,
             lv_bpnum.
 
      concatenate lc_prefix ls_prct_cond_du-kotabnr into lv_tname.
 
      if ls_prct_cond_du-kschl = lc_zsr1.
 
        select single sold_to_party into lv_soldto
          from (lv_tname)
          where varnumh = ls_prct_cond_du-knumh.
 
        if sy-subrc = 0.
* read the business id from guid
 
          lv_bpguid = lv_soldto.
 
          call function 'BAPI_BUPA_GET_NUMBERS'
            exporting
              businesspartnerguid = lv_bpguid
            importing
              businesspartnerout  = lv_bpnum.
 
* build the change fields
          ls_orderadm_i_com-zz_salesrep_1 = lv_bpnum.
          ls_orderadm_i_com-zz_pct_1 = ls_prct_cond_du-kbetr / 10.
          ls_fieldname-fieldname = 'ZZ_SALESREP_1'.
          insert ls_fieldname into table lt_input_field_names.
          ls_fieldname-fieldname = 'ZZ_PCT_1'.
          insert ls_fieldname into table lt_input_field_names.
 
        endif.
 
      endif.
 
      if ls_prct_cond_du-kschl = lc_zsr2.
 
        select single sold_to_party into lv_soldto
          from (lv_tname)
          where varnumh = ls_prct_cond_du-knumh.
 
        if sy-subrc = 0.
* read the business id from guid
 
          lv_bpguid = lv_soldto.
 
          call function 'BAPI_BUPA_GET_NUMBERS'
            exporting
              businesspartnerguid = lv_bpguid
            importing
              businesspartnerout  = lv_bpnum.
 
* build the change fields
          ls_orderadm_i_com-zz_salesrep_2 = lv_bpnum.
          ls_orderadm_i_com-zz_pct_2 = ls_prct_cond_du-kbetr / 10.
          ls_fieldname-fieldname = 'ZZ_SALESREP_2'.
          insert ls_fieldname into table lt_input_field_names.
          ls_fieldname-fieldname = 'ZZ_PCT_2'.
          insert ls_fieldname into table lt_input_field_names.
 
        endif.
 
      endif.
 
      if ls_prct_cond_du-kschl = lc_zsr3.
 
        select single sold_to_party into lv_soldto
          from (lv_tname)
          where varnumh = ls_prct_cond_du-knumh.
 
        if sy-subrc = 0.
* read the business id from guid
 
          lv_bpguid = lv_soldto.
 
          call function 'BAPI_BUPA_GET_NUMBERS'
            exporting
              businesspartnerguid = lv_bpguid
            importing
              businesspartnerout  = lv_bpnum.
 
* build the change fields
          ls_orderadm_i_com-zz_salesrep_3 = lv_bpnum.
          ls_orderadm_i_com-zz_pct_3 = ls_prct_cond_du-kbetr / 10.
          ls_fieldname-fieldname = 'ZZ_SALESREP_3'.
          insert ls_fieldname into table lt_input_field_names.
          ls_fieldname-fieldname = 'ZZ_PCT_3'.
          insert ls_fieldname into table lt_input_field_names.
 
        endif.
 
      endif.
 
      if ls_prct_cond_du-kschl = lc_zsr4.
 
        select single sold_to_party into lv_soldto
          from (lv_tname)
          where varnumh = ls_prct_cond_du-knumh.
 
        if sy-subrc = 0.
* read the business id from guid
 
          lv_bpguid = lv_soldto.
 
          call function 'BAPI_BUPA_GET_NUMBERS'
            exporting
              businesspartnerguid = lv_bpguid
            importing
              businesspartnerout  = lv_bpnum.
 
* build the change fields
          ls_orderadm_i_com-zz_salesrep_4 = lv_bpnum.
          ls_orderadm_i_com-zz_pct_4 = ls_prct_cond_du-kbetr / 10.
          ls_fieldname-fieldname = 'ZZ_SALESREP_4'.
          insert ls_fieldname into table lt_input_field_names.
          ls_fieldname-fieldname = 'ZZ_PCT_4'.
          insert ls_fieldname into table lt_input_field_names.
 
        endif.
 
      endif.
 
      if ls_prct_cond_du-kschl = lc_zsr5.
 
        select single sold_to_party into lv_soldto
          from (lv_tname)
          where varnumh = ls_prct_cond_du-knumh.
 
        if sy-subrc = 0.
* read the business id from guid
 
          lv_bpguid = lv_soldto.
 
          call function 'BAPI_BUPA_GET_NUMBERS'
            exporting
              businesspartnerguid = lv_bpguid
            importing
              businesspartnerout  = lv_bpnum.
 
* build the change fields
          ls_orderadm_i_com-zz_salesrep_5 = lv_bpnum.
          ls_orderadm_i_com-zz_pct_5 = ls_prct_cond_du-kbetr / 10.
          ls_fieldname-fieldname = 'ZZ_SALESREP5'.
          insert ls_fieldname into table lt_input_field_names.
          ls_fieldname-fieldname = 'ZZ_PCT_5'.
          insert ls_fieldname into table lt_input_field_names.
 
        endif.
 
      endif.
 
    endloop.
 
* maintain the changes on adm item data
 
    call function 'CRM_ORDERADM_I_CHANGE_OW'
      exporting
        is_orderadm_i_com       = ls_orderadm_i_com
      changing
        ct_input_field_names    = lt_input_field_names
        ct_doc_links            = lt_doc_links
        ct_doc_flow_field_names = lt_doc_flow_field_names
      exceptions
        item_change_error       = 1
        item_create_error       = 2
        item_delete_error       = 3
        item_create_wref_error  = 4
        item_not_created_no_msg = 5
        item_not_created        = 6
        error_occurred          = 7
        others                  = 8.
 
  endif.
 
endfunction.
  1. Jaya Nair
    June 1st, 2010 at 14:42 | #1

    Hello,

    This is good.
    I was also looking for IPC settings.
    Can you please give some input on IPC settings.

  1. No trackbacks yet.