---
schema_version: "1"
id: 7ba92009befb6043e569c355d51879c28e2aafd6dce6fdf3395eb121c9b82dbf
title: Use Batch Apex Queries With Real-Time Event Monitoring
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/real_time_event_monitoring_batch_query.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.real_time_event_monitoring_batch_query.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: bd2f01098f1722b51f83ed642e5a657b7418f533b3e31f66bbf886d4c1eddb7c
description: Use Bulk API and batch Apex to query real-time events.
products:
  - Salesforce Platform
  - Cross Cloud
release: "264"
---
# Use Batch Apex Queries With Real-Time Event Monitoring

Use Bulk API and batch Apex to query real-time events.

This example shows how to query and analyze an event HBPO by using a field’s contents.

```
public class EventMatchesObject implements Database.Batchable<sObject> {
    private String lastEventDate;

    public EventMatchesObject(String lastEventDateParam) {
        lastEventDate = lastEventDateParam;
    }

    public Iterable<SObject> start(Database.BatchableContext bc) {
        return [SELECT EventDate, EventIdentifier, QueriedEntities, SourceIp, Username, UserAgent FROM ApiEvent WHERE EventDate > lastEventDate LIMIT 50000]
    }

    public void execute(Database.BatchableContext bc, List<ApiEvent> events){
        // Process this list of entities if a certain attribute matches
        for (ApiEvent event: events) {
            String objectString = 'Patent__c';
            String eventIdentifier = event.EventIdentifier;
            if (eventIdentifier.contains(objectString) {
                // Perform actions on the event that contains 'Patent__c'
            }
            lastEventDate = format(event.EventDate);
        }
    }

    public void finish(Database.BatchableContext bc){         
        // You can daisy chain additional calls using EventDate or other filter fields to get around the 50k governor limit
        EventMatchesObject nextBatch = new EventMatchesObject(lastEventDate);
        Database.executeBatch(nextBatch);
    }
}
```