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();
}
}
I want to create a dynamic search field of created date
String created_date = myVariable.CreatedDate;
List<Account> acclist = Database.query('SELECT Id,Name,CreatedDate FROM Account WHERE CreatedDate = ' + created_date);
Could you write apex code and vf page for it.
Firstly create a field of formula type that contains value of date format ,since createddate is datetime type.
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:
Please let me know if that helps you. Thanks.
why should we create custom field of created date.
vf page:
controller:
Please let me know if that helps you. Thanks.
If you are looking for Search Page Using Visualforce.
Controller:
Visualforce Page:
Please mark as best answer if it helps you.
Thank You
Ajay Dubedi