function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Swamy PSwamy P 

Dynamic SOQL

Hi,

     I wrote Dynamic  soql  for getting records,but am not able to get the solution..Could any one resolve it..........

Error is::

System.QueryException: line 1:59 no viable alternative at character '%'

Error is in expression '{!gettin_objs}' in component <apex:page> in page recsearch
 
Apex Code:
public class recsearchcls {
       public string searchword{get;set;}
   List<SelectOption> finalobj= new List<SelectOption>();

public PageReference gettin_objs() {
  string fword= '%'+searchword+'%';
      List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
             for(Schema.SObjectType f : gd){  
             if(searchword!=null || searchword!=''){     
                list<sobject> ss=Database.query('Select id,Name from '+f+'where Name Like: '+fword);
              
                //if(ss.NOT(isempty())){
                 finalobj.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getName()));    
         // }
         }
       }
    return null;
}

}

-------

v.f:

<apex:page controller="recsearchcls">
    <apex:form >
    <apex:PageBlock >
      <br/><h1><b>Search In Multiple Objects</b></h1><br/><br/><br/>
      <b>Enter Here:</b>   <apex:inputtext value="{!searchword}" onkeydown="sss()">
      <apex:actionfunction action="{!gettin_objs}" name="sss"/>
      </apex:inputtext>
      <apex:commandButton action="{!search}" value="Search"/>
      <apex:pageBlockSection >
      <apex:selectCheckboxes value="{!check}" style="float:left" layout="pageDirection"  >
       <apex:selectOptions value="{!ren}"/>     
       </apex:selectCheckboxes>
     </apex:pageBlockSection>
     </apex:PageBlock>
      </apex:form>  
</apex:page>

 

prakash_sfdcprakash_sfdc
list<sobject> ss=Database.query('Select id,Name from '+f+'where Name Like:'+'%'+searchWord+'%');
souvik9086souvik9086

Hi,

 

Try this

 

list<sobject> ss=Database.query(

                                    'Select id,Name from '+f+'where Name Like'+'\'%'+String.escapeSingleQuotes(searchWord)+'%\'');

 

If this post solves your problem, kindly mark it as solution.

 

Thanks

Swamy PSwamy P

Thanks For your Reply  But geting same Error i.e

System.QueryException: unexpected token: Like

 

Naidu PothiniNaidu Pothini
list<sobject> ss = Database.query('Select id,Name from '+f+'where Name Like:fword');

 try this

JHayes SDJHayes SD

In your call to Database.query() you are using a bind variable where it isn't needed -- simply surround the search filter in single quotes.  This worked just fine for me:

 

String f = 'Account';
String fword = '%Gene%';
list<sobject> ss=Database.query('Select id,Name from '+ f +' where Name Like \''+ String.escapeSingleQuotes(fword) + '\'');
for (SObject s : ss) {
	Account a = (Account) s;
	System.debug(a.Name);
}

 

Bind variable syntax (i.e. :myVariable) should be used inside bracket notation.

 

Regards,

Jeremy