Investigating changes with audit logs
Reconstruct who changed an entity, what they changed, and when with myEntityHistory and myAuditEvents queries.
Business Data Repository records creates, updates, and deletes on nine entity types, listed below. You read that record through two queries. This scenario walks you through a compliance investigation: an asset's configuration changed unexpectedly, and you need to establish who changed it, what the old and new values were, and what else that actor touched in the same session.
By the end, you'll be able to read one entity's full change history, decode the stored before-and-after values, widen the search across a whole workspace, and tie an audit entry back to your own application logs.
Prerequisites
You need an access token for the workspace you work in. The API takes the workspace from the token, so no operation on this page asks for a workspace ID. See Authentication for how to get a token and how the API chooses the workspace.
You also need an audited entity that has been changed at least once. The first step below shows how to find its ID, including when the entity was already deleted. See Which entities are audited for the list of audited entities.
How audit works
Every product that records audit events uses the same event format, filter, and pagination. Audit log explains them: what one event records, the event codes and operations, and how to filter and page. This section covers what is specific to Business Data Repository: which entities produce events and how to read their recorded values.
Both queries on this page, myAuditEvents and myEntityHistory, return only the events of the workspace your token names, with product set to bdr. They include the changes the platform made in the workspace on its own. To spot those, check whether actor is a SystemActor.
Which entities are audited
Nine entity types are recorded. aggregateType contains one of these codes:
asset
CREATED, UPDATED, DELETED
asset_group
CREATED, UPDATED, DELETED
catalog_item
CREATED, UPDATED, DELETED
custom_field_definition
CREATED, UPDATED, DELETED
device
CREATED, UPDATED, DELETED
geo_object
CREATED, UPDATED, DELETED
inventory
CREATED, UPDATED, DELETED
schedule
CREATED, UPDATED, DELETED
workspace
CREATED, UPDATED, DELETED, RESTORED
catalog_item covers every catalog item, such as asset types, tags, and catalogs. Archiving a custom field definition and restoring it with the restore action both have the operation UPDATED, because neither deletes the definition. Their event codes tell them apart: custom_field_def_archived and custom_field_def_restored. RESTORED is recorded only for a workspace that Console reactivates. Older events can also have the aggregate types integration and member, which are no longer recorded. Audit event codes lists the event code of every change.
What eventData contains
eventData holds the answer to the investigator's main question: what exactly changed. The other event fields say who changed which entity and when, while eventData holds the values themselves, so you can see the old and the new state without any other source. It's a JSON scalar with two keys:
changedFields holds one entry per field whose value differs, each with its old and new value. Every update sets a new updatedAt, so on an UPDATED event changedFields always lists at least that field.
additionalContext holds the wider context. operation names what happened (CREATE, UPDATE, DELETE, or RESTORE). before_state and after_state hold the entity's stored state immediately before and immediately after the change. Where changedFields shows only what changed, these two show the whole record.
The keys in changedFields and in both states are the property names that Business Data Repository stores internally, not the GraphQL field names. For example, an asset's type is stored as assetTypeId, and all its custom field values are in one key, customFieldsData. A reference to another entity is stored as that entity's ID. The API doesn't guarantee these keys, so use them to read a change rather than in automated logic.
Which of the two you get depends on the operation, because a newly created entity has no "before" and a deleted one has no "after":
Operation
before_state
after_state
changedFields
CREATE
absent
present
every field that has a value, with "old": null
UPDATE
present
present
only the fields that differ
DELETE
present
absent
every field that had a value, with "new": null
RESTORE
present
present
only the fields that differ
eventData is plain JSON rather than a typed GraphQL object, so you can't select individual keys inside it in the query. Request it as one field and read the values in your own code.
How to track a logged change
Find the ID of the entity to investigate
Both audit queries work with entity IDs, so start by finding the ID of the entity you're investigating. List queries take filters that narrow down the output by what you already know, such as a name or a hardware identifier. For example, to find a truck by a fragment of its name, run this query:
Response:
The same pattern works for the other audited entity types: narrow down devices by identifierContains (a fragment of an IMEI or serial number) or titleContains and use titleContains for geo objects, schedules, and asset groups.
If the entity was deleted, list queries no longer return it, but the audit log still has its events. Search the deletions of its type by event code and take aggregateId from the matching event:
The response lists one entry per deleted asset. aggregateId is the deleted entity's ID:
Either way, the ID you found is the entity ID for the rest of the scenario.
Read the entity's history
Start with myEntityHistory, which returns every audit event for one entity, newest first:
Response:
The most recent change is first. That's the one to look at.
actor is the Actor interface. Select id and title. Its types have no other fields, so select __typename to tell a User from an ApiKey, Staff, or a SystemActor.
Identify the changes
Add eventData to see what changed, field by field. Narrow down the response to the single event you're looking for with the operations filter:
Response:
changedFields shows the answer directly: Anna Weber renamed the truck. The updatedAt and version entries appear alongside it because every successful update of an asset sets a new updatedAt and increases the version that optimistic locking checks.
Widen the search to the entire workspace
You now know who made the change. To find out what else that actor did around the same time, switch to myAuditEvents, which covers the whole workspace rather than one entity, and narrow it down by actor and period:
Response:
Switching direction to ASC puts the events in the order they happened, which is what you want when retracing a sequence of actions.
Note that the first two events share a traceId. That means they came from the same request: renaming the asset and deleting a device happened in one operation.
Match audit events with your application logs
traceId is the W3C trace context trace ID from the request's traceparent header, when that header is present and well-formed. If the header is missing or malformed (wrong version, uppercase hex, an all-zero ID), the API generates a trace ID instead. Either way, you see the ID in the event's traceId and in extensions.traceId of any error the request raised. A generated ID has no match in your own logs, since your client never sent it.
A change that the platform makes on its own has a SystemActor as actor, the source type INTERNAL, and the trace ID of what caused it, such as a request or a command from Console. A scheduled run, or a Console command sent without a trace ID, gets a generated trace ID.
The title of a SystemActor names the service that made the change, such as Console Workspace Projection for a workspace that Console created or changed. Events recorded before each service had its own actor show System.
One API request can change several entities, and every event it produces has the same traceId. Filtering by it returns that whole set:
When your client sends the traceparent header on the request, the same ID appears in your own logs and tracing tools, so it's the shared value that connects the audit log to them. Use it in the other direction too: given a suspicious request in your logs, this query tells you exactly which entities it modified.
Filtering, ordering, and pagination
Both queries accept the filter, order, and pagination that every audit query shares, described in Filtering, ordering, and pagination. The products filter accepts only bdr here.
For example, to see every deletion of an asset or a device in one day, run this query:
You can also fetch a single Business Data Repository audit event by its ID through the node query:
Error handling
Audit queries return the errors described in Audit log errors. A products filter that names any product other than bdr is refused with VALIDATION_ERROR.
How to handle an empty result set
An empty nodes array is a normal response, not an error. Work through the likely causes in this order:
The filter matches no recorded value. An event code in
eventCodesmatches only when its letter case is the same as the recorded code, and a code that Business Data Repository never records matches nothing.RESTOREDis recorded only for a workspace.The change isn't audited. Only the nine types listed above produce events, and some changes aren't recorded at all, such as a change to a user account or an asset group membership. See Which entities are audited.
The period is wrong.
fromandtoare compared againstoccurredAtin UTC. Make sure your values are in UTC too.The entity belongs to another workspace.
myEntityHistoryonly searches the workspace that your token names, so an entity from a different workspace returns nothing.
See also
Audit log: How the audit log works in every product, and the shared filter and errors
Audit event codes: The codes of every product with their operations
Audit: Complete reference for the Business Data Repository audit queries
Pagination: Page through large result sets with cursors
Optimistic locking: Prevent concurrent updates from overwriting each other with
version
Last updated
Was this helpful?