function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ekmekm 

Flex Apex Query and Subquery

Hi,
I'm attempting to run a query and a subsequent subquery based off the original query and then display the results in a datagrid. I'm having difficulty with the subquery and would appreciate any help or guidance as to what I need to do. Below is the portion of code that I'm working with. As I said, the Role field from the 1st query populates fine, but how do I get the results from the 2nd query.

Thanks,

[Bindable]
public var ar2:ArrayCollection = new ArrayCollection();
   
    [Bindable]
    public var ar:ArrayCollection = new ArrayCollection();
   
    private function queryAccountManagerEdit1():void
      {
        currentState="searchAccountManagerEdit";
        var acc:SObject = new SObject('AccountContactRole');
        apex.query("Select ContactId, Role From AccountContactRole where Role = 'Decision-maker'",  
            new AsyncResponder(function (qr:QueryResult):void
              {
                for (var j:int=0;j<qr.records.length;j++)
                    {
                    ar.addItem(  { ContactId:qr.records[j].ContactId, Role:qr.records[j].Role } );
                    //ar.addItem(  { Role:qr.records[j].Role } );
                    apex.query("Select Id, Name From Contact where Id = ContactId:qr.records[j].ContactId",
                    new AsyncResponder(function (qr2:QueryResult):void
                        {
                            for (var k:int=0;k<qr2.records.length;k++)
                            {
                                ar2.addItem( {Name:qr2.records[k].Name});
                            }
                            //dgSearchAccountManagerEdit.columns = [ new DataGridColumn('Name'), new DataGridColumn('Role') ];
                            //dgSearchAccountManagerEdit.dataProvider = ar2;
                        }
                    )                    
                    )
                }
                // create the columns and specify the order
                dgSearchAccountManagerEdit.columns = [ new DataGridColumn('Name'), new DataGridColumn('Role') ];
                dgSearchAccountManagerEdit.dataProvider = ar;    //assign the array as the data provider
                }
               ) // closes new AsyncResponder(function (qr:QueryResult):void
        ); //closes 1st apex query
    }  
werewolfwerewolf
How about by not doing the second query at all?  You can nest that subquery inside a single query using a Relationship as follows:

Select a.Id,a.Role,a.Contact.Id,a.Contact.Name from AccountContactRole a
ekmekm
Awesome. I had initially tried nesting my subquery but my syntax was wrong. Thanks a bunch for your help!
For anyone interested, here's the related code:

[Bindable]
    public var ar:ArrayCollection = new ArrayCollection();
   
    private function queryAccountManagerEdit1():void
      {
        currentState="searchAccountManagerEdit";
        var acc:SObject = new SObject('AccountContactRole');
        //apex.query("Select ContactId, Role From AccountContactRole where Role = like '%" + txtAccountManagerEdit.text + "%'",
        apex.query("Select a.Id, a.Role, a.Contact.Id, a.Contact.Name from AccountContactRole a where a.Role = 'Decision-maker'",  
            new AsyncResponder(function (qr:QueryResult):void
              {
                for (var j:int=0;j<qr.records.length;j++)
                    {
                    ar.addItem(  { Role:qr.records[j].Role, Name:qr.records[j].Contact.Name } );
                    //ar.addItem(  { Role:qr.records[j].Role } );
                }
                // create the columns and specify the order
                dgSearchAccountManagerEdit.columns = [ new DataGridColumn('Name'), new DataGridColumn('Role') ];
                dgSearchAccountManagerEdit.dataProvider = ar;    //assign the array as the data provider
                }
               ) // closes new AsyncResponder(function (qr:QueryResult):void
        ); //closes 1st apex query
    }  
ekmekm
I have a follow up to the above. The following (as suggested above) works great.

Select a.Id, a.Role, a.Contact.Id, a.Contact.Name from AccountContactRole a where a.Role = 'Decision-maker'

But the following does not work (even in the Apex explorer).
Select a.Account.Id, a.Account.AccountNumber, a.Account.Name, a.AccountId, a.ActivityDate, a.ActivityDateTime, a.Type from Event a
 
Anyone have any thoughts? And back to my original question, would I have to subquery the accounts table to get the account info for each event?