Platform Event Trigger Handler
We can use Breezz in an easy way to handle incoming Platform Events.
Step 1: Create Platform Event
For this example I will create a simple Platform Event with some potential Lead fields.
Step 2: Create Apex Trigger
Next we need to create a trigger class. For that we can use Generate Trigger Code functionality provided in the triggers menu. Simply go to Breezz Setup → Triggers → Generate Trigger Code.
Pick our newly created Platform Event and after insert trigger event. Next click Copy to Clipboard.
trigger IncomingLeadTrigger on IncomingLead__e (after insert) {
forvendi.BreezzApi.TRIGGERS.handle();
}
Your code should look like the one provided above. Next you need to create a trigger class in your IDE or Developer Console. To create in Developer Console go to Setup → Developer Console → File → New → Apex Trigger.
To know more about Salesforce Triggers please check the Salesforce Trigger Documentation.
Step 3: Create Trigger Config
To create Trigger Configuration go to Breezz Setup → Triggers → New or simply create Trigger config from Dashboard.
Choose the object name, for our purpose it will be IncomingLead__e. Select the type of event which will be After Insert. Next input is Order of execution. Name of the trigger config should be meaningful. Next Create Default Step Group will be set to yes. Step Group for after insert IncomingLead__e will be created automatically.
Step 4: Create Step Class
Let’s start from Step class creation. You can read more about this here.
1public with sharing class IncomingLeads extends forvendi.Step {
2 public IncomingLeads() {
3 super(IncomingLeads.class.getName());
4 }
5
6
7 public override Boolean initRecordProcessing(Object record, Object optionalOldRecord) {
8 // we can cast record parameter to IncomingLead__e because step we are planning to pass list of IncomingLead__e
9 IncomingLead__e incomingLead = (IncomingLead__e)record;
10
11
12 // create new Lead object based on incoming Platform Events.
13 Lead lead = new Lead(
14 FirstName = incomingLead.FirstName__c,
15 LastName = incomingLead.LastName__c,
16 Email = incomingLead.Email__c,
17 Phone = incomingLead.Phone__c,
18 Status = incomingLead.Status__c,
19 Company = incomingLead.Company__c);
20
21
22 // using Modification Context to insert record in to the database
23 getContext().addToInsert(lead);
24
25
26 // returning false - because we have all we need to finish process
27 return false;
28 }
29}
This class will add a new Lead to the organization, based on fields in the incoming Platform Event.
Step 5: Create a Test Method for the Step class
You can read more about how to test Step here 4.6.1 How to test Step.
Here we have a test method to our IncomingLeads step.
1@IsTest
2private class IncomingLeadsTest {
3
4
5 @TestSetup
6 static void testSetup() {
7 // initializing Breezz setup,
8 // you have to provide forvendi.BreezzPlugins.BaseApexPlugin implementation if you would like to
9 // use public classes like IncomingLeads in Breezz
10 forvendi.BreezzApi.TESTS.init('BreezzPlugin');
11 }
12 @IsTest
13 static void when_IncomingLead_eIsPublished_expect_LeadWillBeCreated() {
14
15
16 IncomingLead__e incomingLead = new IncomingLead__e(
17 firstName__c = 'Tom', lastName__c = 'Stewart',
18 email__c = 'stewaart123@ww.xx',
19 phone__c = '111222333',
20 status__c = 'New - Not Contacted',
21 company__c = 'Company ABC');
22 List<IncomingLead__e> incomingLeads = new List<IncomingLead__e>{incomingLead};
23
24
25 Test.startTest();
26 forvendi.ModificationContext ctx = forvendi.BreezzApi.STEPS.build()
27 .addStep(new IncomingLeads())
28 .execute(incomingLeads);
29 Test.stopTest();
30
31
32 forvendi.BreezzAPI.TESTS.assertErrorLogs();
33
34
35 Assert.areEqual(1,ctx.getRecordsToInsert().size());
36 }
37}
Step 6: Register Step
To create Step go to Breezz Setup → Step Groups → choose a previously created group → Click New Step.
In Step Class Name choose previously created class: IncomingLeads. Choose the name, for our purpose it will be IncomingLeads. Add some Description. Next input Order of execution.
Step 7: Test Platform Event Trigger Handler
After completing all previous steps go to Setup → Developer Console → Debug → Open Execute Anonymous Window
Paste below code and click execute.
1IncomingLead__e incomingLead = new IncomingLead__e(
2 firstName__c = 'Tom',
3 lastName__c = 'Stewart',
4 email__c = 'stewaart123@ww.xx',
5 phone__c = '111222333',
6 status__c = 'New - Not Contacted',
7 company__c = 'Company ABC');
8
9EventBus.publish(incomingLead);
We create a Platform Event here and after publishing it, logic from our trigger will automatically run.
Now we can open our Leads on the platform and we can see new ones.