[SalesForce] salesforce .net toolkit relationship query serialization

I created strongly type classes to use when using the toolkit to query salesforce

public class SFAccount
{        
    public string Id { get; set; }
    public string Name { get; set; }
    public string Account_Code__c { get; set; }
}

QueryResult<SFAccount> results = await _client.QueryAsync<SFAccount>(@"SELECT Id,
                                                                     Name,
                                                                     Account_Code__c
                                                                     FROM Account
                                                                     ORDER BY Id ASC");

This works fine as expected.

But when the query includes relationship columns using __r, I can't seem to get the toolkit to serialize my data to class object

public class SFLocation
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Account__c { get; set; }
    public string Account__r_Name { get; set; }
    public string Account__r_Account_Code__c { get; set; }           
}

QueryResult<SFLocation> results = await _client.QueryAsync<SFLocation>(@"SELECT Id,
                                                                Name,
                                                                Account__c,
                                                                Account__r.Name,
                                                                Account__r.Account_Code__c,
                                                                FROM Location__c
                                                                ORDER BY Id ASC");

The last two properties (Account__r_Name and Account__r_Account_Code__c) are not being populated and my query is valid and returns data

How would I construct my class so that the toolkit would be able to serialize the result data to the class?

Best Answer

I assume you are using the Force.com Toolkit for .NET for this?

In this case your SFLocation class should have a property of type Account that it called Account__r. And the Account class would have the Name and Account_Code__c properties.

You could check this in the debugger with a dynamic type. Set a breakpoint in the loop and inspect the sfl.

var dynamicLocations = await _client.QueryAsync<dynamic>(query );
foreach (dynamic sfl in dynamicLocations .Records)
{
    Console.WriteLine("location - " + sfl.Account__r);
}

Another alternative would be to get the raw JSON response from the query. Something like Workbench or Fiddler could be useful here. Then create the C# classes for Contact and Account from that JSON using http://json2csharp.com/

Related Topic