Wednesday, August 3, 2011

Adding functions to events using CRMV_EVENT

Another great tool to influence the system is adding Callback Functions in CRMV_EVENT. This is a totally different mechanism from last weeks' IC Event mechanism.
The similarity between the two is that both enable you to add custom coding to standard. The difference is that IC events occur because they are raised in specific coding (can be anytime), while the events available in CRMV_EVENT are always triggered from an operation on a oneorder object (where a oneorder object can be a salesorder, activity, service order, etc etc.).

The transaction
Let's take a look at the transaction CRMV_EVENT.

In the top there is a 'Read This Information' button, which actually contains valuable information.

In the next block, called Assignments, you can see 5 fields, where the last field is the Callback field. This field contains the functionmodule to be called, and the 4 fields above are the determination fields. So logically explained, if the situation occurs where the 4 fields are true, the funtion in the callback field is called.

In the next block, called Definitions, you can find the customizing of the Events, Objects etc which are used for the determination. Under normal circumstances, there is no need to adjust anything here, except in the Object Function / Callback option. Here, you are able to add a custom Z-function, which you can then use in the Assignments. I will show this later.

The last block called 'Checks' is a report that tells you which functions will be called for a specific transaction type or item type. This basically is a report that helps you determine which functions are raised.

Instead of adding entries to CRMV_EVENT as described here, it's probably better to add the callback function in transaction SM30 --> CRMV_EVENT_CUST, or via the IMG (SPRO-->CRM-->Transactions-->Basic Settings-->Edit Event Handler Table). The result will be the same, but this is probably a more upgrade-proof solution.

Let's say you want a certain field to be occupied with a certain value based on certain values in the transaction. You could then build a function where you first do a CRM_ORDER_READ (reads from the buffer, so pretty quick), followed by your logic and a CRM_ORDER_MAINTAIN to update the document. It is not recommended to add a commit in a function that is called by an event. Add this function to the object on event '78 Before Saving'.

The Attribute field
The attribute field can be used to filter on a specific attribute value. For instance, if you use object STATUS, you can add value E0004 as a value in the attribute field, so the function is only called if the status has been changed to 'E0004'. The attribute field cannot be used on all processing times (for instance not on the save).

Be careful
Events in CRMV_EVENTS is a powerful tool. You can implement all kind of logic. Be aware of performance though, because if you add a lot of logic on for instance time 1 (immediately), your logic will be processed on every change (not saved, but changed). Of course, there are examples where this is needed (take a look at what standard SAP has on time Immediately), but be cautious. Also, be aware of nesting your function (for instance if you implement a change of the document in your function and schedule your function on change of the document...).