Monday, June 13, 2011

Creating a S4S DataSource query for a child-to-parent relationship

Salesforce uses the following relationship between Account and Contact as the example of a SOQL query from the child (Contact) to the parent (Account):

SELECT Contact.FirstName, Contact.Account.Name from Contact

S4S versions greater than 1.5.2011.0906 can replicate this type of query as follows:

SalesforceSession salesforceSession = new SalesforceSession(  
        new LoginDetails("", "salesforcePassword"));
ContactDataSource contactDataSource = new ContactDataSource(salesforceSession);

List<Contact> contacts = contactDataSource.QueryEntities<Contact>(
  Contact.Fields.Account(Account.AccountFields.Id, Account.AccountFields.Name)

foreach (Contact contact in contacts)
 Account account = contact.Account;
 string accountName = account.Name;