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 

How to get list of account records by searching created date in the searchfield

Shruti SShruti S
You should be writing a query like this - 
List<Account> accounts = [
    SELECT  Name
    FROM    Account
    WHERE   CreatedDate >= 2018-08-10T00:00:00Z AND
            CreatedDate <= 2018-08-10T23:59:59Z
];
adi salesforceadi salesforce
Hi,
I want  to create a dynamic search field of created date
PINKY REGHUPINKY REGHU
Hi,

String  created_date = myVariable.CreatedDate;
List<Account> acclist = Database.query('SELECT Id,Name,CreatedDate FROM Account WHERE CreatedDate = ' + created_date);
adi salesforceadi salesforce
Hi,
Could you write apex code and vf page for it.
 
PINKY REGHUPINKY REGHU
Hi, 
     Firstly create a field  of formula type that contains value of date format ,since createddate is datetime type.
User-added image

Try this code:
controller:
public with sharing class accountsearch{  
   public list <account> acc {get;set;}  
   public  String searchstring {get;set;}
   public accountsearch(ApexPages.StandardController controller) {  
   }  
   public void search(){  
       System.debug(searchstring);
     string searchquery='select name,id,created_date__c from account where created_date__c ='+ searchString;  
     acc= Database.query(searchquery);  
       System.debug(acc);
   }  
   public void clear(){  
   acc.clear();  
   }  
 }
vf page:
<apex:page standardController="account" extensions="accountsearch">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input">   
  <apex:commandButton value="Search records" action="{!search}" />  
     </apex:inputText>
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!acc}" var="a"> 
     <apex:column value="{!a.name}"/>  
       </apex:pageblockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>

Check this screenshot also:
vf page

Please let me know if that helps you. Thanks.
adi salesforceadi salesforce
Hi,
why should we create custom field of created date.
 
PINKY REGHUPINKY REGHU
Hi,
vf page:
<apex:page standardController="account" extensions="accountsearch">  
  <apex:form > 
      <apex:selectList size="1" value="{!cdate}" required="false" > 
 				<apex:selectOptions value="{!AccountNames1}" /> 

			 </apex:selectList>
                  <apex:commandButton value="Search records" action="{!search}" />  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!acc}" var="a"> 
     <apex:column value="{!a.name}"/>  
       </apex:pageblockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>

controller:
public with sharing class accountsearch{  
   public list <account> acc {get;set;}  
  public  String cdate {get;set;}
     public List<selectOption> accountlist{get;set;}
   public accountsearch(ApexPages.StandardController controller) {  
   }  
     public List<selectOption> getAccountNames1() {
        accountlist = new List<selectOption>();
        accountlist.add(new selectOption('--none--','--none--'));
		for(Account accs:[select id,name,createddate from Account]) 
		{  
    			String acccreateddate = String.valueOf(accs.createddate);
				accountlist.add(new selectOption(acccreateddate,acccreateddate));
		}
		return accountlist;
     }
   public PageReference search(){  
      System.debug(cdate);
       DateTime convertedDate = DateTime.valueOf(cdate);
    string searchquery='select name from account where createddate ='+convertedDate.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
 
     acc= Database.query(searchquery);  
       System.debug(acc);
       return null;
   }  
   public void clear(){  
   acc.clear();  
   } 
 }

Please let me know if that helps you. Thanks.
Ajay K DubediAjay K Dubedi
Hi Adi,

If you are looking for Search Page Using Visualforce.

Controller:
 
public with sharing class accsearchcontroller {  
public static Date startDate{get;set;}
public static Date endDate{get;set;}
public static List<Account> accList{get;set;}
public boolean showResult{get;set;}
public boolean blnNoResultFound{get;set;}

public accsearchcontroller(){
    showResult = false;
    blnNoResultFound = false;
    accList = new List<Account>();
}
public PageReference searchAccount(){
    if(startDate != NULL && endDate != NULL){
        accList = [SELECT Id,Name FROM Account WHERE createdDate >= : startDate AND createdDate <: endDate];
    } 
    if(accList.size() > 0){
         showResult = true;
         blnNoResultFound = false;
    }
    else{
         blnNoResultFound = true;
         showResult = false;
    }
    return null;        
      }
   }


Visualforce Page:
 
<apex:page controller="accsearchcontroller" docType="html-5.0">
<center><h1>Account Search Page</h1></center>
<apex:form>
   Start Date: <apex:input type="date" value="{!startDate}" required="true" />
   End Date: <apex:input type="date" value="{!endDate}" required="true" />
   <apex:commandButton action="{!searchAccount}" value="Search" reRender="dataWrapperPanel" lang="End Date"/>

   <apex:outputPanel id="dataWrapperPanel">
      <apex:pageBlock title="Account List" id="dataBlock" rendered="{! showResult }">
          <apex:pageBlockSection >
              <apex:pageBlockTable value="{!accList}" var="acc" >
                  <apex:column value="{!acc.Name}"/>
              </apex:pageBlockTable>
          </apex:pageBlockSection> 
      </apex:pageBlock>
      <apex:outputLabel id="noRecFound" rendered="{! blnNoResultFound }">No Record Found !!!</apex:outputLabel>
   </apex:outputPanel>
 </apex:form>
 </apex:page>

Please mark as best answer if it helps you.

Thank You
Ajay Dubedi