Search code examples
salesforceapex-codedynamic-sqlvisualforcesoql

Dynamic SOQL Query


I'm trying this code for dynamic results but it's showing the error shown below. Kindly help me with this code.

System.QueryException: Only variable references are allowed in dynamic SOQL/SOSL.

      boolean first = true;
      string database_query = '';
       List<Schema.FieldSetMember> userfields = SObjectType.Task__c.FieldSets.Search.getFields();
     for(Schema.FieldSetMember f : userfields) {  
        if (!first) {
               database_query += ', ';                
         }
         first = false;
         database_query += f.getFieldPath(); 
     }
    if(Search_value != '' && database_query != ''){           
      TaskList  = Database.query('select ' + database_query +',SOE__c from task__c where SOE__c =: \''+selectSOE +'\' and '+ selectedfield + ' like \'%' + Search_value +'%\'');           
    }

Solution

  • Please try to replace the ':=' with just '=' in your query. Also have a look at the answer to this question, I think you have similar problem:

    Query using string not working