Custom triggers requires encapsulating a portion of ABAP code in a custom function module. The event-detection code is written as a function module to ensure that the processing remains separate from the transaction. Any tables or variables used from the transaction must be passed to the function module by value and not by reference.
To minimize the effects of locking a business object when retrieving an event, the function module typically executes in an update-task mode. To avoid inconsistencies, do not use update task if the function module is already being called within a process that is in an update-task mode.
To minimize the impact in the transaction, place the function module within another include program. Using an include program allows you to make changes to custom code rather than to SAP code.
The event-detection code contains logic that identifies the object for the event. For example, the sales order transaction handles many types of orders, but only one order type is required. This logic is in the event-detection code. The general strategy for placing this event-detection code is to insert it just before the data is committed to the database. The function module containing the event detection code is typically created as a part of the function group for the business object.
To implement a custom trigger for event detection:
CASE HEADER_CHANGE_IND.
WHEN 'I'.
* The verb will always be a create if KNA1 data is entered.
IF KNA1_CREATE = 'X'.
HEADER_EVENT = C_CREATE_EVENT.
ELSE.
* Check if an entry is in the config table for converting a create. If
* no entry is found, the default is to convert the extension of sales
* area or company code to an update.
SELECT SINGLE * FROM /CWLD/CONF_VAL
WHERE CONF_NAME = C_CONVERT_CREATE
AND CONF_VALUE = C_FALSE_WORD.
IF SY-SUBRC = 0.
HEADER_EVENT = C_CREATE_EVENT.
ELSE.
HEADER_EVENT = C_UPDATE_EVENT.
ENDIF.
ENDIF.
WHEN 'U'.
HEADER_EVENT = C_UPDATE_EVENT.
WHEN 'E' OR 'D'.
HEADER_EVENT = C_DELETE_EVENT.
ENDCASE.
* See if it's a sold-to company.
SELECT SINGLE * FROM /CWLD/CONF_VAL
WHERE CONF_NAME = C_AGCUSTMASTER
AND CONF_VALUE = KNA1-KTOKD.
* clear temp_obj_type.
CLEAR TEMP_OBJ_NAME.
IF SY-SUBRC = 0.
* temp_obj_type = 'YXR_V51'.
TEMP_OBJ_NAME = C_OBJ_CUSTOMERMASTER.
ELSE.
* If it's not a sold-to company, check if it's another partner.
SELECT SINGLE * FROM /CWLD/CONF_VAL
WHERE CONF_NAME = C_AGCUSTPARTNER
AND CONF_VALUE = KNA1-KTOKD.
ENDIF.
CALL FUNCTION '/CWLD/ADD_TO_QUEUE_AEP'
EXPORTING
OBJ_NAME = TEMP_OBJ_NAME
OBJKEY = OBJKEY
EVENT = HEADER_EVENT
* IDOC_NUMBER =
GENERIC_RECTYPE = GENERIC_RECTYPE
IMPORTING
RECTYPE = RECTYPE
TABLES
EVENT_CONTAINER = EVENT_CONTAINER
EXCEPTIONS
OTHERS = 1.
The following code fragment illustrates the function call to the /CWLD/ADD_TO_QUEUE_IN_FUT_AEP event trigger (single key value).
DATA: DATE_IN_FUTURE LIKE SY_DATUM.
CALL FUNCTION ' /CWLD/ADD_TO_QUEUE_IN_FUT_AEP'
EXPORTING
OBJ_NAME = TEMP_OBJ_NAME
OBJKEY = OBJKEY
EVENT = HEADER_EVENT
VALID_DATE = DATE_IN_FUTURE
IMPORTING
RECTYPE = RECTYPE
TABLES
EVENT_CONTAINER = EVENT_CONTAINER
EXCEPTIONS
OTHERS = 1.