This is Part 2 of the below series of blogs:
- Real-time notifications and workflow using ABAP Push Channels (websockets) Part 1: Creating the APC and AMC
- Real-time notifications and workflow using ABAP Push Channels (websockets) Part 2: Broadcasting the message via Workitem exits
- Real-time notifications and workflow using ABAP Push Channels (websockets) Part 3: Creating a UI5 Application for receiving notifications
- Real-time notifications and workflow using ABAP Push Channels (websockets) Part 4: Creating a Web Dynpro ABAP Application for receiving notifications
- Real-time notifications and workflow using ABAP Push Channels (websockets) Part 5: Using the UI5 or Web Dynpro applications in Side Panels
There are many ways to approach the broadcasting of the message. You could either do this via user-exits, BADI’s, implicit enhancement points, using Event Type Linkages such as in transaction SWETYPV as used by SAP Business workflow and using a Receiver Function Module instead of a workflow or any other way you can think of.
However, in my case, as I want to notify a user when a work item has been created, it seemed appropriate to use a work item Program Exit class handler. Each use case should be assessed on a case by case basis and use the appropriate method. You may choose to do it differently if you find a more efficient way of doing it.
So in my example, I’m going to use a Parked Journal Approval workflow and in the dialog activity step I’m going to go to the Program Exits tab. There you will see you can enter any number of classes to be used as Program exits. If you go to the F1 help of the class field, it shows that the class must implement the IF_SWF_IFS_WORKITEM_EXIT interface and the code executed must be in the method EVENT_RAISED.
We now need to create an Exit handler class which implements this interface.
And implement the following code in the IF_SWF_IFS_WORKITEM_EXIT~EVENT_RAISED method.
DATA: lo_producer_text TYPE REF TO if_amc_message_producer_text.
DATA: lx_amc_error TYPE REF TO cx_amc_error.
DATA: lv_channel_ext TYPE amc_channel_extension_id.
* Check the work item event, it could be any of the following:
* BEF_CREAT – before creation
* CREATED – after creation
* BEF_EXEC – before execution
* AFT_EXEC – after execution
* AFT_ASYINV – after async invoke
* BEF_REMOVE – before remove
* STATE_CHG – state changed
* AFT_REXEC – after rule exec
* BEF_ACTION – before action
* AFT_ACTION – after_action
* BEF_DECI – before decision
* In our case we are going to broadcast a message for CREATED event
DATA(ls_header) = im_workitem_context->get_header( ).
DATA(lo_wi) = cl_swf_run_wim_factory=>find_by_wiid( im_wiid = ls_header–wi_id ).
* Get the workitem agents to broadcast the message to
DATA(lt_agents) = lo_wi->get_agents( ).
* Create the notification message, which will just be ‘You have a new work item:’ + work item text
DATA(lv_message) = ‘You have a new work item:‘ && cl_abap_char_utilities=>horizontal_tab && ls_header–wi_text.
LOOP lt_agents AT ASSIGNING FIELD–SYMBOL(<ls_agent>) WHERE otype = ‘US‘.
lv_channel_ext = sy–mandt && <ls_agent>–objid.
lo_producer_text ?= cl_amc_channel_manager=>create_message_producer( i_application_id = ‘ZAMC_WF_NOTIFY‘
i_channel_id = ‘/workflow_notify’
i_channel_extension_id = lv_channel_ext ).
* Send message to the AMC channel
lo_producer_text->send( i_message = lv_message ).
CATCH cx_amc_error INTO lx_amc_error.
MESSAGE lx_amc_error->get_text( ) TYPE ‘E’.
After saving and activating this class, add it as a Program Exit in the Workitem Activity step and reactivate the Workflow template.
Lastly, we want to add this Program exit class to our AMC (ABAP Messaging Channel) under the list of Authorised Programs. Do this by going back to the AMC in transaction SAMC. Entering the following:
Authorised Program: ZCL_WORKITEM_EXIT_WF_NOTIFY
Prog. Type: CLASS
Next we need to create a client which will connect to the APC(websocket) and listen for messages that are broadcast from the workitem exit.
If you are interested in doing this via a UI5 application see: Real-time notifications and workflow using ABAP Push Channels (websockets) Part 3: Creating a UI5 Application for receiving notifications
Or if you are more interested in doing this in Web Dynpro ABAP you can go straight to: Real-time notifications and workflow using ABAP Push Channels (websockets) Part 4: Creating a Web Dynpro ABAP Application for receiving notifications