Salesforce DML Exception Cannot have more than 10 chunks in a single operation

Salesforce DML Exception Cannot have more than 10 chunks in a single operation

Exception:

System.TypeException: Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking.

This exception is thrown when List < sObject > variable has more than 10 records of different types in a mixed fashion of multiple objects records.

Sample Code to reproduce the issue:

List< sObject > listRecords = new List<sObject>();

for ( Integer i = 0; i <= 5; i++ ) {

    listRecords.add(
        new Account( Name = 'Test Account' + i )
    );
    listRecords.add(
        new Lead( 
            FirstName = 'Test' + i, 
            LastName = 'Example' + i,
            Email = 'test' + i + '@test.com',
            Company = 'Testing Inc.' + i
        )
    );
    listRecords.add(
        new Contact( 
            FirstName = 'Test' + i, 
            LastName = 'Example' + i,
            Email = 'test' + i + '[email protected]'
        )
    );

}

insert listRecords;

To fix the issue, sort the list so that the records are sorted by objects.

List< sObject > listRecords = new List<sObject>();

for ( Integer i = 0; i <= 5; i++ ) {

    listRecords.add(
        new Account( Name = 'Test Account' + i )
    );
    listRecords.add(
        new Lead( 
            FirstName = 'Test' + i, 
            LastName = 'Example' + i,
            Email = 'test' + i + '@test.com',
            Company = 'Testing Inc.' + i
        )
    );
    listRecords.add(
        new Contact( 
            FirstName = 'Test' + i, 
            LastName = 'Example' + i,
            Email = 'test' + i + '[email protected]'
        )
    );

}

listRecords.sort();
insert listRecords;

Leave a Reply