Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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'

share|improve this answer
    
hi your post is so confusing can you repost it –  Teja Feb 3 at 10:15
    
Is that better now? –  Artur Kępczyński Feb 3 at 10:22
    
ya thankyou.how can i show data in visualforce –  Teja Feb 3 at 10:29
    
can you tell me how can i call this one List<sObject> L = Database.query(query ); in vf –  Teja Feb 3 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. –  Artur Kępczyński Feb 3 at 11:18

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 ];

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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