Salesforce Apex Setup Audit trail


Starting with Winter ’16, Salesforce allowing access Setup Audit Trail object via API. Which is having access audit trail data more than 6 months but no official confirmation on how much depth you can go into history

SetupAuditTrail is not a supported standard controller. Its support only queries and retrieve API calls.

You can use SOQL joins to get the information you need more quickly. For example, running SELECT CreatedBy.Name FROM SetupAuditTrail returns the first and last names of the people to make changes in Setup.

You can use where Cause and Order By Cause to perform an operation on data retrieval.

SELECT CreatedDate, CreatedBy.Username, Display, Section, Action, DelegateUser FROM SetupAuditTrail WHERE CreatedDate >= 2016-09-10T00:00:00Z AND CreatedDate <= 2016-10-01T00:00:00Z ORDER BY CreatedDate DESC.

Aggregate queries aren’t supported on SetupAuditTrail object.

Fields Supported: –

SetupAuditTrail Objects supports these fields namely Action, DelegateUser, Display, Section,

Examples: –

List<SetupAuditTrail> listAudits = [SELECT Id,  Action, CreatedBy.Name, CreatedDate,Display,Section FROM SetupAuditTrail WHERE CreatedBy.Email LIKE ‘’];

Another Example with Last few years information.

SELECT Id,Action,CreatedBy.Name,CreatedDate,Display,Section FROM SetupAuditTrail WHERE CreatedDate = LAST_N_YEARS:8 Order By CreatedDate DESC   Limit 10

Here is the visual force page UI

Audit trail Image

Here is Github URL for the complete example.


Leave a Reply