I'm trying to get a list of Contact Roles. The code is a Visualforce Page controller that first takes the current UserId and looks up all Accounts that are owned by that user, inserts them into a Map and then populates a list of those Contacts that a have a AccountContactRole based on those accounts. Here's my current code:

private List<SelectOption> contactResults = new List<SelectOption>();
    public String strSearch {get;set;}
    public String selectedSearch {get;set;}
    public Boolean bOnlyMyContacts {get;set;}
    public Boolean bOnlyMyAccounts {get;set;}
    public Campaign cCampaign {get;set;}
    public String theString {get;set;}
    public List<String> addIds = new List<String>();
    public List<String> removeIds = new List<String>();
    public String size {get;set;}
    public Map<Id, Boolean> idMap = new Map<Id,Boolean>();
    public Map<Id, Id> myAcctMap = new Map<Id,Id>();
        Id myId = UserInfo.getUserId();

public void doSearch(){
        if( strSearch.length() > 2 ){
            List<Account> myAccnts = [SELECT Id,OwnerId from Account where OwnerId =: UserInfo.getUserId()];
            for(Account ac : myAccnts){
            contactResults = new List<SelectOption>();
            Id myId = UserInfo.getUserId();
            RecordType rt = Util_GetRecordType.getRecordType('Contact','Company contact');                

        String soqlQuery;

        if( strSearch == 'My Accounts'){
            soqlQuery = 'Select Id, FirstName, LastName, Email, Title, Account.Name from Contact where Account.OwnerId = \'' + myId + '\'';
        else if( strSearch == 'My Contacts'){
            soqlQuery = 'Select Id, FirstName, LastName, Email, Title, Account.Name from Contact where Contact.OwnerId = \'' + myId + '\'';
        else if( strSearch == 'My Contact Roles'){
            soqlQuery = 'Select Id, FirstName, LastName, Email, Title, Account.Name from Contact where Id in (SELECT ContactId from AccountContactRole where AccountId IN:  myAcctMap.keySet()';
            System.debug('SOQL is: '+soqlQuery);


this is my relevant VF Page code:

             <apex:inputText title="" value="{!strSearch}"/>
            <apex:commandButton value="Search" action="{!doSearch}" rerender="resultsView" />
            only my contacts? <apex:inputCheckbox value="{!bOnlyMyContacts}" onChange="doSearch()"/>
            only my accounts? <apex:inputCheckbox value="{!bOnlyMyAccounts}" onChange="doSearch()"/>

What I get when I select 'My Contact Roles' from a dropdown and click 'Go' to populate a list, I get on my page 'unexpected token: '(' Error is in expression '{!doSearch}' in component in page managecampaignmembers.

I'm guessing that my 'My Contact Roles' query is a bit malformed. This is the correct method of accessing a Map in a query,no?

  • In your My Contact Role string search, it looks like you are missing a closing parentheses ')'. May 8 '14 at 11:40

As sfdc_ninja mentions there is a trailing bracket missing. But also in dynamic SOQL only "simple bind variables" are allowed (no dotted expressions) so code something like this is needed:

private Set<Id> myAcctIds = myAcctMap.keySet();
else if( strSearch == 'My Contact Roles'){
    soqlQuery = ''
    + ' Select Id, FirstName, LastName, Email, Title, Account.Name'
    + ' from Contact'
    + ' where Id in (SELECT ContactId from AccountContactRole where AccountId IN :myAcctIds)

Note that the queries you have listed in your question do not need to be dynamic (string based) and using the normal query syntax (in square brackets) would get you compile-time syntax checking and allow more complicated binding expressions too.

  • Thanks for that tip Keith. I was not aware of the dynamic SOQL restriction. Your <Set> addition works just fine. Thanks!
    – akarnid
    May 8 '14 at 22:04

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.