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
XXXXXX 

Accessing value from subquery in AJAX

I have an S-Control that is failing. Assuming that my query is this:

 

SELECT id, lastname, contact.account.name FROM contact, contact.account

 

and that I have already gotten access to the id and lastname fields using contacts = queryResult.getArray('records'); and id = contacst[i].Id, how do I access the contact.account.name field? I assumed that contacts[i].Account.Name would do it, but of course it doesn't.

 

TIA,

 

John

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
DevAngelDevAngel

Hey XXX (triple x?),

 

I think you have the correct syntax.  The docs agree at least.  Are you getting an error or is there some other way that you are detecting that it's not working properly?

 

From the docs:

 

 

var result = sforce.connection.query("SELECT c.Id, c.firstname, " +
"c.lastname, c.leadsource, a.Id, a.name, a.industry, c.accountId " +
"FROM Contact c, c.account a ORDER BY leadsource LIMIT 10");

var it = new sforce.QueryResultIterator(result);

while(it.hasNext()) {
var record = it.next();
var accountName = record.Account ? record.Account.Name : null;

log( record.FirstName + " " + record.LastName +
" in account " + accountName);
}

 

 

 

All Answers

DevAngelDevAngel

Hey XXX (triple x?),

 

I think you have the correct syntax.  The docs agree at least.  Are you getting an error or is there some other way that you are detecting that it's not working properly?

 

From the docs:

 

 

var result = sforce.connection.query("SELECT c.Id, c.firstname, " +
"c.lastname, c.leadsource, a.Id, a.name, a.industry, c.accountId " +
"FROM Contact c, c.account a ORDER BY leadsource LIMIT 10");

var it = new sforce.QueryResultIterator(result);

while(it.hasNext()) {
var record = it.next();
var accountName = record.Account ? record.Account.Name : null;

log( record.FirstName + " " + record.LastName +
" in account " + accountName);
}

 

 

 

This was selected as the best answer
XXXXXX

Yeah, my choice of nickname is unfortunate. There's a bug in the registration page, and once I finally around that bug and got registered, I found out there's another bug in the change-your-nickname page. I reported both bugs last year but nothing happened, so now I go by Triple-X ... at least it's easy to pick out my posts on the front page :)

 

That worked great, thanks!

 

John