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
Hari nadh babu Eluru 7Hari nadh babu Eluru 7 

Records between Min and Max of age field

If user enter min age, max age . fetch all the students in between that min and max age

  • in above example try to load students in ascending order based on age

 
Best Answer chosen by Hari nadh babu Eluru 7
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

Please find the below VF page and Apex class for that. I have used the Contact object just replace the object name and fields what ever you need.

VF page:
<apex:page Controller="SearchInVFController">
    <apex:form>
        <apex:inputText value="{!minvalue}" label="minvalue"/>
        <apex:inputText value="{!maxvalue}" label="maxvalue"/>
        <apex:commandButton value="Search records" action="{!search}"/>
        <apex:commandButton value="Clear records" action="{!clear}"/>
        <apex:pageBlock title="Search Result">
            <apex:pageBlockTable value="{!acc}" var="a">
                <apex:column value="{!a.name}"/>
                <apex:column value="{!a.id}"/>
                <apex:column value="{!a.Age__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class
 
public class SearchInVFController {
    public list <Contact> acc {get;set;}
    public Decimal minvalue {get;set;}
    public Decimal maxvalue {get;set;}
    public SearchInVFController( ) {
    }
    public void search(){
        string searchquery='select Name,id,Age__c  from Contact where Age__c > :minvalue and Age__c < :maxvalue ORDER  BY Age__c ASC';
        acc= Database.query(searchquery);
    }
    public void clear(){
        acc.clear();
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari Nadh,

You are looking for search functionality  ? . If so please help the community what sort of code you are expecting. Is it using Visualforce page, Aura or LWC?

Thanks,
 
Hari nadh babu Eluru 7Hari nadh babu Eluru 7
Visualforce page sir
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hari,

Please find the below VF page and Apex class for that. I have used the Contact object just replace the object name and fields what ever you need.

VF page:
<apex:page Controller="SearchInVFController">
    <apex:form>
        <apex:inputText value="{!minvalue}" label="minvalue"/>
        <apex:inputText value="{!maxvalue}" label="maxvalue"/>
        <apex:commandButton value="Search records" action="{!search}"/>
        <apex:commandButton value="Clear records" action="{!clear}"/>
        <apex:pageBlock title="Search Result">
            <apex:pageBlockTable value="{!acc}" var="a">
                <apex:column value="{!a.name}"/>
                <apex:column value="{!a.id}"/>
                <apex:column value="{!a.Age__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class
 
public class SearchInVFController {
    public list <Contact> acc {get;set;}
    public Decimal minvalue {get;set;}
    public Decimal maxvalue {get;set;}
    public SearchInVFController( ) {
    }
    public void search(){
        string searchquery='select Name,id,Age__c  from Contact where Age__c > :minvalue and Age__c < :maxvalue ORDER  BY Age__c ASC';
        acc= Database.query(searchquery);
    }
    public void clear(){
        acc.clear();
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi Hari,
Here is the Solution to get the record between Min and Max age.

VisualForce Page:
<apex:page Controller="StudentMinMaxAge" >
    <apex:form > <apex:pageBlock title="Student Age">
        <apex:pageBlockSection columns="1"> 
            Min Age<apex:inputText value="{!minage}"/> 
            Max Age<apex:inputText value="{!maxage}"/> 
        </apex:pageBlockSection> <apex:pageBlockButtons > 
        <apex:commandButton action="{!search}" value="Search"/> 
        </apex:pageBlockButtons> 
        <apex:pageBlockTable value="{!studentObj}" var="c"> 
            <apex:column value="{!c.Name}"/> 
            <apex:column value="{!c.minAge__c}"/> 
            <apex:column value="{!c.maxAge__c}"/> 
        </apex:pageBlockTable> 
        </apex:pageBlock> 
    </apex:form> 
</apex:page>  

Apex Class:

public class StudentMinMaxAge {
    public integer maxage{get;set;}
    public integer minage{get;set;}
    public List <Student__c> studentObj {get;set;}
    public void search(){
        studentObj = new list<Student__c>();
        studentObj=[select Name, minAge__c,maxAge__c from Student__c where minAge__c>=:minage AND maxAge__c<=:maxage ORDER  BY minAge__c ASC];// you can sort the data in ascending order by minAge__c or maxAge__c. 
        system.debug('studentObj'+studentObj);
    } 
}
Hope you find your solution.
Thanks and Regards.
 
nijat ali khannijat ali khan
The ultrawide gaming monitors are all the rage these days and with good reason. They offer a cinematic, immersive gaming experience and the best ones offer a lot of advantages over their more traditional counterparts. We have listed our favorite gaming monitors and talked about what makes them good and also provided some tips on how to get the most out of your ultrawide monitor.
https://checkthenbuy.com
https://www.10bestkeyboards.com