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
adi salesforceadi salesforce 

Display list of records by searching with record name or with a particular created date.

SHAHNA MULLASHAHNA MULLA
Hi,

Here is the code to display list of Account records having Name="Test". If you need to display the records based on a perticular created date, then replace 'Name' in the SOQL query with the 'Created_Date' field.

Visualforce Page :
<apex:page controller="ListOfRecords" >
     <apex:form >
         <apex:pageBlock title="List of Records">
       	 <apex:pageBlockTable value="{!Acclist}" var="i">
            <apex:column value="{!i.id}"/>
            <apex:column value="{!i.Name}"/>  
         </apex:pageBlockTable>
         </apex:pageBlock>
    </apex:form>
</apex:page>
Controller :
public class ListOfRecords {
     
    public List<Account> Acclist{get;set;}
    
    public ListOfRecords()
    {
       getlist();     
    }
    
    public List<Account> getlist()
    {
        Acclist = [select id,Name from Account where Name=:'Test'];
        return Acclist;
    }
}

If you are trying to dynamicaly display the records, then you can use following query in your controller,
String queryStr='select id, name from Account ' +
                ' where name=:'givenName';
List<Account> accs=Database.query(queryStr);
'givenName' indicates the name given by the user.

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Adi,

Please try the below code I think this will help you:


VF Page:
 
<apex:page controller="DisplayAccountBasedOnNameorDateClass">
    <apex:form> 
      <apex:pageBlock>
    Name:<apex:inputText value="{!Name}" />
    Created Date:<apex:inputText value="{!CD}" onfocus="DatePicker.pickDate(false, this , false);"/>
    <apex:commandButton value="Search" action="{!SearchMethod}" reRender="table" title="Search"/>
  
        <apex:pageBlockTable id="table" value="{!AccountList}" var="acc">
            <apex:column value="{!acc.name}"/>
        
        </apex:pageBlockTable>
    
    </apex:pageBlock>
        </apex:form>
    </apex:page>

Class:
 
public class DisplayAccountBasedOnNameorDateClass {
    public String Name{get;set;}
    public Date CD{get;set;}
    public List<Account> accountList {get;set;}
    public DisplayAccountBasedOnNameorDateClass()
    {
		accountList=new List<Account>();        
    }
    public void SearchMethod()
    {
        AccountList = [SELECT id,name,CreatedDate from Account where name like: Name or CreatedDate =: CD];
  
    }

}
Thanks
Bhargavi.
Hope this will help.
 
Meenu MathewMeenu Mathew
Hi adi,

Use the below code if you want to search records based on date.
 
DateTime startDT=DateTime.newInstance(2011, 7, 13, 0, 0, 0);
DateTime endDT=DateTime.newInstance(2011, 7, 14, 0, 0, 0);
        
String queryStr='select id, name from Account ' +
                        ' where CreatedDate>=:startDT ' + 
                        ' and   CreatedDate<=:endDT';
        
List<Account> accs=Database.query(queryStr);

Thanks
 
adi salesforceadi salesforce
Hi,
Thank you for responding. Bhargavi your code is working for searching by name but I want to get records by created date also. created date should be like calender date should come.either by entering name or created date or if both entered the corresponding records have to come.
could you look into that part.