While working on S/4HANA On Premise system; you will often come across requirements where custom functionality is required on data changes whether its triggering an Outbound Interface / Sending approval mail etc. . On high level, you can build solution using either of below listed approach
I thought to pen down the use cases and steps to enable the Custom events for both the scenarios in my Blog series as this might help people working on similar requirements. We have used S/4HANA 2020 FPS01 for the use cases. I will be covering below topics in Business Events series.
- Enable Change Pointer and Custom Event for Custom Table
- Trigger , Trace and Debug Custom Event
- Event Mesh: Enabling for Custom Events
- Event Mesh: Enabling Topic for Custom Events
To start with in this Blog we will discuss how to trigger Change Pointers and Custom Events for a Custom table in S/4HANA On Premise System .While searching for this requirement; I could find some good blogs explaining SCDO Configuration using Update FM to trigger events . But now since events are class based; I thought to cover the detailed steps in this Blog post. These Custom Events we will later use for Event Mesh set up too.
Let’s take a scenario of Custom table where we need to track the changes in one field of table like Quantity. In case Quantity is Changed; it should be updated in Change Pointers CDHDR and CDPOS and custom event should be raised to pass the Custom table data either to middleware /Event Mesh Queue.
- In Data Element of Quantity field, check the checkbox for Change Document
- In Transaction SCDO, Create Change Document Object for this case giving table name and make a note of the class that gets generated ZCL_ZMATERIAL_CHDO
- You can find the details of generated Objects; by checking on generated Information.
- For Testing, you can directly execute the method WRITE of class ZCL_ZMATERIAL_CHDO or you can write custom program to call this to generate Change pointers for our Quantity field. Example when I directly executed the method from the class ; it generated the changenumber.
- SAP has given sample code in the generated class that can be used in your Custom Report ; you just need to COPY+PASTE the code from the generated custom class. Below is the sample code generated for my class
*” uncomment the needed parts
*” change names if needed
*” Start of default parameter part
* DATA: objectid TYPE cdhdr-objectid,
* tcode TYPE cdhdr-tcode,
* planned_change_number TYPE cdhdr-planchngnr,
* utime TYPE cdhdr-utime,
* udate TYPE cdhdr-udate,
* username TYPE cdhdr-username,
* cdoc_planned_or_real TYPE cdhdr-change_ind,
* cdoc_upd_object TYPE cdhdr-change_ind VALUE ‘U’,
* cdoc_no_change_pointers TYPE cdhdr-change_ind.
* DATA: cdchangenumber TYPE cdhdr-changenr.
*” End of default parameter part
*” Begin of dynamic DATA part for class ZCL_ZMATERIAL_CHDO
*” table with the NEW content of: ZMATERIAL_CHG
* DATA XZMATERIAL_CHG TYPE ZCL_ZMATERIAL_CHDO=>TT_ZMATERIAL_CHG.
*” table with the OLD content of: ZMATERIAL_CHG
* DATA YZMATERIAL_CHG TYPE ZCL_ZMATERIAL_CHDO=>TT_ZMATERIAL_CHG.
*” change indicator for table: ZMATERIAL_CHG
* DATA UPD_ZMATERIAL_CHG TYPE IF_CHDO_OBJECT_TOOLS_REL=>TY_CDCHNGINDH.
*” Change Number of Document
* DATA CHANGENUMBER TYPE IF_CHDO_OBJECT_TOOLS_REL=>TY_CDCHANGENR.
*” End of dynamic DATA part for class ZCL_ZMATERIAL_CHDO
*” Begin of method call part
*” define needed DATA for error handling
* DATA err_ref TYPE REF TO cx_chdo_write_error.
* DATA err_action TYPE string.
* TRY.
* CALL METHOD ZCL_ZMATERIAL_CHDO=>write
* EXPORTING
* objectid = objectid
* tcode = tcode
* utime = utime
* udate = udate
* username = username
* planned_change_number = planned_change_number
* object_change_indicator = cdoc_upd_object
* planned_or_real_changes = cdoc_planned_or_real
* no_change_pointers = cdoc_no_change_pointers
*” End of default method call part
*” Begin of dynamic part for method call
*” table with the NEW content of: ZMATERIAL_CHG
* XZMATERIAL_CHG = XZMATERIAL_CHG
*” table with the OLD content of: ZMATERIAL_CHG
* YZMATERIAL_CHG = YZMATERIAL_CHG
*” change indicator for table: ZMATERIAL_CHG
* UPD_ZMATERIAL_CHG = UPD_ZMATERIAL_CHG
*” Change Number of Document
* IMPORTING
* changenumber = cdchangenumber.
* CATCH cx_chdo_write_error INTO err_ref.
*” MESSAGE err_ref TYPE ‘A’.
*” error information could be determined with default GET_TEXT, GET_LONGTEXT, GET_SOURCE_POSITION methds
* ENDTRY.
*” End of dynamic part for method call
- You can add this sample code in your Custom Report to Create change pointers. This can be added after INSERT/UPDATE on custom table so that once table is updated; change pointer can be triggered.
- Once the Change Pointer is created; you can raise the Event for the same Object Key using below Code :
DATA: container_table TYPE TABLE OF swcont.
DATA: objkey TYPE sweinstcou-objkey.
DATA: event_container TYPE REF TO if_swf_ifs_parameter_container,
event TYPE REF TO if_swf_evt_event.
“Set Container and Raise the event ‘CHANGED’
event_container = cl_swf_evt_event=>get_event_container(
im_objcateg = cl_swf_evt_event=>mc_objcateg_cl
im_objtype = ‘ZCL_EVENT_CUSTOMTABLE’
im_event = ‘CHANGED’ ).
CHECK event_container IS BOUND.
CONCATENATE sy-mandt p_matnr p_werks INTO objkey.”Create Object Key
event = cl_swf_evt_event=>get_instance(
im_objcateg = cl_swf_evt_event=>mc_objcateg_cl
im_objtype = ‘ZCL_EVENT_CUSTOMTABLE’
im_event = ‘CHANGED’
im_objkey = objkey
im_event_container = event_container ).
IF event IS BOUND.
TRY .
event->raise( ).
CATCH cx_swf_evt_invalid_objtype cx_swf_evt_invalid_event.
RETURN.
ENDTRY.
COMMIT WORK.
ENDIF.
This code will trigger the CHANGED event of the Custom Class and functionality written inside CHANGED event will get triggered.
In this blog post, we have covered how to create change pointers on Custom table and trigger Custom Event. Detailed steps on how to create Custom Event class and events is covered in the subsequent Blog post: Part 2.
Regards
Taranam
Source: https://blogs.sap.com/2021/05/31/business-events-in-s-4hana-on-premise-system-triggering-custom-change-pointers-and-custom-event-on-custom-table-part-1/