Skip to content

Event Streaming Tutorial

Matt and Neil edited this page Feb 22, 2019 · 8 revisions

Time Estimate - about 20 mins

In this tutorial we explore alternative approaches for integrating Sagas and Services.

Common Initial Processing

All events are inserted with an operation similar to the following:

The "to" parameter controls which Saga or Service will execute.

:bulb: Tip: Events are always persisted. But an event cannot be processed if the to parameter is invalid

Specific Integrations

Streaming events can be created in many ways:

  • Apex - DML insert
  • Apex - Dynamic DML
  • Apex - Service.enqueue
  • Json - Workflow Task enqueue
  • Json - Process Builder enqueue
  • Json - Flow enqueue
  • Button/Link - Bulk API enqueue (CSV file)
  • Button/Link - URL parameter enqueue

Apex - DML insert

String event = JSON.serialize( new Map<String,Object>{'to' => 'ETL.LogService'} );
insert new ETL__Stream__c(ETL__Event__c = event);

Apex - Dynamic DML

SObject stream =  Schema.getGlobalDescribe().get('ETL__Stream__c').newSObject();
String event = JSON.serialize( new Map<String,Object>{'to' => 'ETL.LogService'} );
stream.put('ETL__Event__c', event);
insert stream;

Apex - Service.enqueue

  • Single Event
Map<String,Object> event = new Map<String,Object>{'to' => 'ETL.LogService'};
String jobId = Service.enqueue(event);
  • Multiple Events
List<Map<String,Object>> events = new List<Map<String,Object>>{
    new Map<String,Object>{'to' => 'ETL.LogService'},
    new Map<String,Object>{'to' => 'ETL.LogService'}
};
String jobId = Service.enqueue(events);

Json - Workflow Task enqueue

  1. For a selected SObject, create a workflow rule that evaluates to true when a record is created, and every time it's edited.
  2. For the Action, create a Task that similar to the following:
:bulb: Tip: The status is unused; the task is deleted immediately once the event is enqueued.

Json - Process Builder enqueue

  1. For a selected SObject, create a process that evaluates to true when a record is created, and every time it's edited.
  2. Create a create a New Record action similar to the following:

Json - Flow enqueue

  1. Create a JSON representation of the Stream event by creating a variable
  2. Connect a Create Record step in your flow referencing the JSON event variable

Button/Link - Bulk API enqueue (CSV file)

  1. Create a CSV file - each row represents a business Event in JSON
  • Name the first row Event
  • Export to CSV
  1. Note the escaped the double quotes in the exported file.
  2. Open the Streams app in Salesforce
  3. Navigate to the Streams tab - if the help page appears, click Continue to skip.
  4. Click Import.
  5. Using the Data Import Wizard, select the Stream Custom Object and accept the Event field mapping

Button/Link - URL parameter enqueue

  1. For a selected SObject, create a Button that enqueues to a specific saga or service.
  2. Set the Behaviour to Display in existing window without sidebar or header.
  3. Set the Content Source to URL.
  4. Enter the following Formula
/apex/ETL__Enqueue?to=ETL.LogService
  1. Click Save.
  2. Add the Button to the Page Layout:

Key Takeaway

  • There are numerous approaches to integrate your business events into Streams
  • All approaches offer guaranteed persistence with immediate handoff back to the caller

Next Steps

You can’t perform that action at this time.