+ Start a Discussion
boihueboihue 

Retrieve data from the joined tables using VB .NET

Hi,
Could any expert show me how to use VB .NET for retrieving the data from both joined Account and Contact tables? I can retrieve the data from the Account table but I don't know how to retrieve the Contact data. Here're my codes:
 
Dim qr As QueryResult = Nothing
binding.QueryOptionsValue = New apex.QueryOptions
binding.QueryOptionsValue.batchSize = 250
binding.QueryOptionsValue.batchSizeSpecified = True
 
Try
    qr = binding.query("Select a.Id, a.Name, (Select c.Id, c.firstname, c.lastname From a.Contacts c) From Account")
    Dim bContinue As Boolean = True
    Dim loopCounter As Integer = 0
    While bContinue
        loopCounter += 1
        For i As Integer = 0 To qr.Records.GetUpperBound(0)
            Dim acct As Account = CType(qr.records(i), Account)
            Dim sId As String = acct.Id
            Dim sName As String = acct.Name
 
            Console.WriteLine("Contact " & (i + 1) & ": " & sId & " " & sName)
        Next
 
        If qr.done Then
            bContinue = False
        Else
            qr.binding.queryMore(qr.queryLocator)
        End If
    End While
 
If there's any way to retrieve the data from both joined tables, it would be appreciated.
 
Best regards!
SuperfellSuperfell
There should be a contacts property on the Account object that's of type QueryResult, so access it just like the outer query results.
boihueboihue

Thanks Simon for your quick response!

I'm a newbie of Salesforce, could you give me more information how to do the test if the Contact's QueryResult is empty (Some Account may have no Contact). I have tried different ways but always got the following error message when no Contact was found for the Account: "Object reference not set to an instance of an object".

Best regards!



Message Edited by boihue on 11-04-2008 11:34 AM
SuperfellSuperfell
(conversion from c# to vb left as an exercise for the reader)

if (acct.contacts != null && acct.contacts.records != null) {
foreach(Contact c in acc.contacts.records)
Console.WriteLine(c.Name);
}
boihueboihue
Thanks Simon, I found the way to test if the Account has no Contact.
 
Best regards!