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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x