Newer Version Available
Async SOQL Use Cases
Customer 360 Degree and Filtering
In this use case, administrators load various customer engagement data from external sources into Salesforce big objects and then process the data to enrich customer profiles in Salesforce. The goal is to store customer transactions and interactions, such as point-of-sale data, orders, and line items in big objects and then process and correlate that data with your core CRM data. Anchoring customer transactions and interactions with core master data provides a richer 360-degree view that translates into an enhanced customer experience.
The following example analyzes the customer data stored in the Rider record of a car-sharing service. The source big object, Rider_Record_b, has a lookup relationship with the Contact object, allowing for an enriched view of the contact’s riding history. You can see that the query includes Rider__r.FirstName, Rider__r.LastName, Rider__r.Email as part of the SELECT clause. This example demonstrates the ability to join big object data (Rider_Record__b) with Contact data (FirstName, LastName, Email) in a single Async SOQL query.
- Example URI
-
1https://yourInstance—api.salesforce.com/services/data/v38.0/async-queries/ - Example POST request body
-
1{ 2 "query": "SELECT End_Location_Lat__c, End_Location_Lon__c, End_Time__c, 3 Start_Location_Lat__c, Start_Location_Lon__c, Start_Time__c, 4 Car_Type__c, Rider__r.FirstName, Rider__r.LastName, 5 Rider__r.Email 6 FROM Rider_Record__b WHERE Star_Rating__c = '5'", 7 8 "targetObject": "Rider_Reduced__b", 9 10 "targetFieldMap": {"End_Location_Lat__c":"End_Lat__c", 11 "End_Location_Lon__c":"End_Long__c", 12 "Start_Location_Lat__c": "Start_Lat__c", 13 "Start_Location_Lon__c": "Start_Long__c", 14 "End_Time__c": "End_Time__c", 15 "Start_Time__c": "Start_Time__c", 16 "Car_Type__c": "Car_Type__c", 17 "Rider__r.FirstName": "First_Name__c", 18 "Rider__r.LastName": "Last_Name__c", 19 "Rider__r.Email": "Rider_Email__c" 20 } 21} - Example POST response body
-
1{ 2 "jobId": "08PB000000000NA", 3 4 "message": "", 5 6 "query": "SELECT End_Location_Lat__c, End_Location_Lon__c, End_Time__c, 7 Start_Location_Lat__c, Start_Location_Lon__c, Start_Time__c, 8 Car_Type__c, Rider__r.FirstName, Rider__r.LastName, 9 Rider__r.Email 10 FROM Rider_Record__b WHERE Star_Rating__c = '5'", 11 12 "status": "New", 13 14 "targetFieldMap": {"End_Location_Lat__c":"End_Lat__c", 15 "End_Location_Lon__c":"End_Long__c", 16 "Start_Location_Lat__c": "Start_Lat__c", 17 "Start_Location_Lon__c": "Start_Long__c", 18 "End_Time__c": "End_Time__c", 19 "Start_Time__c": "Start_Time__c", 20 "Car_Type__c": "Car_Type__c", 21 "Rider__r.FirstName": "First_Name__c", 22 "Rider__r.LastName": "Last_Name__c", 23 "Rider__r.Email": "Rider_Email__c" 24 }, 25 26 "targetObject": "Rider_Reduced__b" 27}
Field Audit Trail
Field Audit Trail lets you define a policy to retain archived field history data up to 10 years from the time the data was archived. This feature helps you comply with industry regulations related to audit capability and data retention.
You define a Field Audit Trail policy using the HistoryRetentionPolicy object for each object you want to archive. The field history data for that object is then moved from the History related list into the FieldHistoryArchive object at periodic intervals, as specified by the policy. For more information, see the Field Audit Trail Implementation Guide.
You can use Async SOQL to query archived fields stored in the FieldHistoryArchive object. You can use the WHERE clause to filter the query by specifying comparison expressions for the FieldHistoryType, ParentId, and CreatedDate fields, as long as you specify them in that order.
This example queries archived accounts created within the last month.
- Example URI
-
1https://yourInstance.salesforce.com/services/data/v38.0/async-queries/ - Example POST request body
-
1{ 2 "query": "SELECT ParentId, FieldHistoryType, Field, Id, NewValue, OldValue 3 FROM FieldHistoryArchive WHERE FieldHistoryType = ‘Account’ 4 AND CreatedDate > LAST_MONTH”, 5 6 "targetObject": "ArchivedAccounts__b", 7 8 "targetFieldMap": {"ParentId": "ParentId__c", 9 "FieldHistoryType": "FieldHistoryType__c", 10 "Field": "Field__c", 11 "Id": "Id__c", 12 "NewValue": "NewValue__c", 13 "OldValue": "OldValue__c" 14 } 15} - Example POST response body
-
1{ 2 "jobId": "07PB000000006PN", 3 "message": "", 4 "query": "SELECT ParentId, FieldHistoryType, Field, Id, NewValue, OldValue 5 FROM FieldHistoryArchive WHERE FieldHistoryType = ‘Account’ AND CreatedDate > LAST_MONTH”, 6 "status": "New", 7 "targetObject": "ArchivedAccounts__b", 8 "targetFieldMap": {"ParentId": "ParentId__c", 9 "targetObject": "Rider_Reduced__b" } 10 }
Event Monitoring
With Real-Time Events, you can monitor data accessed through API calls, which covers many common scenarios because more than 50% of SOQL queries occur using the SOAP, REST, or Bulk APIs. Key information about each query, such as the username, user ID, browser, and source IP address, is stored in the ApiEvent object. You can then run SOQL queries on this object to find out details of user activity in your org.
For example, let’s say you want to know everyone who viewed the contact record of your company’s CEO, Jane Doe. The key to this query is the CEO’s contact record ID: 003D000000QYVZ5. (You can also query the ID using SOQL: SELECT Id FROM Contact WHERE Name = 'Jane Doe'). You can use the following Async SOQL query to determine all users who saw their contact information, including when, how, and where they saw it.
- Example URI
-
1https://yourInstance—api.salesforce.com/services/data/v43.0/async-queries/ - Example POST request body
-
1{ 2 "query": "SELECT Query, SourceIp, Username, EventDate FROM ApiEvent 3 WHERE RecordData Like '%003D000000QYVZ5%'", 4 5 "targetObject": "QueryEvents__c", 6 7 "targetFieldMap": {"Query":"QueryString__c","SourceIp":"IPAddress__c", 8 "Username":"User__c", "EventDate":"EventDate__c", 9 "UserAgent":"UserAgent__c" 10 } 11} - Example POST response body
-
1{ 2 "jobId": "05PB000000001PQ", 3 4 "message": "", 5 6 "query": "SELECT Query, SourceIp, Username, EventDate 7 FROM ApiEvent WHERE RecordData Like '%003D000000QYVZ5%'", 8 9 "status": "Complete", 10 "targetObject": "QueryEvents__c", 11 "targetFieldMap": {"Query":"QueryString__c","SourceIp":"IPAddress__c", 12 "Username":"User__c", "EventDate":"EventDate__c", "UserAgent":"UserAgent__c" } 13 }
1curl -H "Content-Type: application/json" -X POST -d
2'{"query": "SELECT Query, SourceIp, UserAgent, Username, EventDate FROM ApiEvent WHERE
3RecordData Like'%003D000000QYVZ5%'","targetObject": "QueryEvents__c",
4"targetFieldMap": {"Query":"QueryString__c", "SourceIp":"IPAddress__c", "Username":"User__c", "EventDate":"EventDate__c",UserAgent}}'
5"https://yourInstance.salesforce.com/services/data/v43.0/async-queries" -H
6"Authorization: Bearer 00D30000000V88A!ARYAQCZOCeABy29c3dNxRVtv433znH15gLWhLOUv7DVu.
7 uAGFhW9WMtGXCul6q.4xVQymfh4Cjxw4APbazT8bnIfxlRvUjDg"Another event monitoring use case is to identify all users who accessed a sensitive field, such as Social Security Number or Email. For example, you can use the following Async SOQL query to determine the users who saw social security numbers and the records in which those numbers were exposed.
- Example URI
-
1https://yourInstance—api.salesforce.com/services/data/v43.0/async-queries/ - Example POST request body
-
1{ 2 "query": "SELECT Query, Username, RecordData, EventDate FROM ApiEvent 3 WHERE Query Like '%SSN__c%'", 4 5 "targetObject": "QueryEvents__c", 6 7 "targetFieldMap": {"Query":"QueryString__c", "Username":"User__c", 8 "EventDate":"EventDate__c", "RecordData":"Records_Seen__c" 9 } 10} - Example POST response body
-
1{ 2 "jobId": "08PB000000001RS", 3 4 "message": "", 5 6 "query": "SELECT Query, Username, RecordData, EventDate FROM ApiEvent 7 WHERE Query Like '%SSN__c%'", 8 9 "status": "Complete", 10 11 "targetFieldMap": {"Query":"QueryString__c", "Username":"User__c", 12 "EventDate":"EventDate__c", "RecordData":"Records_Seen__c" 13 }, 14 15 "targetObject": "QueryEvents__c" 16}