I'm writing Java program which use enterprie WSDL to upsert approx 20,000 records in Salesfore. This program will run every night.

I'm able to write a program which upsert one record a time, using this syntax

UpsertResult[] results = connection.upsert("Name_of_externalID_Field__c",new SObject[] { strObj });

But problem is, this statement,will be executing in 'for' loop(see below code) and hence inefficient, as list(objList) present in 'for loop' could contain upto 20k records. it will be something like this :

for(myobj__c strObj :objList ) {
                UpsertResult[] results = connection.upsert("Name_of_Metadata__c",new SObject[] { strObj });}

I've gone through 100 articles/forums links, but nowhere I've seen example, which handles above scenario using enterprise WSDL.

Most places, it is written to use something like this:

SaveResult[] = connection.create(sObject[] sObjects);

But its not clear how to use above statement within for loop(In java program). specifically, how to ensure sObject[] sObjects contain 200 records and then execute upsert?.

Do I've to do all manipulation(writing logic) within 'for loop' myself to ensure, 200 records are coming in list sObject[] sObjects and then execute upsert and then make it(list) blank for next iteration ?? Or there is a standard way to do that?

  • Hi Eric. I've to run this Java program in my customer's org. But permission etc are required to be taken which will take lots of time. Currently, I'm trying in my own dev org which doesn't have that much data(20K records) to be sent to salesforce. I'm thinking for best practices now only, so that when I've to deploy my java program in customer org, it is all set and don't run into performance issues. thanks ! – sfdc_Qrius Jul 29 '18 at 14:25

The standard way to do this would be to split the list into batches.

for(int i = 0; i < objList.size(); i += 200) {
  // Create a list from i to i + 200 (exclusive), or end of list
  SObject[] currentPage = new SObject[Math.min(200, objList.size() - i)];
  // Copy up to 200 records to new array
  for(int j = i; j < Math.min(objList.size(), i + 200); j++) {
    currentPage[j-i] = objList[j];
  // Perform upsert
  UpsertResult[] results = connection.upsert(extFieldName, currentPage);

Basically, we split the list into groups of up to 200, by first creating a new list, then copying 200 elements from i to i + 199 or the end of the list, whichever occurs first, then do the upsert.

Note: My Java is a bit rusty, so you might need to experiment a bit to get this just right.

  • so I assume the soap api can only handle a max of 200 at a time? – Eric Jul 29 '18 at 21:11
  • 1
    @Eric Yes, the docs explicitly mention a 200/transaction limit. – sfdcfox Jul 29 '18 at 21:19
  • Thanks sfdcfox. This seems quite workable. You're always my last savior! I'll try this by EOD and get back! – sfdc_Qrius Jul 30 '18 at 6:01

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.