Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I try to join 2 tables in SOQL that I am using in C#. not sure how get the value from second table that is contact table. This is my query:

 SOQL = "select Name,FirstName,LastName,BillingCity,Id, (Select ID, LastName,Title from Contacts) from Account";

        queryResult = SfdcBinding.query(SOQL);
        if (queryResult.size > 0)
        {

            for (int i = 0; i < queryResult.size; i++)
            {

                Account acc = (Account)queryResult.records[i];
                string name = acc.Name;
                string FirstName = acc.FirstName;
                string LastName = acc.LastName;
                string city = acc.BillingCity;
                string Id = acc.Id;

            }

How access to ID, LastName,Title from contact table in C# code?

share|improve this question
    
why would you use SOQL in C# :?. –  rao Feb 21 at 19:27
    
Because I am using SalesForce API and it should be in SOQL. –  nikoo m Feb 21 at 19:30
add comment

3 Answers

EDIT: Note: this is exactly what sfdcfox said

Since you are querying Contacts with Accounts, the Account will have the list of Contacts returned as a Contact[] called "Contacts".

Example:

for (int i = 0; i < queryResult.size; i++){
    Account acc = (Account)queryResult.records[i];
    string name = acc.Name;
    string FirstName = acc.FirstName;
    string LastName = acc.LastName;
    string city = acc.BillingCity;
    string Id = acc.Id;

    Contact[] contacts = (Contact[])acc.Contacts;

    for(int j=0; i<contacts.length; j++){
        Contact con = contacts[j];
        string contactId = con.id;
        string contactLastName = con.LastName;
        string contactTitle = con.Title;
    }

}
share|improve this answer
    
Thanks @amatorVitae for your reply. I am getting error on line: Contact[] contacts = (Contact[])acc.Contacts; this is error I am getting: "Cannot convert type QuertResult to Contact[] " –  nikoo m Feb 21 at 22:27
add comment

It is accessed much the same as you'd access it in Apex Code: the acc variable will have a member called Contacts which (should) be a Contact[] (or at worst, a SObject[], which you can cast each member into a Contact). I'm not well-versed in .Net code, but based on what I do know about the language and WSDL in general, this should be about right.

share|improve this answer
    
I am getting error on line: Contact[] contacts = (Contact[])acc.Contacts; this is error I am getting: "Cannot convert type QuertResult to Contact[] " Do you know how I can get the fields from second table? –  nikoo m Feb 22 at 1:20
add comment

acc.Contacts is a nested QueryResult, so you like you're accessing the records property of your top level query result, you do the same on the nested one, e.g.

Contact[] contacts = (Contact[])acc.Contacts.records

Also, queryResult.size is the size of the entire result, which may be significantly larger than the current chunk of results you have, so your outer loop based on queryResult.size should be changed to be based on the queryResult.records.length instead.

share|improve this answer
    
I get this error now on this line you added: Unable to cast object of type 'sObject[]' to type 'Contact[]'. –  nikoo m Feb 24 at 21:53
    
probably you have a class called Contact that is conflicting with the generated one. –  superfell Feb 24 at 22:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.