Friday, October 23, 2009

Creating a new Salesforce Account using the S4S Data Connector

The following is an example of creating a new Salesforce Account from C# using the Sitecore for Salesforce Data Connector (DC).
NB: These code snippets are applicable to the current 1.0.5 release.

For Salesforce Organizations with customizations, like fields, you would use the T4 generated classes that match your org rather than those in FuseIT.Sitecore.SalesforceConnector.Entities.

using Microsoft.VisualStudio.TestTools.UnitTesting; //Only used for the Assert in the example
using FuseIT.Sitecore.SalesforceConnector.Entities;
using FuseIT.Sitecore.SalesforceConnector.Services;

//...

[TestMethod]
public void CreateAccount()
{
    Account account = new Account();
    account.Name = "FuseIT";

    SalesforceSession salesforceSession = new SalesforceSession(
        new LoginDetails("username@example.com", "salesforcePassword"));
    AccountService accountService = new AccountService(salesforceSession);

    var saveResult = accountService.Insert(account);
    string newAccountId = null;
    if (saveResult.success)
    {
         newAccountId = account.Id;
    }
    Assert.IsTrue(accountService.ValidEntityId(newAccountId));
}