0

I have an object with a picklist including two values "status" (Valid,Invalid).. I'm using search in my VF page dependent on the selected status from this picklist, so the SOQL should return the values where the status is equal "selected value".

While I'm searching for a solution I found this example, put should I use this complex way ! I tried to get the value as following but i got error.

VF Page Code:

<apex:pageBlock title="Account Search">
      <apex:pageBlockSection>
            <apex:inputField id="status" value="{!account.Status__c}"/>
      </apex:pageBlockSection>
      <apex:pageBlockButtons>
           <apex:commandButton value="Search" action="{!getAccounts}"/>
      </apex:pageBlockButtons>
</apex:pageBlock>

AccController Code:

public Account Account{set;get;}
public list<Account> Accounts{set;get;}

public AccController(){
        Account= new Account();
    }

public PageReference getAccounts(){

        Accounts = Database.query('SELECT Name
                                   from Account
                                   WHERE Status__c = account.Status__c');
        return null;
    }

The Error : System.QueryException: Variable does not exist: account.Status__c And If I put account.Status__c outside the singe comma like this WHERE Status__c = ' + account.Status__c And Choose a value, for Example Valid, I got this Error

System.QueryException: unexpected token: 'Valid'

2 Answers 2

1

Dynamic SOQL bind variables can only be simple variables e.g. :status not dotted expressions such as references to fields e.g. :account.Status__c in the query string. Here is the documentation.

In the same link you can find the solution; you assign your account.Status__c to a simple variable and use it in the query string instead:

String status = account.Status__c;
Accounts = Database.query('SELECT Name from Account WHERE Status__c = :status');
4
  • Someone reading just your first sentence would me misled: you "can’t use bind variable fields" but you "can use simple bind variables".
    – Keith C
    Mar 25, 2014 at 9:42
  • agreed @KeithC. I answered it by explaining as it required to the raised question and giving the details to what I'm saying. If it looks like misleading kindly update or let me know a better way to express it.
    – highfive
    Mar 25, 2014 at 9:49
  • works fine with variable string query = account.status__c; '... WHERE Status__c =: query'; thank you :) Mar 25, 2014 at 9:50
  • Glad to help you. And it's pleasure to be a member of such a community where people support for the success of the community, @KeithC.
    – highfive
    Mar 25, 2014 at 9:57
0

try

Accounts = Database.query('SELECT Name
                           From Account
                           WHERE Status__c =: account.Status__c');
2
  • no problem with this way, it works fine.. but I need to use Database.Query because I have more than Search Criteria, I'm using if statements to append search criteria Mar 25, 2014 at 9:37
  • System.QueryException: Variable does not exist: holiday.Status__c but i put it in a variable with type string and it works fine ! Mar 25, 2014 at 9:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .