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
jf317820jf317820 

grabbing length of soql-r query

 Code:
  var buildings = sforce.connection.query("SELECT Id, (Select Id, Name, Building_Street__c, Building_City__c, Building_State__c FROM Buildings__r) FROM Site__c WHERE Deal__c = '{!Opportunity.Id}'");

 
how do i get the length (or size) of the nested buildings query?
 
thanks in advance
cheenathcheenath
Sub query returns a QueryResult, so you can get the length of this
query by using the code below.

result = sforce.connection.query(...);
records = result.getArray("records");

  for (var i=0; i<records.length; i++) {
    var record = records[i];
    var childQuery = record.Buildings_r;
    if (childQuery) {
      var buildings = childQuery.getArray("records");
      alert(buildings.length);
    }
  }

See if you can use QueryResultIterator. It is much easier  to use
with  nested query.  You can find an example here:
AjaxTools->samples->Query->join.

HTS




 



jf317820jf317820
thanks for the reply...i'm almost there.  essentially, i have an object "Sites" that is a child of Opportunities.  Buildings is the child of Sites.  What I'm trying to do is grab the entire list of buildings from all the sites associated with the opportunity.
 
So, i have this code now:
Code:
 else if (test.length > 1) { 
  var ait = new sforce.QueryResultIterator(buildings);

  while(ait.hasNext()) {
    var SITE = ait.next();

    var BUILDINGS = [];
    if (SITE.Buildings__r) {
      var cit = new sforce.QueryResultIterator(SITE.Buildings__r);
      while(cit.hasNext()) {
        var building = cit.next();
        BUILDINGS.push(building.Id);
      }
    }
  }
  
 }

 
ok, the opportunity i'm working wiht right now has 2 sites (test.length represents the number of sites associated with the deal).  one of the sites has 2 buildings, the other site has 1 building.  I'm simply trying to output a list of the three buildings, but I can't seem to get these two "unnassociated" sets of buildings to play nice in the array so I can grab them and output them.
 
please help, i've been grinding this out for days.
cheenathcheenath
I dont have enough info to know what is going wrong.

You can run this query in the debug shell. Just cut and paste
the query api call in the shell text box and press return. shell will display
the server response as a easy to read table. See if the server
is sending back all the details you are expecting.

Also note that the coloum heading name must be used to access
the value of that field in javascript.

HTHs,