1

My org has an account object with a related list of Contacts. I am querying for the Contacts from the Account and want to use the data on the Contact object. How can I access fields from the contact objects?

Code:

<script type="text/javascript">
  var jq$ = jQuery.noConflict();
    jq$(document).ready(function() {                         
        try{
            sforce.connection.sessionId = '{!$Api.Session_ID}';
            var qr = sforce.connection.query("Select Id, Name, (Select Id, Name, Address_Verified_Date__c, Child_Document_Verification_Date__c From Contacts) From account where id = " + "'" + "{!Account.id}" + "'");
            alert('I am here');
            var records = qr.getArray("records");
            alert(records.length);
         }
        catch(err){
            alert(err.message);
        }
    });
</script>
2
  • Did you examine the structure of records? I would use console.log(JSON.stringify(records)); to look at the structure. I'm not sure how nested records are returned in the result set. May 8 '15 at 14:36
  • It's not a JSON string, otherwise I would have used JSON.parse. I found a workable solution though that I will post. May 8 '15 at 14:39
2

Solution:

var jq$ = jQuery.noConflict();
    jq$(document).ready(function() {                         
        try{
            sforce.connection.sessionId = '{!$Api.Session_ID}';
            var qr = sforce.connection.query("Select (Select Id, Name, Address_Verified_Date__c, Child_Document_Verification_Date__c From Contacts) From account where id = " + "'" + "{!Account.id}" + "'");
            var records = new sforce.QueryResultIterator(qr);
            var contacts = [];
            while(records.hasNext()){
                var account = records.next();
                if(account.Contacts){
                    var cit = new sforce.QueryResultIterator(account.Contacts);
                    while(cit.hasNext()){
                        contacts.push(cit.next());
                    }
                }
            }
            alert(contacts.length);
         }
        catch(err){
            alert(err.message);
        }
    });
2
  • Nice. Thanks. Curious if you considered JS Remoting or not? If so, why did you go with this approach? May 8 '15 at 18:05
  • 1
    @ArtieBrosius I went with this approach because this project is for a non profit. I wanted a quick and dirty approach instead of needing to write a class and also write tests for it. May 8 '15 at 18:20
1
var qr = sforce.connection.query("SELECT Id, Name, (SELECT Id, Name, Address_Verified_Date__c, Child_Document_Verification_Date__c FROM Contacts) FROM Account WHERE Id = '{!Account.id}'").getArray("records");
var records = qr[0].Contacts.getArray("records");

That will enable you to use the fields within the Contact e.g records.Address_Verified_Date__c, records.Id, records.Child_Document_Verification_Date__c, etc...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.