Edit

Share via


Publish business events using User Data Function (UDF) and get notified via email using Activator

This tutorial walks you through an end-to-end workflow for publishing business events using a user data function and setting up an Activator rule to react to those events with email notifications. The steps you follow are:

  1. Create a business event and define its schema in Real-Time hub.
  2. Create a user data function in your workspace that publishes a business event.
  3. Validate the published events in Real-Time hub.
  4. Create an Activator rule that listens for the business event and triggers an email notification when the event occurs.

Important

This feature is in preview.

Create a new business event

  1. Go to Business events in Real-Time hub.

  2. Select + New business event and then select Create new schema.

    Screenshot that shows the Business events page with + New Business event selected.

  3. Define the business event schema.

    1. For Name, enter OrderPlaced.

    2. In the right pane, for Event schema set, select Create.

      Screenshot that shows the Define business event page.

    3. Enter Orders for the schema set name.

      Screenshot that shows the name of the event schema set.

    4. Select Add row in the middle plane.

      Screenshot that shows the selection of the Add row button.

    5. Select string for event type, and enter OrderID for the name.

      Screenshot that shows the configuration of the business event property.

    6. Repeat the previous step to add the following properties: CustomerID (string), Quantity (string), OrderTotal (string), Status (string), DiscountApplied (string)

      Screenshot of the business event schema properties configuration.

    7. Select Next to continue.

  4. Review and confirm the configuration, and then select Create to create your business event.

    Screenshot of the review and confirm page for creating a business event.

  5. Confirm that you see the business event in the list of business events in Real-Time hub.

    Screenshot of the list of business events in Real-Time hub.

Configure user data function to publish business events

  1. After creating the business event, open another tab in your web browser, and go to your workspace.

  2. Select + New item and then search for User data functions and select it.

    Screenshot of the new user data function selection.

  3. Enter PublishOrderPlacedEvent as the name for the user data function, and then select Create.

    Screenshot of creating a new user data function in the workspace.

  4. Select New function to create a new function in the user data function item.

    Screenshot of the new function button.

  5. In the Home ribbon, select Manage connections.

    Screenshot of the Manage connections button in the Home ribbon.

  6. In the Connections pane, select + Add connection.

    Screenshot of the Add connection button in the Connections pane.

  7. Search for Orders, select the schema set you created earlier, and then select Connect.

    Screenshot of selecting the business event schema set for the connection.

  8. In the Manage connection pane, follow these steps:

    1. Confirm that the connection is listed under the Connected to this item section.

    2. Note down the alias for the connection, which is Orders by default.

    3. Select X in the top-right corner to close the pane.

      Screenshot of Manage connections pane.

  9. Replace the existing code in the function with the following code.

    import fabric.functions as fn 
    import logging 
    
    udf = fn.UserDataFunctions() 
    @udf.connection(argName="businessEventsClient", alias="Orders") 
    
    @udf.function() 
    def PublishOrders(businessEventsClient: fn.FabricBusinessEventsClient) -> str: 
        logging.info("PublishOrders invoked.") 
    
        # Prepare the event data payload 
    
        event_data = { 
            "OrderID": "12345",  
            "CustomerID": "C-10",
            "Quantity": "12",
            "OrderTotal": "23.21",
            "Status": "shipped",
            "DiscountApplied": "5%"
        } 
    
        # Generate the business event 
    
        businessEventsClient.PublishEvent( 
            type="OrderPlaced",  
            event_data=event_data,  
            data_version="v1" 
        ) 
    
        return "Event 'OrderPlaced' published successfully." 
    
    
  10. In the Functions explorer pane, hover over the function you modified, select the ⋯ (three dots) menu, and then select Test.

    Screenshot of the Functions explorer with the Test option selected.

  11. In the Test window, select Test to run the function, and confirm that it works as expected. You should see the output message Event 'OrderPlaced' published successfully. in the output pane.

    Screenshot of the Test window for the user data function.

Verify published business events

  1. Switch to the tab with Real-Time hub page or select Real-Time on the left navigation pane.

  2. Go to Business events in Real-Time hub.

  3. Select the OrderPlaced business event.

    Screenshot of the Business events page with the event selected.

  4. In the Publisher tab, confirm that you see an event and the function is listed in the publisher list.

    Screenshot of the OrderPlaced event page with a sample event and the publisher information.

  5. Select the Data preview tab.

  6. In the publisher filter, select the name of the function (PublishOrderPlacedEvent), which is a publisher.

  7. Visualize the event in the preview table.

Create an Activator trigger to consume events and send email notifications

  1. Select the Real-Time hub icon in the left navigation pane of the Fabric portal.

  2. In the Real-Time hub, select Business events under the Subscribe to category.

  3. In the Business events list, locate OrderPlaced event. Select either the ⚡ lightning icon or the ⋯ (three dots) menu next to the event, and then select Set alert.

    Screenshot of selecting the Set alert option for a business event.

  4. On the Add rule page, in the Details section, for Rule name, enter a name for the rule. For example, OrderPlaced_Rule.

  5. In the Condition section, for Check, select On each event.

  6. In the Action section, select one of the following actions. To configure the alert to trigger a function when the condition is met, follow these steps:

    1. For Select action, select Email.

    2. For To, enter the email address where you want to receive the notification when the event occurs.

    3. For Subject, enter the subject of the email notification (for example, Order Placed Event Detected).

    4. For Body, enter the content of the email notification. While entering the content, enter @ to access event properties to include in the email. You can also use dynamic content by selecting the icon next to each text box.

      Screenshot of configuring the email action for the Activator rule.

  7. In the Save location section, for Workspace, select the workspace where you want to create the Fabric activator item.

  8. For Item, select the drop-down list, and then select Create a new item.

  9. For New item name, enter a name for the new Fabric activator item (for example, OrderPlacedActivator), and then select Create.

    Screenshot of the specifying a name for the activator item.

  10. You see the Alert created page with a link to open the rule in the Fabric activator user interface in a separate tab.

    Screenshot of the Alert created page.

  11. Select Open to open the rule in the Fabric Activator user interface.

Test the solution

Publish another event

  1. Switch to the tab with the user data function.

  2. Test the function again to publish another OrderPlaced event by selecting the Test button in the Test window.

View business event in the Real-Time hub

Publishers tab

  1. In the Real-Time hub, select Business events in the left menu.

  2. Select OrderPlaced from the list of business events.

  3. In the Publisher tab, confirm that you see the newly published event and the old event you published at the beginning of the tutorial. If you don't see the new event, select the refresh button to refresh the list of events. It might take a few seconds for the new event to appear in the list.

  4. In the list of publishers, confirm that the function you used to publish the event is listed as a publisher.

    Screenshot of the Publishers tab of the business event page.

Consumers tab

  1. Switch to the Consumers tab.

  2. Confirm that you see an event was delivered within the last hour.

  3. Confirm that the activator you created is listed as a consumer.

    Screenshot of the Consumers tab of the business event page.

Data preview tab

  1. Switch to the Data preview tab.

  2. Confirm that you see the consumer filter with the activator you created.

    Screenshot of the consumer filter in the Data preview tab.

View business event in the activator run history

  1. Go to the workspace where you created the activator item, and open the activator item (for example, OrderPlacedActivator) if it's not already open.

  2. In the activator item, select the History tab.

  3. Confirm that you see one activation. In the Activation details pane at the bottom, you see the activation details that include context information you wanted to include in the email notification.

    Screenshot of the history tab for the Activator rule.

Check your email for the notification

You should receive an email notification with the subject and content you configured in the email action of the activator rule. The email should also include the context information from the business event that you included in the email body.

Screenshot of the email notification received.