+ Start a Discussion
MattLMattL 

Nested SOQL queries in Flex: Possible?

I'm writing a Flex app to graph out resource usage by hours. It utilizes two custom Objects, Resource and Assignment.

I first query to get the resource ID, and then inside of that for each resource, I query all Assignments that the resource is associated with, with the intention of tallying them up.

Code:
<mx:Script>
 <![CDATA[
  import mx.collections.ArrayCollection;
  import com.salesforce.AsyncResponder;
  import com.salesforce.results.QueryResult;
  import com.salesforce.AsyncResponder;
  import com.salesforce.objects.LoginRequest;
  private function login():void{
   apex.login(  new LoginRequest({
         server_url : this.parameters.server_url,
         session_id : this.parameters.session_id,
         callback : new AsyncResponder(render)
         })
         );
  }
  public var resHours:ArrayCollection = new ArrayCollection();
  private function render(result:Object):void {
           apex.query("Select Id from SFDC_Resource__c",
     new AsyncResponder(
     function (qr:QueryResult):void
     {
       for(var x:int=0;x<qr.records.length;x++)
       {
        apex.query("Select Hours, Resource__c from SFDC_Assignment__c Where Resource__r.Id='"qr.record[x].Id"'",
        new AsyncResponder(
        function (qr2:QueryResult):void
        {
         var tempHours=qr2.records[0].Hours;
         for(var y:int=1;y<qr2.records.length;y++)
         {
       tempHours+=qr2.records[y].Hours;
         }
         resHours.addItem({Hours:tempHours, Resource:qr.records[y].Id});
        },
        function (fault:Object):void {}
        ));
       }
     },
     function (fault:Object):void {}
        ));
  }
 ]]>
</mx:Script>

 My problem is that, it's throwing errors that bounce all over the place, and I can't quite get a fix on the originating error.

1078: Label must be a simple identifier.    ResourceUsage.mxml    line 31   
1084: Syntax error: expecting colon before for.    ResourceUsage.mxml    line 34   
1084: Syntax error: expecting identifier before comma.    ResourceUsage.mxml    line 39   
1084: Syntax error: expecting identifier before var.    ResourceUsage.mxml    line 33  
1084: Syntax error: expecting rightbrace before leftbrace.    ResourceUsage.mxml    line 40
1084: Syntax error: expecting rightbrace before semicolon.    ResourceUsage.mxml    line 34
1084: Syntax error: expecting rightparen before qr.    ResourceUsage.mxml    line 29  

Does anyone have any suggestions/comments? I'm presently at a loss.
MattLMattL
So... I figured out the problem. I had forgotten the + signs in my SOQL query where I'm injecting that variable.

Fixed all the errors. Gotta love those. X.x
csharmacsharma
Hi,

I am having the same issue, kindly let me know how did u handle it.
as u have mentioned u have added the plus signs, i guess it wud be to the variable inside the where clause but HOW/WHERE? m not sure of the syntax.
csharmacsharma
//This is my flex script.......dont know how to use the plus signs u have mentioned and are your sure nested queries are possible!!
//anyhow do lemme know if anyone could find a simple workaround to what I am tryin to do here. Basically m trying to populate datagrid //with the account names and respective opportunities (with detatails) in those accounts. Say 1st column would be Opportunity Name, //2nd would be Account to which the opp belong and then other columns with the opp details. Urgent help is required. THANKS!


            [Bindable]
            private var accountList:ArrayCollection = new ArrayCollection();
           
            [Bindable]
            private var ol:ArrayCollection = new ArrayCollection();

            [Bindable]
            private var bl:ArrayCollection = new ArrayCollection();
           
            [Bindable]
            private var ar:String;
       
            private function loadData(o:Object=null):void {
                apex.query("SELECT AccountId, Name, StageName  FROM Opportunity", new AsyncResponder(
                    function(qr:QueryResult):void {
                        if (qr.size > 0) {
                            accountList = qr.records;
                           
                            for(var i:int=0;i<qr.records.length;i++){
                                apex.query("SELECT Name FROM Account where Account.Id ='"accountList[i].AccountId"'", new AsyncResponder(
                                    function(qr1:QueryResult):void {
                                        if (qr1.size > 0) {
                                            bl.addItem(qr1.records);
                                            // would be using bl as a dataprovider to datagrid later on 
                                            }
                                    },
                                handleFault)
                                );
                            }
                           
                        }
                    },
                    handleFault)
                );
            }