-4

Controller:

public with sharing class textInputsCon {
     public String inputText1{get;set;} // input text1 value  from vf
     public String inputText2{get;set;} // input text2 value  from vf  
      public String inputText3{get;set;} // input text1 value  from vf
     public String inputText4{get;set;}
     Public string operator{get;set;}

     public list<Quote__c> quo{get;set;}       //this is will hold data to be displayed on page
     string query='select from1__c,Quote_number_new__c from  quote__c ';
     public void showlist(){ //do not return anything
     if(inputText1 <> NULL ){
     //&& inputText4 ==Null){
     query += 'WHERE ' + inputText3 + ' ' + operator + '  : inputText1 ' ;   }
    if(inputText2 <> NULL ){
     query +=' and  '+ inputText4 + ' ' + operator + ' : inputText2';  }       
     quo = database.query(query);
     }   
     }

vf code:

<apex:page showHeader="false" sidebar="False" controller="textInputsCon">
    <apex:form >
      Input Text1 <apex:inputText value="{!inputText1}"/>

      <apex:selectList id="inputText3" value="{!inputText3}" size="1">
            <apex:selectOption itemValue="Quote_number_new__c" itemLabel="Quote Number"/>
            <apex:selectOption itemValue="From1__c" itemLabel="from"/>
             <apex:selectOption itemValue="Null" itemLabel="None"/>
        </apex:selectList> 
            Input Text2 <apex:inputText value="{!inputText2}"/>
      <apex:selectList id="inputText4" value="{!inputText4}" size="1">
            <apex:selectOption itemValue="Quote_number_new__c" itemLabel="Quote Number"/>
                 <apex:selectOption itemValue="From1__c" itemLabel="from"/>
            <apex:selectOption itemValue="From1__c" itemLabel="None"/>
        </apex:selectList>        
      <apex:selectList id="operator" value="{!operator}" size="1">
            <apex:selectOption itemValue="=" itemLabel="Equal"/>
            <apex:selectOption itemValue="!=" itemLabel="Not equal to"/>
            <apex:selectOption itemValue="<=" itemLabel="Less than"/>
        </apex:selectList> 
        <apex:commandButton value="list" action="{!showlist}"/>
         <apex:pageBlock >
            <apex:pageBlockTable value="{!quo}" var="q">
                <apex:column value="{!q.Quote_Number_New__c}"/>
                 <apex:column value="{!q.From1__c}"/>
            </apex:pageBlockTable> 

        </apex:pageBlock>
    </apex:form>
    </apex:page>

BY entering the text1 and text2 it is showing the results:

if(inputText1 <> NULL ){
     //&& inputText4 ==Null){
     query += 'WHERE ' + inputText3 + ' ' + operator + '  : inputText1 ' ;   }
    if(inputText2 <> NULL ){
     query +=' and  '+ inputText4 + ' ' + operator + ' : inputText2';  }       
     quo = database.query(query);

By entering the text1 it is filtering but not accepting the text two:

 if(inputText1 <> NULL ){
         //&& inputText4 ==Null){
         query += 'WHERE ' + inputText3 + ' ' + operator + '  : inputText1 ' ;   }
        if(inputText2 <> NULL && inputtext1==NULL ){
         query +=' and  '+ inputText4 + ' ' + operator + ' : inputText2';  }       
         quo = database.query(query);

i need that in such a way that if i entered the text1 it has to show the results of text 1 and if i entered the data of text2 .it has to show the data of text2.if i entered the both .it has to filter by both can you correct my program?

closed as too broad by bigassforce, Matt Lacey Feb 25 '14 at 5:12

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please paste the VF page code for better understanding . – Soberano Feb 4 '14 at 5:23
3
string query='select from1__c,Quote_number_new__c from quote__c ';
     public void showlist(){ //do not return anything
     if(inputText1 != null){

     query += 'WHERE Quote_number_new__c ' + operator + ' : inputText1';

     }

Update your controller with this code . The query was not formatted properly. After from quote__c the white space was missing. Your query ended up like below error image.

ErrorImage

  • if(inputText1 != null){ query += 'WHERE Quote_number_new__c ' + operator + ' : inputText1'; } if(inputText2 != null){ query += 'WHERE Quote_number_new__c ' + operator + ' : inputText1 and from1__c ' + operator + ' : inputText2'; } if i wrote second one again i am getting the error System.QueryException: unexpected token: 'Quote_number_new__c' – Teja Feb 4 '14 at 6:11
  • Have a look on my program .i had modified the controller .again i am getting the error – Teja Feb 4 '14 at 6:14
1

You can refernce the following code. Use escapeSingleQuotes() methods while passing input.I have wriiten it on account object.You can replace it with your custom object

public String inputText2{get;set;} 
inputText2 = 'Account1';
String operator = '=';
String query1 ='select Name,area__c from Account  ';
String query =query1 + ' WHERE Name '+operator+' \'' + String.escapeSingleQuotes(inputText2) + '\'';
 quo = database.query(query);

  • no it is my requirement – Teja Feb 4 '14 at 5:24
  • Thiss change will fulfill your requirement – Sonal Feb 4 '14 at 5:52
  • @Sonal it was just space issue and not wrt special chars. PFA image above how the query ended up before call. – Soberano Feb 4 '14 at 5:59
  • @Soberano I executed your code with account object public list<Account> quo{get;set;} public String inputText2{get;set;} inputText1 = 'Account1'; String operator = '='; String query ='select Name,area__c from Account '; query+='WHERE Name ' + operator + ' : inputText1'; quo = database.query(query); – Sonal Feb 4 '14 at 6:05
  • @Soberano Compile error at line 4 column 1 Variable does not exist: inputText1 – Sonal Feb 4 '14 at 6:05
1

Update your conditional logic like this.

system.debug('inputText1:'+inputText1+'   :: inputText2: '+inputText2);
if(inputText1 <>null and inputText2==null){
system.debug('inside if-else-1');
query +='WHERE '+inputText3  ' + operator +' \''+inputText1+'\'';
}
else if(inputTest2<>null and inputText1 == null){
query +='WHERE '+inputText4  ' + operator +' \''+inputText2+'\'';
system.debug('inside if-else-2');
}
else if(inputTest2<>null and inputTest1<>null){
query +='WHERE '+inputText3  ' + operator +' \''+inputText1+'\''+ ' AND inputText4  '+ operator +' \''+inputText2+'\'';
system.debug('inside if-else-3');
}

If you still get some error, check values for inputText1 and inputText2 in debug logs.

  • change 'and' with '&&' logical operator in if-else logic.Sorry for the mistake! – userRg Feb 4 '14 at 11:09
0

Try removing the ':' with this code

string query='select from1__c,Quote_number_new__c from quote__c ';
     public void showlist(){ //do not return anything
     if(inputText1 != null){

     query += 'WHERE Quote_number_new__c ' + operator + '  inputText1';

     }
  • no it is not taking – Teja Feb 4 '14 at 7:45
  • if(inputText1 <> NULL ){ //&& inputText4 ==Null){ query += 'WHERE ' + inputText3 + ' ' + operator + ' : inputText1 ' ; } if(inputText2 <> NULL ){ query +=' and '+ inputText4 + ' ' + operator + ' : inputText2'; } quo = database.query(query); – Teja Feb 4 '14 at 7:48
0

Update your conditions to as follow :

if(inputText1 != null || inputText2 != null) {
    query += 'where ';

    if(inputText1 != null) {
        query += inputText3 + ' ' + operator + '  : inputText1 ' ;   
    }

    if(inputText2 != null && inputText1 != null) {
        query +=' and  '+ inputText4 + ' ' + operator + ' : inputText2';   
    }else if(inputText2 != null && inputText1 == null) {
        query += inputText4 + ' ' + operator + ' : inputText2';   
    }
}

Please let me know if this doesn't work..........

  • no it is not showing the records – Teja Feb 4 '14 at 9:17
  • it is working when i enter both the text fields .individually it is not working – Teja Feb 4 '14 at 9:22
  • It should work. Can you please confirm how it's showing wrong result with single value ? – S.Sharma Feb 4 '14 at 9:25
  • it is not throwing any error .when i click on list it not showing any records when i enter both the text box .it is showing the record – Teja Feb 4 '14 at 9:29
  • Can you debug the query in code and check what query is executing ? – S.Sharma Feb 4 '14 at 10:22
0

It seems that 'inputtext1' is a field value for "Quote_number_new__c".

So to evaluate a condition try to reform your query like this:

string query='select from1__c,Quote_number_new__c from quote__c ';
if(inputText1 != null){
 query += 'WHERE Quote_number_new__c ' + operator +' ' +inputText1+'';
}

Provide a space between 'Quote__c' and 'Where' clause

Notice I am not including inputText1 within quotes.

I'm not getting why you are using ':' in the query string. I think the above code snippet would work.

  • unexpected token: 'Alexis' this is the thing 'Alexis' i had typed in input1 .but i am getting error like above – Teja Feb 4 '14 at 9:48
  • try to use system.debug after and before Database.query(String) statement and check the results in Query Editor of developer console. It may be cause of sql injection. Can you try this: 'WHERE Quote_number_new__c ' + operator +' ' +'\''+inputText1+''\'; – userRg Feb 4 '14 at 10:02
0

I have tried to do similar stuff as follows:

string operator = '=';
string str = 'Test';
string q = 'select name from ABC__c where name '+operator+'\''+str+'\'';
sytem.debug(database.query(q));

I am getting correct result with record id.

You can try this code with your customObject__c and field value.

  • i am using dynamically inputtext1 ,from the user input it will take teh value – Teja Feb 4 '14 at 10:29
  • Its ok, when you are taking value from the vf page, i guess you must performing some action to pass the input value back to the controller. So in the function body assign the userinput in a string type variable(let say 'str' will be a new variable containing the user-input ) and then you can try to query as mentioned above. – userRg Feb 4 '14 at 10:38
  • ya its working .still my problem not yet solved .it remains the same. check it out at the top with this heading BY entering the text1 and text2 it is showing the results: same problem is still going on – Teja Feb 4 '14 at 10:47
-1
       if(inputText1<>''){
         query += 'WHERE ' + inputText3 + ' ' + operator + ' : inputText1' ;  } 
          if(inputText2<>''){
         query +=' and  '+ inputText4 + ' ' + operator + ' : inputText2';    }   
         quo = database.query(query);

but this change i am getting exactly what i need .thankyou for your answers

Not the answer you're looking for? Browse other questions tagged or ask your own question.