1

Filter criteria

vf page :

<apex:page showHeader="false" sidebar="False" controller="textInputsCon">
    <apex:form >
       Input Text1 <apex:inputText value="{!inputText1}"/>
       Input Text2 <apex:inputText value="{!inputText2}"/>
      <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>

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 operator{get;set;}
     public list<Quote__c>  quo{get;set;}       //this is will hold data to be displayed on page
    // public list<Quote__c> quo1{get;set;}
     public void showlist(){ //do not return anything
     if(inputText1==null && inputText2==null)
    quo= [select from1__c,Quote_number_new__c from quote__c];
    if(inputText1!=null && inputText2==null)
    quo= [select from1__c,Quote_number_new__c from quote__c where Quote_number_new__c=:inputtext1 ];
     }
     }

if i dont type the boxes it has to show all the records . if i type in textbox1 .i has to be filter by textbox1. if i type in textbox2.i has to filter by both textbox1 and textbox2.

it would be more helpful we can get the criteria of equal and not equal to

2

In this case you should use Dynamic SOQL : Using string you can easy add query criteria based on fields values.

example:

public String operator {get; set;}
public String inputText1 {get; set;}
public String inputText2 {get; set;}
String query = 'SELECT Id FROM MyCustomObject__c ';
if(inputText1 != null){
    query += 'WHERE Name ' + operator + ' : inputText1';
}
if(inputText2 != null){
    query += ' OR Name ' + operator + ' : inputText2';
}
List<sObject> L = Database.query(query );

You just have to build query string, in my example if you fill in both fields you will get:

1.(operator = '=', inputText1 = 'some Name', inputText2 = 'other Name')

'SELECT Id FROM MyCustomObject__c WHERE Name =: inputText1 OR Name =: inputText2'

2.(operator = '=', inputText1 = null, inputText2 = null)

'SELECT Id FROM MyCustomObject__c'

9
  • hi your post is so confusing can you repost it
    – Teja
    Feb 3 '14 at 10:15
  • Is that better now? Feb 3 '14 at 10:22
  • ya thankyou.how can i show data in visualforce
    – Teja
    Feb 3 '14 at 10:29
  • can you tell me how can i call this one List<sObject> L = Database.query(query ); in vf
    – Teja
    Feb 3 '14 at 10:40
  • I guess the best Idea will be create some kind of search button and assign method witch execute this query to it. Feb 3 '14 at 11:18
1

For Condition :- . if i type in textbox1 .i has to be filter by textbox1. if i type in textbox2.i has to filter by both textbox1 and textbox2.

To filter by both textbox1 and textbox2.:-

List inputTextList = new List(); inputText.add(inputtext1,inputtext2); quo= [select from1__c,Quote_number_new_c from quote_c where Quote_number_new__c in:inputTextList ];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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