Newer Version Available
Big Objects Queueable Example
To read or write to a big object using a trigger, process, or flow from a sObject,
use asynchronous Apex. This example uses the asynchronous Apex Queueable interface to isolate DML operations on
different sObject types to prevent the mixed DML error.
| Available in: both Salesforce Classic and Lightning Experience |
| Available in: Enterprise, Performance,
Unlimited, and Developer Editions for up to 1 million
records Additional record capacity and Async SOQL query available as an add-on license. |
Example
This trigger occurs when a case record is inserted. It calls a method to insert a
batch of big object records and demonstrates a partial failure case in which some
records succeed and some fail. To create metadata files for the Customer_Interaction__b object in this example,
use the XML excerpts in the Create Metadata Files for Deployment
example.
1// CaseTrigger.apxt
2
3trigger CaseTrigger on Case (before insert) {
4 if (Trigger.operationType == TriggerOperation.BEFORE_INSERT){
5 // Customer_Interaction__b has three required fields in its row key, in this order:
6 // 1) Account__c - lookup to Account
7 // 2) Game_Platform__c - Text(18)
8 // 3) Play_Date__c - Date/Time
9 List<Customer_Interaction__b> interactions = new List<Customer_Interaction__b>();
10
11 // Assemble the list of big object records to be inserted
12 for (Case c : Trigger.new) {
13 Customer_Interaction__b ci = new Customer_Interaction__b(
14 Account__c = c.AccountId,
15 // In this example, the Case object has a custom field, also named Game_Platform__c
16 Game_Platform__c = c.Game_Platform__c,
17 Play_Date__c = Date.today()
18 );
19 interactions.add(ci);
20 }
21
22 // CustomerInteractionHandler is an asynchronous queuable Apex class
23 CustomerInteractionHandler handler = new CustomerInteractionHandler(interactions);
24 System.enqueueJob(handler);
25 }
26}The trigger uses the Queueable Apex interface to asynchronously call a method to insert into a big object.
1// CustomerInteractionHandler.apxc
2
3public class CustomerInteractionHandler implements Queueable {
4
5 private List<Customer_Interaction__b> interactions;
6
7 public CustomerInteractionHandler(List<Customer_Interaction__b> interactions) {
8 this.interactions = interactions;
9 }
10
11 /*
12 * Here we insert the Customer Interaction big object records,
13 * or log an error if insertion fails.
14 */
15 public void execute(QueueableContext context){
16
17 List<ExceptionStorage__c> errors = new List<ExceptionStorage__c>();
18
19 try {
20 // We have to use insertImmediate() to insert big object records.
21 List<Database.SaveResult> srList = Database.insertImmediate(interactions);
22
23 // Check the save results from the bulk insert
24 for (Database.SaveResult sr: srList) {
25 if (sr.isSuccess()) {
26 System.debug('Successfully inserted Customer Interaction.');
27 } else {
28 for (Database.Error err : sr.getErrors()) {
29 // Display an error message if the insert failed
30 System.debug(err.getStatusCode() + ': ' + err.getMessage() + '; ' +
31 'Error fields: ' + err.getFields());
32
33 // Write to a custom object, such as ExceptionStorage__c
34 // for a more durable record of the failure
35 ExceptionStorage__c es = new ExceptionStorage__c(
36 name = 'Error',
37 ExceptionMessage__c = (err.getMessage()).abbreviate(255),
38 ExceptionType__c = String.valueOf(err.getStatusCode()),
39 ExceptionFields__c = (String.valueOf(err.getFields())).abbreviate(255)
40 );
41 errors.add(es);
42 }
43 }
44 }
45 }
46 catch (Exception e) {
47 // Exception occurred, output the exception message
48 System.debug('Exception: ' + e.getTypeName() + ', ' + e.getMessage());
49
50 // Write any errors to a custom object as well
51 ExceptionStorage__c es = new ExceptionStorage__c(
52 name = 'Exception',
53 ExceptionMessage__c = e.getMessage(),
54 ExceptionType__c = e.getTypeName()
55 );
56 errors.add(es);
57 }
58
59 // If any errors occurred, save the ExceptionStorage records
60 if (errors.size() > 0) {
61 insert errors;
62 }
63 }
64}