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
geo789geo789 

query using wildcard/bind varibale

Hey..I am kinda new to Salesforce/apex/visualforce. Plz help me resolve the following issue.

 

for(TP_TPM_DEAL_TYPE_TBL__c dea:(Database.query('Select DEAL_TYPE__c,DEAL_TYPE_NAME__c From TP_TPM_DEAL_TYPE_TBL__c where DEAL_TYPE__c like :(SearchCriteria.DEAL_TYPE__c+'%') order by ' + sortFullExp + ' limit 1000'))){
     wrapList.add(New wrapperclass(dea,false));
   }

 

error messgae:

 

Save error: line 53:174 no viable alternative at character '%'

 

What is the difference between using database.query and using a query directly in []?

 

Any help is greatly appreciated!Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You have to create the wildcard string outside of the SOQL query and bind that in, otherwise it has no effect even if there is no syntax error:

 

String wcVal=SearchCriteria.DEAL_TYPE__C + '%';

for(TP_TPM_DEAL_TYPE_TBL__c dea:(Database.query('Select DEAL_TYPE__c,DEAL_TYPE_NAME__c From TP_TPM_DEAL_TYPE_TBL__c where DEAL_TYPE__c like :wcVal order by ' + sortFullExp + ' limit 1000'))){
     wrapList.add(New wrapperclass(dea,false));
   }
 

 

All Answers

bob_buzzardbob_buzzard

You have to create the wildcard string outside of the SOQL query and bind that in, otherwise it has no effect even if there is no syntax error:

 

String wcVal=SearchCriteria.DEAL_TYPE__C + '%';

for(TP_TPM_DEAL_TYPE_TBL__c dea:(Database.query('Select DEAL_TYPE__c,DEAL_TYPE_NAME__c From TP_TPM_DEAL_TYPE_TBL__c where DEAL_TYPE__c like :wcVal order by ' + sortFullExp + ' limit 1000'))){
     wrapList.add(New wrapperclass(dea,false));
   }
 

 

This was selected as the best answer
geo789geo789

Sorry to bother you. But now i am getting another error.

 

 

Attempt to de-reference a null object

 


I have used an input field to input the data in the page

<apex:inputField value="{! SearchCriteria.DEAL_TYPE__c}"/>

 

And has used a getter/setter in the controller

public TP_TPM_DEAL_TYPE_TBL__c SearchCriteria{get;set;}

 

 

bob_buzzardbob_buzzard

Have you created an instance of the record to store the results in? 

 

If not, you'll need the following in your constructor:

 

SearchCriteria=new TP_TPM_DEAL_TYPE_TBL__c();

 

geo789geo789

My bad. I had conveniently forgot that.Thanks a lot for your help!!!!