function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alivepjcalivepjc 

Creating objects in batches C#

Hi,
 
I've been searching the forums for creating objects in batches of 200 but I can't find any examples (C#). the sample code to download doesn't have an example of batch creation. To create opportunities for example, I do this:
 
sforce.Opportunity newOp = new sforce.Opportunity();

// populate newOp

newOp.AccountId = acct_id;

newOp.StageName = "ClosedWon"

newOp.Name = "John";

sforce.sObject[] records = new sforce.sObject[] {newOp};

sforce.SaveResult[] saveResults = binding.create(records);

this is OK if I need to create a few, but if I need to create thousands of them, it is not OK. I know this is simple question, but I can't find the answer.. I'd like to follow the best practices..

thank you!

JesterJester
Well, right now you setting an array of size 1 in create, and 1 record is being created per call.

If you set an array of size 2 for the create call, then 2 records will be created per call.

If you create an array containing 200 SObjects, and pass that array of 200 to the create call, then 200 records will be created with that call.

The upper limit is 200.
DevAngelDevAngel
As you can see in your code, the call takes an array of objects.  To send more than one in a single call, create an array with more than one.
alivepjcalivepjc

Something like this?

sforce.sObject[] records = new sforce.sObject[10];

for (int i=0; i< 10; i++)

{

sforce.Opportunity newOp = new sforce.Opportunity();

// populate newOp

records[i] = newOp;

}

sforce.SaveResult[] saveResults = binding.create(records);

DevAngelDevAngel
Yup.