+ Start a Discussion

Perform an additional SOQL query

Below is an extension that brings back contact roles that are related to an account and have a type = Board Member.  The extension is called from a visualforce page which is triggered by a button on an account page the results are displayed in a table on the page - this bit works well.:smileyhappy:


For the contacts retrieved I need to perform an additional query to see what other board member contact roles they have and display them in a separate table.




So query 1 would bring back Joe Bloggs who is a board member of ABC Co. And Jane Doe who is a board member of ABC Co.


I need query 2 to use Joe and Janes contact ID and search to find any contact roles that have a type of board member and are not ABC Co.


Not sure how to join these statements, any suggestions welcome - thanks in advance.




public with sharing class AccountListExtension { private final Account acct; public AccountListExtension(ApexPages.StandardController controller) { this.acct = (Account)controller.getRecord(); } public List<Contact_Role__c> getBoardmembers(){ List<Contact_Role__c> listBd = [Select c.id, c.contact__r.name, c.title__c, c.State__c, c.primary_account__c from Contact_Role__c c where c.account__r.Id = :this.acct.Id and c.Contact_Role__c='Board Member']; return listBd; }}




SOQL doesn't support joins of this nature. So you would need a second query to get the information that you need. Make sure that you read ContactId in the first SOQL.  Loop through the result set and read the contact ids into a list.  Now fire the second SOQL that reads Contact_Role with "ContactId in :cntctId" clause and other clauses excluding the account in the first query.  Hope this helps.
Thanks for the reply, to be honest I am very new to apex code and soql statements.  To get this far I have tailored a few examples and tutorials.  Do you have any examples or help files that might help me get started.  Thanks again.

You can find SOQL documentation here -http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content%2Fsforce_api_calls_soql.htm|SkinName=webhelp


To understand the basic Apex language constructs, start with the first few chapters of this reference documentation.



Hope this helps.