Page 1 of 1

Upsert for dotconnect salesforce

Posted: Wed 10 Oct 2018 21:40
by pl6306
How do I do an Upsert using dotconnect salesforce? Thanks!

Re: Upsert for dotconnect salesforce

Posted: Thu 11 Oct 2018 14:52
by Pinturiccio
You can use the SalesforceLoader class for performing upsert. For more information, please refer to https://www.devart.com/dotconnect/sales ... oader.html

To use the upsert operation, you need to set SalesforceLoader Mode to SalesforceLoaderMode.Upsert and specify the Salesforce field to use as External Id.

Here is an example of how it works. Suppose, we have a custom object custom_table1__c. We will provide values for new_one__c and custom_field1__c fields and use External5__c as External Id. Suppose the custom_table1__c object has 5 records with the following External5__c values: 1, 2, 3, 4, and 5. The following code will update a record with External5__c equal to 1 and insert a record with External5__c equal 10:

Code: Select all

static void Main(string[] args)
{
    SalesforceConnection conn = new SalesforceConnection("your connection string");
    conn.Open();

    SalesforceLoader loader = new SalesforceLoader();
    loader.TableName = "custom_table1";

    loader.Connection = conn;
    loader.CreateColumns();
    loader.Columns["External5"].ExternalId = true;
    loader.Mode = SalesforceLoaderMode.Upsert;
    loader.Open();
    loader.SetValue("new_one", "test string");
    loader.SetValue("custom_field1", "Test string");
    loader.SetValue("External5", 1);
    loader.NextRow();
    loader.SetValue("new_one", "test string");
    loader.SetValue("custom_field1", "Test string");
    loader.SetValue("External5", 10);
    loader.NextRow();

    loader.Close();
    conn.Close();
}
As the result, a record with External5__c 10 will be added to custom_table1__c. A record with External5__c equal to 1 will be updated with the values "test string" for the new_one__c field, and "Test string" for custom_field1__c.