0

I am using dynamic soql where I am reading the conditions from an XML and getting those as a String but I am not able to find it who the query will structure

String query = 'SELECT id, Name FROM CustomObject__c WHERE Status__c =: InProgress';
Database.query(query);

It throws error message as

System.QueryException: Variable does not exist: InProgress

0

3 Answers 3

5

use variable binding

String status = 'InProgress';
String query = 'SELECT id, Name FROM CustomObject__c WHERE Status__c = :status';
List<CustomObject__c> result = Database.query(query);

it is preferable to use variable binding than string concatenation in order to prevent SOQL Injections. Also, if it not necessary to use dynamic SOQL use static SOQL.

String status = 'InProgress';
List<CustomObject__c> result = [
    select id, Name
    from CustomObject__c
    where Status__c =:status
    ];
1
  • actually all the fields I am reading from compact layout and all the where condition I am reading from a xml file store in one object record and passing it to the the dynamic soql so not sure who I will do it.. Commented Nov 30, 2017 at 9:56
1

As Alexander said in his answer, use variable binding or static SOQL.

If you do need to use dynamic SOQL without variables, you will need to add these values as a string(=escaping single quotes):

String query = 'SELECT id, Name FROM CustomObject__c WHERE Status__c = \'InProgress\'';

Note, that I removed : from the query, which is used by Apex to understand that you are using a variable there.

-1

You must add InProgess in quotes('InProgress').

String query = 'SELECT id, Name FROM CustomObject__c WHERE Status__c =: 'InProgress' '; Database.query(query);

1
  • your code will give compilation error
    – Novarg
    Commented Nov 30, 2017 at 10:01

You must log in to answer this question.

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