You are here:
Use Apex to Configure Record Creation in Batches for High-Volume Sites
Experience Cloud site users can create more cases, leads, and custom objects per second when they’re collected into batches and processed asynchronously rather than one at a time. Batch processing improves performance and keeps your site moving during heavy traffic spikes. You can invoke batch record creation using the Apex method added for this purpose.
Required Editions
| Available in: Salesforce Classic and Lightning Experience |
| Available in: Enterprise, Developer, Performance, and Unlimited Editions |
| User Permissions Needed | |
|---|---|
| To configure record and case creation in batches: | Use Micro-Batching to Create Records |
| To set the Created By field of a record created to the original user: | Set Audit Fields upon Record Creation |
| To publish an Experience Cloud site: |
|
| To edit Apex classes: |
|
The following applies to authenticated users who create records in batches using micro-batching:
- The Use Micro-Batching to Create Records profile permission is required.
- They own all records that they create via micro-batching.
- Field-level security and record type access are bypassed when creating the records. They apply after the record is created.
- Records created via micro-batching use the default record type for the org when created via micro-batching.
- When creating a child record, only Read access on the parent record is required.
The Apex Network.createRecordAsync method has three parameters:
- The first parameter
processTypemust be set to 'GENERIC'. - The second parameter
sObjectcan’t be empty or null. It must contain a valid value representing a supported object. Client-side validations are performed through the end-user experience before the submission of the record for creation using micro-batching. - The third parameter is an array of file attachment links. The file attachments must be created ahead of time. This parameter is optional, and available to authenticated users only
Salesforce uses the Automated Process user to create records in batches on behalf of the original users. Site administrators with the Set Audit Fields upon Record Creationpermission can set the Created By field of the record created to the original user. Otherwise, the Created By field is set to Automated Process user.
When guest users create records via micro-batching, the Created By field is set to Guest User, however, guest users don’t own the created records.
- From Setup, in the Quick Find box, enter Apex Classes, and then select Apex Classes.
-
Use this function for the Apex controller that manages the batch record creation of the
object that you want to create.
To create cases in batches:
Case mycase = new Case( Subject = 'sample case', Description = 'case description');
String uuid = Network.createRecordAsync('GENERIC', mycase, [enter ContentDocumentIDs separated by commas])
To verify that micro-batching is working as expected via Apex, test the createRecordAsync method in the Apex class by adding the test class definition. For example:
This is the apex class
public class MicrobatchCreateRecord {
public string microbatchCreateCase(String subject) {
Case c = new Case();
c.subject = subject;
String uuid = Network.createRecordAsync('GENERIC', c);
return uuid;
}
}This is the apex test class:
@IsTest
public class MicrobatchCreateRecord_Test {
@IsTest
public static void testMicrobatchCreateCase() {
string newCaseSubject = 'ApexTestCase1';
MicrobatchCreateRecord controller = new MicrobatchCreateRecord();
Test.startTest();
string uuid = controller.microbatchCreateCase(newCaseSubject);
Assert.isNotNull(uuid);
Test.getEventBus().deliver();
Test.stopTest();
// Assert that the Case was created and has the expected subject
List<Case> testCases = [Select Id, Subject from Case];
Assert.areEqual(1, testCases.size());
Assert.areEqual(newCaseSubject, testCases[0].subject);
}
}