dotConnect for Salesforce Documentation
In This Topic
    Bulk Data Loading
    In This Topic

    The SalesforceLoader component is used for bulk uploading a large number of records to Salesforce.com or Database.com.

    To start uploading data, create a SalesforceLoader component. Then you may tweak its BatchSize, UpdateStatusInterval, and ParallelBatchProcessing properties. BatchSize property determines a maximal number of records in a batch, and ParallelBatchProcessing specifies if the batches can be processed simultaneously on the server. After this you need to specify columns to load data. You may do it either manually, using the Columns property, or you may use the CreateColumns method to retrieve table columns information from the table specified in the TableName property. Then call the Open method and use the SetValue method to set values for the row fields. To switch to the next row, call the NextRow method.

    After you have finished loading data, call the Close method. The server actually starts processing the data and insert them to the table only after this method is called. After the data processing starts, the Close method queries the state of all batches each UpdateStatusInterval interval.

    If one or more batches fail, SalesforceLoaderException is thrown. It contains a collection of errors, returned by Salesforce.com or Database.com.

    The following example demonstrates using the SalesforceLoader class.

    public void LoadAccounts() {
    
      SalesforceConnection connection = new SalesforceConnection(
        "Server=login.salesforce.com;User [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;"
    );
      connection.Open();
      SalesforceLoader loader = new SalesforceLoader("Account", connection);
      loader.Columns.Add("Name", SalesforceType.String);
    
    try {
        loader.Open();
        for (int i = 1; i <= 50000; i++) {
          loader.SetValue(0, "Account " + i);
          loader.NextRow();
        }
        loader.Close();
      }
      catch (SalesforceLoaderException ex) {
    
    foreach (SalesforceError err in ex.Errors)
          // process error...
          ;
    
        int recordsSucceded = ex.RecordsSucceded;
        int recordsFailed = ex.RecordsFailed;
      }
    }
    
    
    Public Sub LoadAccounts()
    
    	Dim connection As New SalesforceConnection( _
        "Server=login.salesforce.com;User [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;" _
      )
    	connection.Open()
    	Dim loader As New SalesforceLoader("Account", connection)
    	loader.Columns.Add("Name", SalesforceType.[String])
    
    	Try
    		loader.Open()
    		For i As Integer = 1 To 50000
    			loader.SetValue(0, "Account " & i)
    			loader.NextRow()
    		Next
    		loader.Close()
    	Catch ex As SalesforceLoaderException
    
    		For Each err As SalesforceError In ex.Errors
    			' process error...
    		Next
    
    		Dim recordsSucceded As Integer = ex.RecordsSucceded
    		Dim recordsFailed As Integer = ex.RecordsFailed
    	End Try
    End Sub
    
    

    After calling the Close method you can retrieve IDs of the inserted records as an array of strings with the GetIDs method.

    The SalesforceLoader component supports not just insert operation, but all DML operations, supported by Salesforce, including UPSERT. The operation that SalesforceLoader execute is determined by its Mode property.

    To use the upsert operation, you need to set this property to SalesforceLoaderMode.Upsert and specify the Salesforce field to use as External Id. For the latter, set the ExternalId property of the corresponding Loader column to true.

    static void Main(string[] args)
    {
    	SalesforceConnection conn = new SalesforceConnection(
    		"Server=login.salesforce.com;User [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;");
    	conn.Open();
    
    	SalesforceLoader loader = new SalesforceLoader();
    	loader.TableName = "Account";
    
    	loader.Connection = conn;
    	loader.CreateColumns();
    	loader.Columns["External_ID"].ExternalId = true;
    	loader.Mode = SalesforceLoaderMode.Upsert;
    	loader.Open();
    	loader.SetValue("Name", "Test Name 1");
    	loader.SetValue("Email", "[email protected]");
    	loader.SetValue("External_ID", 1);
    	loader.NextRow();
    	loader.SetValue("Name", "Test Name 2");
    	loader.SetValue("Email", "[email protected]");
    	loader.SetValue("External_ID", 10);
    	loader.NextRow();
    
    	loader.Close();
    	conn.Close();
    }
    
    
    Private Shared Sub Main(ByVal args As String())
        Dim conn As SalesforceConnection = New SalesforceConnection( _
    		"Server=login.salesforce.com;User [email protected];Password=mypassword;Security Token=qweASDzcx1234567890rtyui;")
        conn.Open()
        Dim loader As SalesforceLoader = New SalesforceLoader()
        loader.TableName = "Account"
        loader.Connection = conn
        loader.CreateColumns()
        loader.Columns("External_ID").ExternalId = True
        loader.Mode = SalesforceLoaderMode.Upsert
        loader.Open()
        loader.SetValue("Name", "Test Name 1")
        loader.SetValue("Email", "[email protected]")
        loader.SetValue("External_ID", 1)
        loader.NextRow()
        loader.SetValue("Name", "Test Name 2")
        loader.SetValue("Email", "[email protected]")
        loader.SetValue("External_ID", 10)
        loader.NextRow()
        loader.Close()
        conn.Close()
    End Sub
    
    

    See Also

    SalesforceLoader | SalesforceLoaderException