Wednesday, September 6, 2017

ABAP Example: Creating an event in SAP EM using a BAPI call

You can use SAP Event Management to manage the statuses of your business processes. To communicate with EM you need to send it events. There are several ways of sending events to EM including via IDoc, BAPI call, RFC call and XML call. 
The syntax for sending an event using the BAPI /SAPTRX/BAPI_EH_ADDEVENTMSG_02 is described in detail below. In addition to showing you how to call the BAPI it also shows how to call the BAPI using a a qRFC leveraging a specific outbound queue name.

Here is the extract of ABAP code dealing with calling /SAPTRX/BAPI_EH_ADDEVENTMSG_02:

  data :
** Event Message data
         ls_trackingheader type /saptrx/bapi_evm_header,         ls_trackeemodify  type /saptrx/bapi_evm_ee_modify,
         lv_evtcnt type /saptrx/bapi_evm_header-evtcnt,
         lv_modcnt type /saptrx/bapi_evm_header-evtcnt,
* BAPI Structures
       lt_bapi_evm_header type standard table of /saptrx/bapi_evm_header,
         lt_trackeemodify   type standard table of /saptrx/bapi_evm_ee_modify,
         lt_bapireturn      type standard table of bapiret2,
* Working variables
        new_date type sy-datum,
         new_time type sy-uzeit,
* BAPI Calling structures
         v_last_digit,
         v_queue type trfcqout-qname,
         lt_trxserv type standard table of /saptrx/trxserv,
         lw_trxserv type /saptrx/trxserv. 
*** SET HEADER DATA FOR EVENT **
   Tracking ID = **
** MODIFY EXPECTED EVENT ENTRY **
  ls_trackeemodify-evtcnt = lv_evtcnt.
  ls_trackeemodify-evtact = ‘I’.   “Insert, U=Update, D=Delete
  ls_trackeemodify-language = sy-langu.
**   Set new message and actual Event Date / Time / Timezone
*  New_date = sy-datum + 1.
  New_time = sy-uzeit.
  ls_trackeemodify-etxdat = ls_trackeemodify-msgdat = new_date.
  ls_trackeemodify-etxtim = ls_trackeemodify-msgtim = new_time.
  ls_trackeemodify-evtid    = ‘INV_CREATED’.
  lv_modcnt = lv_modcnt + 1.
  ls_trackeemodify-modcnt = lv_modcnt.
  append ls_trackeemodify to lt_trackeemodify.
**** CHANGE QUEUE NAME FOR OUTBOUND COMMUNICATION TO EVENT MANAGER **
  v_last_digit = vbeln+9(1).
  case v_last_digit.
    when 1 or 2.
      concatenate ‘Z_SORDER’ ‘A’ into v_queue.
    when 3 or 4.
      concatenate ‘Z_SORDER’ ‘B’ into v_queue.
    when 5 or 6.
      concatenate ‘Z_SORDER’ ‘C’ into v_queue.
    when 7 or 8.
      concatenate ‘Z_SORDER’ ‘D’ into v_queue.
    when 9 or 0.
      concatenate ‘Z_SORDER’ ‘E’ into v_queue.
  endcase.
  condense v_queue.
  call function ‘TRFC_SET_QUEUE_NAME’
    exporting
      qname                    = v_queue
   exceptions
     invalid_queue_name       = 1
     others                   = 2 .
  if sy-subrc <> 0.
    Message i999(b1) with ‘Failed to change queue name!’.
  endif.
**** CALL BAPI ** Get tracking servers
  select * from /saptrx/trxserv into table lt_trxserv.
  if sy-subrc <> 0.
    message w021(/saptrx/asc).
  endif.
  loop at lt_trxserv into lw_trxserv.
    if not lt_bapi_evm_header[] is initial.
      call function ‘/SAPTRX/BAPI_EH_ADDEVENTMSG_02’
        in background task
        destination lw_trxserv-rfcdest
        exporting
          simulate           = space
          synchronous        = space
          eh_generation_mode = ‘N’
        tables
          trackingheader     = lt_bapi_evm_header
          trackeemodify      = lt_trackeemodify
          return             = lt_bapireturn.
** Set queue for the Commit as well
      call function ‘TRFC_SET_QUEUE_NAME’
        exporting
          qname                    = v_queue
       exceptions
         invalid_queue_name       = 1
         others                   = 2 .
      if sy-subrc <> 0.
    Message i999(b1) with ‘Failed to change queue name!’.
      endif.
      call function ‘BAPI_TRANSACTION_COMMIT’
        in background task
        destination lw_trxserv-rfcdest.
    endif.
     refresh lt_bapi_evm_header.
    refresh lt_trackeemodify.
    refresh lt_bapireturn.

  endloop.