The project that I am working on has an API endpoint that is meant to upload a large number of records into three different tables: Careers, Degrees, and CareerDegreeRelationships. The endpoint is suppose to upload all the careers, all the degrees, and then all of the career degree relationships.

For reference,

  • There is 800+ careers and 500+ degrees, but there is 10,000+ CareerDegreeRelationships, so I know I needed to take a different approach in inserting those records.
  • Each degree and career have a externalId reference that are used as a reference for creating the career degree relationships:
  • A sample JSON might look like:
"careers": [
    "externalId": 1,
    "name": "Career 1"
    "externalId": 2,
    "name": "Career 2"
"degrees": [
    "externalId": 101,
    "name": "Degree 1"
    "externalId": 102,
    "name": "Degree 2"
"careerDegreeRelationships": [
    "career": 1;
    "degree": 102;

    "career": 2,
    "degree": 101

Here is basically what I did:

// Insert Careers and Degrees
List<Career__c> careers = new List<Career__c>();
for (Career career : requestBody.careers) {
    Career__c newCareer = new Career__c();
    newCareer.Name = career.name;
    newCareer.externalId__c = career.id;
insert careers;

List<Degree__c> degrees = new List<Degree__c>();
for (Degree degree : requestBody.degrees) {
    Degree__c newDegree = new Degree__c();
    newDegree.Name = degree.name;
    newDegree.externalId__c = degree.id;
insert degrees;

// Create a map for both degrees and careers so that getting references for CareerDegreeRelationships is easy
Map<Integer, Career> careersMap = new Map<Id, Career>()
for (Career__c car : careers) {
    careersMap.put(car.externalId, car);
Map<Integer, Degree> degreesMap = new Map<Id, Degree>()
for (Degree__c deg : degrees) {
    degreesMap.put(deg.externalId, deg);

// Insert the Relationships
List<CareerDegreeRelationship__c> newRels = new List<CareerDegreeRelationship__c>()
for (Relationship rel : requestBody.careerDegreeRelationships) {
    CareerDegreeRelationship__c newRel = new CareerDegreeRelationship__c();
    newRel.Career__c = careersMap.get(rel.career).Id;
    newRel.Degree__c = degreesMap.get(rel.degree).Id;
    if (newRels.size() == 10000) {
        insert newRels;
insert newRels;   <<<<< ERROR

So as you will see, I basically create all the careers, create all the degrees, then I create maps for career and degree based with the externalId as the key to be used when creating the CareerDegreeRelationships.

Since I know there will be more than 10,000 relationships, you will see that if my newRels List ever gets to be 10,000 records, I go ahead and insert those 10,000, clear the list and then continue creating more.

Then in the last line of code I showed, you will see I insert the remaining items in the rels list (this is to insert the final x amount if less than 10000). However, I get the error that I attempted to insert 10,001 records on this line.... To me, this says that the clear() function is not working and my list is not actually being emptied.

  • Have you tried debugging to check if the clear() is working? why dont you use System.debug(newRels.size()) after you clear the list to see if its working. You are also missing a semi-colon after newRels.clear() – Z33dawg Apr 1 at 19:13

The 10,000 DML row limit is per transaction, not per operation. You can't update more than 10,000 rows from start to finish. If you need to do so, you'll need to use Batchable or Queueable methods to process more records.

  • And in this case, Per Transaction is the entire API method? – BlondeSwan Apr 1 at 19:14
  • @BlondeSwan Yes, that's correct. – sfdcfox Apr 1 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.