I just tried to insert a bunch of external object records (external data source being a postgre on Heroku). Noticed it took significant amount of time. After looking at the debug logs I realised that the new methods Database.InsertAsync()
and Database.InsertImmediate()
are not bulkified. I ran the following code in the developer console:
List <ExternalObject__x> records = new List <ExternalObject__x> ();
for (Integer i = 0; i < 48; i++)
{
records.add(new ExternalObject__x(Name__c = 'TestBB ' + i));
}
Database.insertImmediate(records);
The debug log output is:
15:36:21.3 (6698016)|DML_BEGIN|[8]|Op:Insert|Type:ExternalObject__x|Rows:48
15:36:21.27 (27334305)|CALLOUT_REQUEST|System.HttpRequest[Endpoint=https://odata-us.heroku.com/odata/v4/randomHash/heroku$externalObject, Method=POST]
15:36:21.989 (989710929)|CALLOUT_RESPONSE|System.HttpResponse[Status=Created, StatusCode=201]
15:36:21.990 (990543138)|CALLOUT_REQUEST|System.HttpRequest[Endpoint=https://odata-us.heroku.com/odata/v4/randomHash/heroku$externalObject, Method=POST]
15:36:22.761 (1761926976)|CALLOUT_RESPONSE|System.HttpResponse[Status=Created, StatusCode=201]
15:36:22.762 (1762741796)|CALLOUT_REQUEST|System.HttpRequest[Endpoint=https://odata-us.heroku.com/odata/v4/randomHash/heroku$externalObject, Method=POST]
15:36:23.184 (2184963373)|CALLOUT_RESPONSE|System.HttpResponse[Status=Created, StatusCode=201]
15:36:23.185 (2185860982)|CALLOUT_REQUEST|System.HttpRequest[Endpoint=https://odata-us.heroku.com/odata/v4/randomHash/heroku$externalObject, Method=POST]
15:36:23.633 (2633244019)|CALLOUT_RESPONSE|System.HttpResponse[Status=Created, StatusCode=201]
15:36:23.634 (2634095011)|CALLOUT_REQUEST|System.HttpRequest[Endpoint=https://odata-us.heroku.com/odata/v4/randomHash/heroku$externalObject, Method=POST]
15:36:24.57 (3057260889)|CALLOUT_RESPONSE|System.HttpResponse[Status=Created, StatusCode=201]
// more of these instances... and then finally
15:36:42.903 (21903634108)|DML_END|[8]
Which shows that SFDC performed a callout to the oData provider for each of the records in my collection.
Inserting 48 records took ~21 seconds.
Is there anything config-wise or even salesforce-support-wise that can be done to bulkify this thing or is this a normal bad behaviour?
In case it's the only way to go, I'd love to know the reason why this can't be done in one go.