Skip to main content

Create custom system events

Warning: System event handlers (any Apex class that implements the SE_ISystemEventHandler or SE_ISystemEventHandlerWithGetters Apex interfaces) do not support the use of data manipulation language (DML) when processing a system event.

To create a custom system event:

  1. Create a System Event (mvn__SE_System_Event__mdt) metadata record. Key fields include:

    • System Event Name - enter a unique API name for the record.

    • Payload Apex Class Name - specify the payload type for the system event. If your custom system event uses a list of SObjects as the payload, leave this field blank. If the system event uses a list of custom Apex class objects as the payload, enter the API name of a custom Apex class. The custom Apex class must implement the SE_ISystemEventApexClassPayload interface:

      global interface SE_ISystemEventApexClassPayload {
String getUniqueId();
}

:::: ::: title Payload :::

/** Custom class that implements the SE_ISystemEventApexClassPayload interface */

          public class MyCustomPayload implements SE_ISystemEventApexClassPayload {
/** Private variable to store this instance's unique Id */
private String uniqueId;

/** Constructor that initializes the instance with an unique Id */

          public MyCustomPayload() {
this.uniqueId = UUIDGenerator.generateId();
}

/** Implementation of the SE_ISystemEventApexClassPayload interface method that returns the unique Id */

          public String getUniqueId() {
return this.uniqueId;
}
}
::::
  • Payload Apex Class Namespace - enter the namespace of the Apex class defined in the Payload Apex Class Name field.

  • Priority - enter the order in which the custom system event should be processed. A system event with a priority of 1 is processed before a system event with a priority of 2.

For a list of all the System Event fields, visit the System Event custom metadata type.

  1. Create a System Event Configuration (mvn__SE_System_Event_Configuration__mdt) metadata record record. Key fields include:

    • System Event - associate the System Event metadata record that you created in step 1.

    • Handler Name - enter the API name of the Apex class that implements the associated System Event metadata record. The custom Apex class must implement the SE_ISystemEventHandler interface:

      /**

      • @author
      • @description Interface for the System Events handlers */
      global interface SE_ISystemEventHandler {
/**
* Processes the SE_System_Event_Configuration__mdt records for the given new and old objects.
*
* The input variables are generic so that any type of payload in the form of an Object/SObject can be passed,
* the implementation is responsible for casting them to the appropriate type to be able to process them.
*
* @param config the SE_System_Event_Configuration__mdt being processed
* @param newRecords the list of new/updated payload Objects
* @param oldRecordsMap the map of old SObject records sorted by Id when the routers processes SObjects
*/
void process(SE_System_Event_Configuration__mdt config, List<Object> newRecords, Map<Id, SObject> oldRecordsMap);
}

In addition to implementing the SE_ISystemEventHandler interface, the handler can optionally implement the SE_ISystemEventHandlerWithGetters interface. This interface allows the handler to create emails, Salesforce notifications, Chatter posts, and audit logs.

/**

  • @author
  • @description Interface for the System Event handlers that also implement getters for notification, chatter posts
  • and/or SObjects */
      global interface SE_ISystemEventHandlerWithGetters extends SE_ISystemEventHandler {
/**
* @return the List of generated SE_SalesforceNotification records that represent the custom notifications
*/
List<SE_SalesforceNotification> getSalesforceNotifications();

/**

  • @return the List of generated SE_EmailMessage that represent the emails */ List<SE_EmailMessage> getEmailMessages();

/**

  • @return the List of generated SE_ChatterFeedPost that represent the chatter posts */ List<SE_ChatterFeedPost> getChatterFeedPosts();

/**

  • @return the List of generated generic SObject records to insert (e.g. document audit log entries) */ List<SObject> getSObjects(); }

For a list of all the System Event Configuration fields, visit the System Event Configuration custom metadata type.