+ Start a Discussion
CB312CB312 

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

RonHess.ax193RonHess.ax193

record.Division__r  is an array ?

CB312CB312

"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

global with sharing class RemotingToolkit {

// Perform a with-sharing, JS Remoting query
@RemoteAction
global static SObject[] query(String soql) {

List <sobject>SObjects = new List<sobject>();
try {
SObjects = Database.query(soql);
} catch (Exception e) {
return null;
}
return SObjects;
}

}

 

 

RonHess.ax193RonHess.ax193

if Divisions__r is an array,

 

would it be 

Divisions__r[0].Name 

CB312CB312

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.

Shashikant SharmaShashikant Sharma

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.

RonHess.ax193RonHess.ax193

what does Division__r look like if you dump it out ? or break on it and look at the members.

CB312CB312

Shashikant - Here is the full page, maybe you can spot where the error is? I'd really appreciate it!

 

Thanks,

Chris

 

<apex:page controller="RemotingToolkit">
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"/>
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.js"/>



<script>
$j = jQuery.noConflict();


var queryString = "SELECT id, Name, Owner.Name, Company, Division__c FROM Lead ";
queryString += "WHERE Owner.name like '%{username}%'";
queryString += " AND isconverted = false ";
queryString += " ORDER BY Division__c ";
queryString += "Limit 5000";

var queryAcct = "SELECT id, Name, Owner.Name, Division__r.Name FROM Account ";
queryAcct += "WHERE Owner.name like '%{username}%'";
queryAcct += " ORDER BY Division__r.Name ";
queryAcct += "Limit 5000";

               
function getleads(){
var un = document.getElementById("username").value;
alert(queryString.replace("{username}",un));
RemotingToolkit.query(queryString.replace("{username}",un),function(result,event) {
        if (event.status) {
        handleLeads(result,$j('#leadresultsDiv'));
        $j("#leadresultsDiv").show('slow');    
            } else {
                    alert('Remoting call failed');
                    }
        });
    }
function handleLeads(records,tableObject) {

            var tableString ='<table border="1">';
                tableString+='<th>Owner</th><th>Contact Name</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.Company+''+ '</td><td>' + record.Division__c+'';
                });
                tableString += '</td></tr></table>';
                tableObject.html(tableString);
                }    
                 

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'));
        $j("#acctresultsDiv").show('slow');
        $j("#bhideleads").show();
        $j("#bshowleads").hide();
        $j("#bhideaccts").show();
        $j("#bshowaccts").hide();
            
            } else {
                    alert('Remoting call failed');
                    }
        });
    }
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);
                }
  
     
function hideleads(){
           $j("#leadresultsDiv").hide('slow');
           $j("#bhideleads").hide('slow');
           $j("#leadheader").hide('slow');
           $j("#bshowleads").show('slow');
            }
function showleads(){
           $j("#leadresultsDiv").show('slow');
           $j("#bhideleads").show('slow');
           $j("#bshowleads").hide('slow');
            }
function hideacct(){
           $j("#acctresultsDiv").hide('slow');
           $j("#bhideaccts").hide('slow');
           $j("#acctheader").hide('slow');
           $j("#bshowaccts").show('slow');
            }
function showacct(){
           $j("#acctresultsDiv").show('slow');
           $j("#bhideaccts").show('slow');
           $j("#bshowaccts").hide('slow');
            }

function searcher(){
getleads();
getaccts();
$j("#bshowleads").hide('slow');
$j("#bshowaccts").hide('slow');
}                                                 
</script>
<html>
<style type="text/css">

.odd {background: #caf2fe;}

</style>

<button type="button" onclick="searcher();">Search Leads and Accounts</button>   
<input type="text" id="username" />
  <table>
      <tr>    
          <td>
              <button type="button" id ="bshowleads" onclick="showleads();">Show Leads</button>             
          </td>
          <td>
              <button type="button" id ="bshowaccts" onclick="showacct();">Show Accounts</button>            
          </td>
      </tr>
      <tr>
          <td>
              <button type="button" id ="bhideleads" onclick="hideleads();">Hide Leads</button>
          </td>
          <td>
              <button type="button" id ="bhideaccts" onclick="hideacct();">Hide Accounts</button>  
                        
          </td>
      </tr>
  </table>
  <table>
  <div id = "leadheader">
  <th>
  Leads
  </th>
  </div>
  <div id = "acctheader">
  <th>
  Accounts
  </th>
  </div>
  <tr>
   <td>
    <div id = "leadresultsDiv"/>
   </td>
   <td>
   <div id = "acctresultsDiv">
   <b>What up?</b>
   </div>
   </td> 
  </tr>
  </table>  
</html>
</apex:page>

 

dhoechstdhoechst

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:

 

var queryAcct = SELECT id, Name, Owner.Name, Division__r.Name DivName FROM Account

 

CB312CB312

@RonHess

 

if i alert out:

Alert(record.Division__r)

 

I get

 

[object Object]

RonHess.ax193RonHess.ax193

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 

RonHess.ax193RonHess.ax193

This is what i had to do to make it work

 

tableString += '<tr><td> ' + 
      record.Owner.Name + '</td><td>' + 
      record.Name+''+ '</td><td>' + 
      (record.Provider__r != null && 
       	record.Provider__r.Name != null ? 
       	record.Provider__r.Name:'none') +  '';;

 

I didn't check for records != null before calling $j.each but you should