Newer Version Available

This content describes an older version of this product. View Latest

Populate a Custom Big Object with Apex

Use Apex to populate a custom big object.

You can create and update custom big object records in Apex using the Database.insertImmediate() method.

Apex tests that use mixed DML calls are not allowed and fail. If you write only to the Big Object, the test inserts bad data into the target big object that you have to delete manually. To contain test DML calls to the target big object, use a mocking framework with the batch Apex stub API instead.

Warning

We recommend that you use the Apex String.trim() method to remove leading and trailing white space before you insert values, especially for values in primary key fields. This best practice ensures that SOQL queries on the big objects later work as you expect. When you specify an index field in a SOQL query WHERE clause, SOQL removes any leading or trailing white space before it compares it to the actual field value. Therefore, leading and trailing white space can’t be compared to values that have been stored. Even if your filter string matches the value that was inserted, no rows are matched by the filter.

Reinserting a record with the same index but different data results in behavior similar to an upsert operation. If a record with the index exists, the insert overwrites the index values with the new data. Insertion is idempotent, so inserting data that exists doesn’t result in duplicates. Reinserting is helpful when uploading millions of records. If an error occurs, the reinsertion reuploads the failed uploads without duplicate data. During the reinsertion, if no record exists for the provided index, a new record is inserted.

If a record insert fails, the Database.insertImmediate() method doesn’t throw an exception. Instead, it returns a SaveResult object that has a getErrors() method that returns a list of Database.Error objects. Each Database.Error object contains information about an error that occurred as a result of a failed record insert. See the Apex developer guide for more information about SaveResult and an example of iterating through the errors.

Here is an example of an insert operation in Apex that assumes a table in which the index consists of FirstName__c, LastName__c, and Address__c.