0

Kindly let me know how to add error messages on the below Apex method. if non >numeric values are entered in the Square Footage and Price Fields.Currently >variables >for the fields are String type.

public void  Search(){
  string searchquery= 'select        name,Beds__c,Square_Footage__c,Initial_List_Price__c,Status__c from Condo__c        where name!=null and Status__c in (\'Available\',\'Unsold\') ';


        if (beds >= '0')
           searchquery+= ' and Beds__c = '+beds+'';

        if (SFminimum > '0' && SFmaximum > '0') 
           searchquery+= ' and Square_Footage__c >= '+SFminimum+' and Square_Footage__c <= '+SFmaximum+' ';

        if (ILPminimum >'0' && ILPmaximum > '0')
           searchquery += 'and Initial_List_Price__c >= '+ILPminimum+' and Initial_List_Price__c <= '+ILPmaximum+' ';


        if (beds >= '0' && SFminimum > '0' && SFmaximum > '0')
           searchquery+= ' and Beds__c = '+beds+' and Square_Footage__c >= '+SFminimum+' and Square_Footage__c <= '+SFmaximum+' '; 

         if (beds >= '0' && ILPminimum > '0' && ILPmaximum > '0')

             searchquery += 'and Beds__c = '+beds+' and Initial_List_Price__c >= '+ILPminimum+' and Initial_List_Price__c <= '+ILPmaximum+' ';

          if(beds >= '0' && SFminimum > '0' && SFmaximum > '0' && ILPminimum > '0' && ILPmaximum > '0')

             searchquery += ' and Beds__c = '+beds+' and Square_Footage__c >= '+SFminimum+' and Square_Footage__c <= '+SFmaximum+' and Initial_List_Price__c >= '+ILPminimum+' and Initial_List_Price__c <= '+ILPmaximum+' ';

         if(beds >= '0' && SFminimum > '0')

              searchquery += ' and Beds__c = '+beds+' and Square_Footage__c  >= '+SFminimum+' ';

          if(beds >= '0' && SFmaximum > '0')

              searchquery += ' and Beds__c = '+beds+' and Square_Footage__c  <= '+SFmaximum+' ';

          if(beds >= '0' && ILPminimum > '0')

             searchquery += ' and Beds__c = '+beds+' and Initial_List_Price__c  >= '+ILPminimum+' ';

           if(beds >= '0' && ILPmaximum > '0')

             searchquery += ' and Beds__c = '+beds+' and Initial_List_Price__c  <= '+ILPmaximum+' ';

           if(SFminimum > '0')

              searchquery += ' and Square_Footage__c >= '+SFminimum+' ';

            if(SFmaximum > '0')

              searchquery += ' and Square_Footage__c  <= '+SFmaximum+' ';

             if( ILPminimum > '0')

               searchquery += ' and Initial_List_Price__c  >= '+ILPminimum+' ';

             if(ILPmaximum > '0')

             searchquery += ' and Initial_List_Price__c  <= '+ILPmaximum+' ';

 cond = Database.query(searchquery);
}

enter image description here

1 Answer 1

1

First you might need to change your code "beds >= '0'".

This will do a string comparson. Instead you might try like

Integer.valueOf(beds) > 0.

Add for showing error message on VF page, you can use <apex:pageMessages></apex:pageMessages> in your VF page and in the controller method write like;

if(Integer.valueOf(beds) <= 0) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter number greater then 0')); return null; }

Edit:

To ensure your text fields have only number entered, you can try like;


try
{
 Integer.valueOf(SFminimum);
 Integer.valueOf(SFmaximum);
 Integer.valueOf(ILPminimum);
 Integer.valueOf(ILPmaximum);
}
catch(TypeException te)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter numbers only'));
return null;
}

Put this in the beginning of search() method.

10
  • beds field is a dropdown list on vf page
    – chaitanya
    Apr 14, 2016 at 12:21
  • i want to check only numbers are entered in Square Footage and Price Fields. i have added a screen shot for reference.
    – chaitanya
    Apr 14, 2016 at 12:25
  • Still you should convert if you want an integer comparison. e.g. ('2' > '19') this will give you true where 2 > 19 is false. Apr 14, 2016 at 12:27
  • @chaitanya check the portion after "Edit". Hope it might help you. But my suggestion will be, do not do string comparison on number. Apr 14, 2016 at 12:45
  • i tried your suggestion. i have kept the method in the begining of search method. but it is displaying only the message when i try to search records.
    – chaitanya
    Apr 14, 2016 at 13:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .