[SalesForce] the maximum number of records which can be insterted in Apex REST calling a POST method

I have an Apex REST class with a POST method:

@HttpPost
global static List<Id> doPost(List<TaskWrapper> tasks) {
    List<Task> tasksToInsert = new List<Task>();

    for (TaskWrapper task : tasks) {
        tasksToInsert.add(task.getTask());
    }

    insert tasksToInsert;

    Map<Id, Task> taskMap = new Map<Id, Task>(tasksToInsert);
    return new List<Id>(taskMap.keySet());
}

Request's body looks like this:

{
    "tasks": [
        {
            "subject": "Edna's task",
            "priority": "High",
            "status": "Not Started",
            "externalId": "efrank"
        }, {
            "subject": "Andy's task",
            "priority": "Low",
            "status": "In Progress",
            "externalId": "ayoung"
        }
    ]
}

Question: Is there any documented limit of maximum number of records which can be passed in JSON file through an Apex REST API method?

The only thing I found is Static Apex Limits section in Execution Governors and Limits:

Maximum size of callout request or response (HTTP request or Web services call): 6 MB for synchronous Apex or 12 MB for asynchronous Apex

But I'm not sure if this is the only limitation. If amount of fields passed in the request is small enough, I can send more than 60 000 records – what, in my opinion, is quite a lot.

Best Answer

Even if you consider best case scenario with following points: 1) you have no trigger in place, no workflows, no additional processing 2) you are just dumping data in target object - no query

Then also you will not be able to insert more than 10k records. Not just in APi but in UI or any other synchronous process.

whatever way you try in synchronous execution we can not insert more than 10k rows.

Here is code to prove my point.

Map<Integer,list<Task>> mapOfList = new Map<Integer,list<Task>>();
List<Task> tasklist = new List<Task>();
For(integer i =0; i<100;i++){
    For(integer j =0; j<9999; j++){ // 10k tasks
        Task t = New Task(Subject = 'Test task', priority = 'Low', status = 'In Progress' );                
        tasklist.add(t);
    }    
    mapOfList.put(i,tasklist);
}

For(Integer i : mapOfList.keyset() ){
    insert mapOfList.get(i);
    System.debug('insert ' +  i);
}

System.debug('Final Heap Size: ' +  Limits.getHeapSize());
System.debug('Final Cpu Time(): ' +  Limits.getCpuTime());

Note following things:
1) We can hold 10k * 100 = 1000K rows in HEAP using map of list and apex wont complain
2) There is not any CUP intensive operation so CPU time limit wont come in picture (DB time is calculated separably)
3) There are no SQL queries
4) Total # DML statements(insert in out case) are less than 150

Ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

Related Topic