Newer Version Available
Populate a Custom Big Object with Apex
You can create and update custom big object records in Apex using the Database.insertImmediate() method.
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. Even if your filter string matches the value that was inserted, leading and trailing white space isn’t compared, and so no rows are matched by the filter.
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.
If you set values to NULL when upserting into a custom big object, the fields aren’t updated if they have existing values. To set these values to NULL, delete the field and recreate it.
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.
1// Define the record.
2PhoneBook__b pb = new PhoneBook__b();
3pb.FirstName__c = 'John';
4pb.LastName__c = 'Smith';
5pb.Address__c = '1 Market St';
6pb.PhoneNumber__c = '555-1212';
7database.insertImmediate(pb);
8// A single record will be created in the big object.1// Define the record with the same index values but different phone number.
2PhoneBook__b pb = new PhoneBook__b();
3pb.FirstName__c = 'John';
4pb.LastName__c = 'Smith';
5pb.Address__c = '1 Market St';
6pb.PhoneNumber__c = '415-555-1212';
7database.insertImmediate(pb);
8// The existing records will be "re-inserted". Only a single record will remain in the big object.1// Define the record with the different index values and different phone number
2PhoneBook__b pb = new PhoneBook__b();
3pb.FirstName__c = 'John';
4pb.LastName__c = 'Smith';
5pb.Address__c = 'Salesforce Tower';
6pb.PhoneNumber__c = '415-555-1212';
7database.insertImmediate(pb);
8// A new record will be created leaving two records in the big object.