Trouble combining arrays in a table

Discussion in 'Javascript' started by ferraro.joseph@gmail.com, Jul 27, 2006.

  1. Guest

    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.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.get("Id") + "'" + " and IsPrimary = true)";
    if (i < contactQuery.records.length - 1) {
    contactIdCriteria += " or "};
    contactsById[contactQuery.records.get("Id")] =
    contactQuery.records;



    }


    <!-- 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;
    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;
    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 += "<td> NEED TO PUT PRIMARY CONTACTS HERE </td>";
    output += "</tr>";



    }


    output += "</table>";
    document.getElementById("ResultsHolder").innerHTML = output;
    , Jul 27, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > 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).


    You should post an example of the objects that are being returned, then
    someone might show you how to combine the arrays in the objects to
    create a table. I am not going to try to reverse-engineer the objects
    from your usage. :)

    [...]

    > var output = "<table class='sortable' id='table1'>";
    > output += "<tr>";


    Using the += compound operator to concatenate strings is notoriously
    slow in some browsers, you are probably better to use an Array and join
    it:

    var output = ['<table class="sortable" id="table1">'];
    output.push('<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;
    > var primaryContact = primaryContactByAccountId[Account.get("Id")];
    > var lastRow = "";


    There is no need to declare the variables on every loop (it doesn't
    hurt, it's just pointless). Declare them once just before the loop,
    then just set their value inside. If there are many rows, it will be
    faster to get the length of queryResult.records just once - and set
    lastRow once per loop, not twice (lastRow does not seem to be used
    anywhere - should it be?):

    var Account, primaryContact, lastRow;
    var j = queryResult.records.length;
    for (var i=0; i < j; i++) {
    Account = queryResult.records;
    primaryContact = primaryContactByAccountId[Account.get("Id")];
    lastRow = primaryContact.get("LastName");

    > lastRow = primaryContact.get("LastName");
    > output += "<tr>";


    [...]

    > document.getElementById("ResultsHolder").innerHTML = output;


    ... = output.join('');

    What does output look like when you get to here, does it appear to be
    valid HTML?

    In most browsers, inserting a table using DOM will be just as fast,
    much more reliable, standards compliant and less code than your use of
    innerHTML. e.g.

    var table, thead, row, cell, cellText;
    table = document.createElement('table');
    table.className = 'sortable';
    table.id = 'table1'
    thead = table.createTHead();
    row = thead.insertRow(-1);

    for ( colTitle in {'Account':'', 'Portfolio':'',
    'Last Activity':'','Primary Contact':''}){
    cell = row.insertCell(-1);
    cell.appendChild(document.createTextNode(colTitle));
    }

    var account, primaryContact, lastRow, alink;
    var j = queryResult.records.length;
    for (var i=0; i<j; i++){
    account = queryResult.records;
    primaryContact =
    primaryContactByAccountId[account.get("Id")];
    lastRow = primaryContact.get("LastName");
    row = table.insertRow(-1);
    cell = row.insertCell(-1);
    alink = document.createElement('a');
    alink.href = '/' + Account.get("Id");
    alink.target = 'NEW';
    alink.appendChild(document.createTextNode(accountName.get('Name'));
    cell.appendChild(alink);
    for (cellText in { Account.get("Portfolio_Rank__c"):'',
    Account.get("SystemModstamp"):'',
    "PUT PRIMARY CONTACTS HERE":''} ){
    cell = row.insertCell(-1);
    cell.appendChild(document.createTextNode(cellText));
    }
    document.getElementById("ResultsHolder").appendChild(table);


    Untested of course, but you should get the idea.


    --
    Rob
    RobG, Jul 28, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,109
    Philipp
    Jan 20, 2009
  2. Derek Cannon

    Trouble with Pushing Arrays to Arrays

    Derek Cannon, Apr 17, 2010, in forum: Ruby
    Replies:
    5
    Views:
    96
    David A. Black
    Apr 17, 2010
  3. combining two arrays

    , Feb 28, 2005, in forum: Perl Misc
    Replies:
    8
    Views:
    127
    Paul Lalli
    Mar 3, 2005
  4. Keith Lee
    Replies:
    8
    Views:
    106
    robic0
    Feb 22, 2006
  5. mike

    Combining object arrays

    mike, Sep 14, 2005, in forum: Javascript
    Replies:
    10
    Views:
    167
Loading...

Share This Page