A trigger is an Apex script that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted. Triggers enable to perform custom actions before or after changes to Salesforce records.
Pre-requiste:
create platform event
How to create Trigger:
- Go to created platform event
- Go to trigger section
- Click on New button.
Use the given Code to apex trigger:
[aux_code language=”javascript” theme=”tomorrow” title=”” extra_classes=””]
trigger trgPlatformAccountEvent on Platform_Event_Account__e (after insert) {
List<Account> lst = new List<Account>();
for(Platform_Event_Account__e evt: Trigger.New) {
Account acc = new Account();
acc.Name = evt.Name__c + ‘evt’ ;
acc.AccountNumber = evt.AccountNumber__c + ‘evt’ ;
lst.add(acc);
}
insert lst;
}[/aux_code]
Make sure Apex Identifier should be correct. For example ‘evt’. if you copy code from browser then it will take char in HTML .
when the apex trigger is used to consume the platform event we can check created record and its data. All the custom fields which we added in platform event are shows with ‘evt’. For example AccountNumber+evt, name+evt.
Follow the steps to show How to use the apex trigger to consume the platform event?
Step1: Go to created integration-> Open your Interface -> go to manual Load -> Uplaod data
Go to message board -> Search for result;