Friday, January 24, 2025

Salesforce Integration Interview Questions Platform Event Part-5

 1) How can you publish a platform event using Apex?

You can publish a platform event using the EventBus.publish() method in Apex. Here's how:

  • Create an instance of the platform event object and populate its fields.
  • Use EventBus.publish() to publish the event, which returns a Database.SaveResult.
  • Check isSuccess() on the result to determine whether the event was published successfully.
TestEvent__e event = new TestEvent__e(
    Customer_Country__c = 'India',
    Customer_State__c = 'Maharashtra',
    Government_Customer__c = true
);
Database.SaveResult result = EventBus.publish(event);
if (result.isSuccess()) {
    System.debug('Platform event published successfully.');
} else {
    for (Database.Error error : result.getErrors()) {
        System.debug('Error: ' + error.getMessage());
    }
}

2) What is the difference between ReplayId and EventUuid in platform events?

  • ReplayId: A system-assigned field that identifies the position of an event in the event stream. It isn’t guaranteed to be unique, especially during Salesforce maintenance activities.
  • EventUuid: A universally unique identifier (UUID) introduced in API version 52.0 and later. It is always unique and should be used to identify a platform event message.

  • Example of Retrieving EventUuid:

    Database.SaveResult result = EventBus.publish(event);
    if (result.isSuccess()) {
        System.debug('UUID=' + EventBus.getOperationId(result));
    }

    Using EventUuid ensures the identification of an event message across different scenarios, including maintenance activities.

    3) What are the Apex governor limits for platform event triggers?

    Although platform event triggers run asynchronously, the synchronous limits apply to platform event triggers. This is because Asynchronous limits are for long-lived processes, such as Batch Apex and future methods. Synchronous limits are for short-lived processes that execute quickly and platform event triggers are short-lived processes that execute in batches rather quickly.

    Because a platform event trigger runs in a separate transaction from the one that fired it, governor limits are reset, and the trigger gets its own set of limits.

    4) How can you configure the user and batch size for a platform event trigger?

    You can override the default running user and batch size of a platform event trigger using PlatformEventSubscriberConfig via Tooling API or Metadata API.
    • Default Configuration: Runs as the Automated Process user with a batch size of 2,000.
    • Custom Configuration: Specify a custom user and a batch size between 1 and 2,000. Smaller batch sizes help avoid hitting governor limits.
    5) How to create and manipulate PlatformEventSubscriberConfig?

    We can create PlatformEventSubscriberConfig from workbench as shown below. To add a configuration, perform a POST request to this endpoint.

    Salesforce Integration Interview Questions Platform Event


    Also, you can query retrieve the configurations in your org with SOQL. If querying from the Developer Console Query Editor, ensure you select Use Tooling API. This example query retrieves all configurations set up in your Salesforce org.

    SELECT Id,DeveloperName,BatchSize,PlatformEventConsumerId,UserId FROM PlatformEventSubscriberConfig

    To get or manipulate a configuration, use this endpoint with the ID of your PlatformEventSubscriberConfig record appended.

    /services/data/v60.0/tooling/sobjects/PlatformEventSubscriberConfig/<ID>

    You can also,

    ·         Delete a specific configuration with a DELETE request.

    ·         Update a specific configuration with a PATCH request. For this request, include the PlatformEventSubscriberConfig definition in the request body.

    6How do you subscribe to platform events using CometD?

    To subscribe to platform events, use the channel name /event/Event_Name__e in a CometD client. For example, to subscribe to a TestEvent__e platform event, use the channel /event/TestEvent__e. The CometD client receives real-time event notifications in JSON format.

    7) How can you encrypt platform event messages in the event bus?

    To encrypt platform event messages:

    1. Create an event bus tenant secret in the Key Management page in Setup.
    2. Enable encryption for platform events on the Encryption Policy page.
      Without Shield Encryption, messages are stored in clear text in the event bus.
    8) How can you monitor platform event usage?

    Use the PlatformEventUsageMetric object to monitor event publishing and delivery usage. Example queries:

    To retrieve the usage of platform events:

    Events Delivered (April 26, 2024, 11:00 to April 27, 2024, 11:00):

    SELECT Name, StartDate, EndDate, Value 

    FROM PlatformEventUsageMetric 

    WHERE Name='PLATFORM_EVENTS_DELIVERED' 

    AND StartDate=2024-04-26T11:00:00.000Z 

    AND EndDate=2024-04-27T11:00:00.000Z

    Events Published (April 26, 2024, 11:00 to April 27, 2024, 11:00):

    SELECT Name, StartDate, EndDate, Value 
    FROM PlatformEventUsageMetric 
    WHERE Name='PLATFORM_EVENTS_PUBLISHED' 
    AND StartDate=2024-04-26T11:00:00.000Z 
    AND EndDate=2024-04-27T11:00:00.000Z

    Publishing Methods: Apex, APIs (Pub/Sub API), Flows, and Process Builder.

    Subscription Methods: CometD, Pub/Sub API, empApi Lightning components, and event relays.

    Note: Delivery metrics exclude event deliveries to Apex triggers, flows, and processes.

    Gain a deep understanding of Salesforce integration, from creating and configuring Connected Apps to mastering advanced topics like OAuth flows, SAML-based Single Sign-On, and Streaming APIs. Our PDF course combines practical examples, real-time scenarios, and integration patterns to equip professionals with the skills needed to streamline processes and enhance productivity. Tailored for those with 2–8 years of experience, it’s your guide to unlocking seamless connectivity between Salesforce and other systems.

    Link to course : Mastering Salesforce Integration

    No comments:

    Post a Comment