You need to sign in to do that
Don't have an account?
Javascript & Relationships (Object__r.name)
Hey, I'm trying to traverse relationships in Javascript, and the dot notation is not working in Javascript. Can you please help me access the name? I have written my own Javascript Remoting class that handles the query and then returns the results.
//Building the SOQL query to pass to the Javascript remoting class
var queryAcct = "SELECT id, Name, Owner.Name, Division__r.Name FROM Account "; queryAcct += "WHERE Owner.name like '%{username}%'"; queryAcct += " ORDER BY Division__c "; queryAcct += "Limit 5000";
//Fucntion that is called to query the account records, this then passes the results off to the next function that builds the table
function getaccts(){ var un = document.getElementById("username").value; alert(queryAcct.replace("{username}",un)); RemotingToolkit.query(queryAcct.replace("{username}",un),function(result,event) { if (event.status) { handleAccts(result,$j('#acctresultsDiv')); } else { alert('Remoting call failed'); } }); }
//Handles the Array that is returned from the query, then using Jquery builds the table and injects the HTML in to the div.
function handleAccts(records,tableObject) { var tableString ='<table border="1">'; tableString+='<th>Owner</th><th>Company</th><th>Division</th>'; $j.each(records, function(index,record) { tableString += '<tr><td> ' + record.Owner.Name + '</td><td>' + record.Name+''+ '</td><td>' + record.Division__r.Name+''; }); tableString += '</td></tr></table>'; tableObject.html(tableString); }
I can pass in record.Division__c and it runs, when I try to pass in record.Division__r.Name it erros out. What can I do to access the name of the related object? What is really wierd is that "record.Owner.Name" works and returns the name of the owner, not the ID, but Division__r.Name throws an error.
Thanks!
-Chris
record.Division__r is an array ?
"records" is the array of SObjects that gets passed to handleAccts, then I go through each record with $j.each(records, function(index,record) { to get each record.
This is the Javascript Remoting Class
if Divisions__r is an array,
would it be
Divisions__r[0].Name
Maybe I said it wrong,
I'm returning an array of Accounts, with the field Division__r.Name. I then pass the Account to the HandleAccts so Division__r is not an array, its part of the Accounts Array. I tried what you suggested, no luck.
I just tested with all variations and I was able to find related values succesfully , I think there is an error in you javascript method where you create table , try to alert your records in this method before you generate table object.
Let me know your findings.
what does Division__r look like if you dump it out ? or break on it and look at the members.
Shashikant - Here is the full page, maybe you can spot where the error is? I'd really appreciate it!
Thanks,
Chris
Where is it throwing an error? In the Javascript or in the Apex code? If it is in the javascript, maybe try aliasing your fields in the soql query:
@RonHess
if i alert out:
Alert(record.Division__r)
I get
[object Object]
Ok, so i built your code and got it to work, it turns out that the Division__r must not be null or you will get an error in javascript. If this lookup value is empty, it does not do you the favor of returning an empty object, it's just not there in the record at all.
so,
1 ) test if you get a null ( zero records) back, as this caused an error for me
2) your code must check for both of these
Division__r != null
Division__r.Name != null
before you use that variable
This is what i had to do to make it work
I didn't check for records != null before calling $j.each but you should