Salesforce Destination

05.26.10

Problem: You need to submit salesforce sObjects including custom fields to many different organization’s salseforce instances.

Solution: Use the Partner WSDL, it's loosely typed and can be used with all organizations and  whatever customizations they have.

 

Code:

//Base class 
public class SalesforceBase
{
private readonly List<SalesforceCustomField> _customFields = new List<SalesforceCustomField>();
private readonly XmlDocument _document = new XmlDocument();
private readonly List<XmlElement> _elements = new List<XmlElement>();
private readonly string _type;

protected SalesforceBase(string type)
{
_type = type;
}

public string Type
{
get
{
return _type;
}
}

public IEnumerable<SalesforceCustomField> CustomFields
{
get
{
return _customFields;
}
}

public XmlElement[] Any
{
get
{
return _elements.ToArray();
}
}

public void AddCustomField(SalesforceCustomField customField)
{
UpdateElements(customField.FieldName, customField.Value);
_customFields.Add(customField);
}

protected void UpdateElements(string name, string value)
{
var element = _elements.Find(ele => ele.Name == name);
if (element != null)
{
element.InnerText = value;
}
else
{
element = _document.CreateElement(name);
element.InnerText = value;
_elements.Add(element);
}
}
}
//Implement Base
public class SalesforceContact : SalesforceBase
{
public SalesforceContact() : base("Contact") { }

private string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
if (value != null)
{
UpdateElements("FirstName", value);
_firstName = value;
}
}
}
}
//Send It
SForceService.create( new sObject(){Any = contact.Any, type = contact.Type});