Hi,
I'm querying Salesforce.com via their AJAX toolkit and outputting query
results into a table. Currently, their toolkit does not possess the
ability to do table joins via their structured query language, which
forces me to do the join manually via arrays.
Right now, I'm having trouble getting these query results (which are in
arrays) to combine effectively (mainly towards the end where I'm
outputting into the tables).
Any help would be greatly appreciated!
Thanks
My code looks like so:
<!--THIS QUERIES A LIST OF ACCOUNTS LABELED AS 'A LIST'--!>
var queryResult = sforceClient.query("Select ID, Name,
Prospect_Status__c, SystemModstamp, Portfolio_Rank__c From Account
where Prospect_Status__c = 'A List' and OwnerID = '{!User_ID}'");
var accountIdCriteria = "Where ";
for (var i=0;i<queryResult.records.length;i++) {accountIdCriteria +=
"AccountId = '" + queryResult.records[i].get("Id") + "'";
if (i < queryResult.records.length - 1) {accountIdCriteria += " or "};
}
<!--THIS QUERIES THE CONTACT TABLE FOR CONTACTS RELATED TO THE RETURNED
ACCOUNTS--!>
var contactQuery = sforceClient.query("Select Id, FirstName, LastName,
AccountId From Contact " + accountIdCriteria);
var contactIdCriteria = "Where ";
var contactsById = [];
for (var i=0; i<contactQuery.records.length;i++) {
contactIdCriteria += "(ContactId = '" +
contactQuery.records[i].get("Id") + "'" + " and IsPrimary = true)";
if (i < contactQuery.records.length - 1) {
contactIdCriteria += " or "};
contactsById[contactQuery.records[i].get("Id")] =
contactQuery.records[i];
}
<!-- THIS QUERIES THE PRIMARY CONTACTS FROM THE ACCOUNTCONTACTROLE
TABLE--!>
var roleQuery = sforceClient.query("Select ContactId from
AccountContactRole " + contactIdCriteria);
for (var i=0;i<roleQuery.records.length;i++) {
var roleRecord = roleQuery.records[i];
var primaryContactByAccountId = [];
var contactRecord = contactsById[roleRecord.get("ContactId")];
if (contactRecord) {
primaryContactByAccountId[contactRecord.get("AccountId")] =
contactRecord;
}
}
var output = "<table class='sortable' id='table1'>";
output += "<tr>";
output += "<th>Account</th>";
output += "<th>Portfolio</th>";
output += "<th>Last Activity</th>";
output += "<th>Primary Contact</th>";
output += "</tr>";
for (var i=0; i < queryResult.records.length; i++)
{
var Account = queryResult.records[i];
var primaryContact = primaryContactByAccountId[Account.get("Id")];
var lastRow = "";
lastRow = primaryContact.get("LastName");
output += "<tr>";
output += "<td><a href='/"+Account.get("Id")+"' target='NEW'>" +
Account.get("Name") + "</a></td>";
output += "<td>" + Account.get("Portfolio_Rank__c") + "</td>";
output += "<td>" + Account.get("SystemModstamp") + "</td>";
output += "<tdNEED TO PUT PRIMARY CONTACTS HERE </td>";
output += "</tr>";
}
output += "</table>";
document.getElementById("ResultsHolder").innerHTML = output;