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
Cynthia Capobianco 163Cynthia Capobianco 163 

Fetch Opportunity records filtered by FromAmount and Toamount

Hi all, I'm new to SF. I'm trying to fetch Opportunity records in a VF page using FromAmount and Toamount, dymically using database.query.
Any help on this would be very useful Your help much appreciated. Thanks.
swati_sehrawatswati_sehrawat
Can you share the code over here.
Cynthia Capobianco 163Cynthia Capobianco 163
This is my code.

public with sharing class OppCOntrolerAmount {
 public Decimal FromAMount{get;set;}
 public List<Opportunity> oplist {set;get;}
 public Decimal ToAMount{get;set;}
 
 public Void search(){
 if(FromAMount!=null && ToAmount!=null )
    {
         oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount>=:FromAMount AND Amount<=:ToAMount');
        
    }
    else If(ToAmount!= null)
    {
       oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount<=:ToAMount '); 
            }
            else If(FromAmount!= null)
     {          
            oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount>=:FromAMount '); 
}}
swati_sehrawatswati_sehrawat
Ok, so what is the issue you are facing in this?
Cynthia Capobianco 163Cynthia Capobianco 163
Thanks for responding. The problem is else conditions are not working. Page displayed Opp records only when I enter both FromAmount and Toamount. Thanks.
swati_sehrawatswati_sehrawat
Try this
 
public with sharing class OppCOntrolerAmount {
	public Decimal FromAMount{get;set;}
	public List<Opportunity> oplist {set;get;}
	public Decimal ToAMount{get;set;}
 
	public Void search(){
		if(FromAMount!=null && ToAmount!=null ){
			oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount>=:FromAMount AND Amount<=:ToAMount');
        
		}
		else If(ToAmount!= null && (FromAMount==null || FromAMount<0)){
			oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount<=:ToAMount '); 
        }
        else If(FromAmount!= null && (ToAmount==null || ToAmount<0)){          
			oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount>=:FromAMount '); 
		}
	}	
}

 
Cynthia Capobianco 163Cynthia Capobianco 163
Thank you so much for your effort. still the problem exists with the last 'else' condition( only if  FromAmount is filled by user)
swati_sehrawatswati_sehrawat
Can you put debug logs and check what are values of FromAmount  and ToAmount and in which conditions it is going, because i tested these conditions and they are working fine for me.
Cynthia Capobianco 163Cynthia Capobianco 163
User-added image

19:44:15.0 (21229637)|USER_DEBUG|[17]|DEBUG|From AMount is____99
19:44:15.0 (21307897)|USER_DEBUG|[18]|DEBUG|To AMount is____0
swati_sehrawatswati_sehrawat
public with sharing class OppCOntrolerAmount {
	public Decimal FromAMount{get;set;}
	public List<Opportunity> oplist {set;get;}
	public Decimal ToAMount{get;set;}
 
	public Void search(){
		if(FromAMount!=null && ToAmount!=null && FromAMount>0 && ToAmount>0){
			oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount>=:FromAMount AND Amount<=:ToAMount');
        
		}
		else If(ToAmount!= null && (FromAMount==null || FromAMount<=0)){
			oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount<=:ToAMount '); 
        }
        else If(FromAmount!= null && (ToAmount==null || ToAmount<=0)){          
			oplist = Database.query('select Id,Name,StageName,Amount from opportunity where Amount>=:FromAMount '); 
		}
	}	
}

Hope this can fix it.