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
puli rajupuli raju 

query search records by name and date range

Mahesh DMahesh D
Hi Puli,

Date Formats and Date Literals:

In a SOQL query you can specify either a particular date or a date literal. A date literal is a fixed expression that represents a relative range of time, such as last month, this week, or next year.

dateTime field values are stored as Coordinated Universal Time (UTC). When a dateTime value is returned in Salesforce, it’s adjusted for the time zone specified in your org preferences. SOQL queries, however, return dateTime field values as UTC values. If you want to process these values in different time zones, your application might need to handle the conversion.


Some of the sample SOQL:
 
SELECT Id FROM Account WHERE Name = 'TestName' AND CreatedDate > 2005-10-08T01:02:03Z

 
Select CreatedDate, Id, LastModifiedDate from Opportunity where Name = 'TestName' AND CreatedDate > 2011-01-01T00:00:00Z AND CreatedDate < 2011-12-31T00:00:00Z
 
Date myDate = date.newinstance(2011, 1, 1);
Date myDate2 = date.newinstance(2011, 12, 31);
 
List<Opportunity> oppLst = [Select CreatedDate, Id, LastModifiedDate from Opportunity where CreatedDate >: myDate AND CreatedDate <:  myDate2 AND Name = 'TestName' order by LastModifiedDate];

Also Please go through the below links:

https://nextgensalesforce.wordpress.com/2015/11/30/dynamic-soql-query-results-in-local-time/

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm


Please do let me know if it helps you.

Regards,
Mahesh
 
DeepthiDeepthi (Salesforce Developers) 
Hi Puli,

Please check the below sample code that searches for records based on the name and date ranges in a Visualforce page. 
<!-- VISUALFORCE PAGE-->
<apex:page controller="Controller" docType="html-5.0">
<apex:form >
    <apex:pageBlock title="Search for a record">
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel >First Name</apex:outputLabel>
                <apex:inputText value="{!name}"/>    
            </apex:pageBlockSectionItem><br/>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Start Date</apex:outputLabel>  
                <apex:inputText value="{!sd}"/>  
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >End Date</apex:outputLabel>  
                <apex:inputText value="{!ed}"/>  
            </apex:pageBlockSectionItem><br/>
            <apex:pageBlockSectionItem >
                <apex:commandButton value="Fetch" action="{!fetchValues}"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock >
        <apex:pageBlockTable var="cnd" value="{!C}">
            <apex:column headerValue="Candidate Number">
                <apex:outputText value="{!cnd.Name}"></apex:outputText>
            </apex:column>
            <apex:column headerValue="First Name">
                <apex:outputText value="{!cnd.First_Name__c}"></apex:outputText> 
            </apex:column>
            <apex:column headerValue="Last Name">
                <apex:outputText value="{!cnd.Last_Name__c}"></apex:outputText>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>
 
// Controller Class
public class Controller {

    public Date ed { get; set; }
    public Date sd { get; set; }
    public String name { get; set; }
    public List<Candidate__c> C {get;set;} 
    public Controller(){      
        C= new List<Candidate__c>(); 
    }
    public PageReference fetchValues() {
           
         C=[select Name, First_Name__c, Last_Name__c from Candidate__c where (First_Name__c like:name) AND (First_Name__c like:name+'%') AND (First_Name__c like:'%'+name+'%')AND (Start_Date__c>=:sd) AND (End_Date__c<=:ed)];
        return null;
    }
}

Hope this helps you!
Best Regards,
Deepthi
JyothsnaJyothsna (Salesforce Developers) 
Hi Raju,

Please check the below sample code.

VisualForce Page:
<apex:page controller="DateRangeCont">
 <apex:form id="dt1">
 <apex:pageBlock >
 <apex:pageBlockSection >
 <apex:pageBlockSectionItem >
 <apex:outputLabel >Start Date</apex:outputLabel>
 <apex:inputfield value="{!a.From_Date__c}"/>
</apex:pageBlockSectionItem>
 <apex:pageBlockSectionItem >
 <apex:outputLabel >End Date</apex:outputLabel>
 <apex:inputField value="{!a.To_Date__c}"/>
 </apex:pageBlockSectionItem>
  
<apex:commandButton value="Go" action="{!go}" reRender="dt"/>
 
</apex:pageBlockSection>
 <apex:pageBlockTable value="{!customer1}" var="ct" id="dt">
<apex:column headerValue="Customer Name">
<apex:inputField value="{!ct.Name}"/>
</apex:column>
<apex:column headerValue="billing city">
  <apex:inputField value="{!ct.Billing_City__c}"/>
  </apex:column>
  <apex:column value="{!ct.createddate}"/> 
  <apex:column headerValue="billing postal code">
  <apex:inputField value="{!ct.Billing_Postal_Code__c}"/>
  </apex:column>
</apex:pageBlockTable>
  
</apex:pageBlock>
 </apex:form>
</apex:page>

Controller:
 
public with sharing class DateRangeCont {

    public list<customer__c> customer1 { get; set; }
    
    public datetime startdate1;
    public datetime enddate1;
   public customer__c a { get; set; } 
    public DateRangeCont(){
     customer1=new list< customer__c>();
    a=new customer__c();
     
    }
    public PageReference go() {
    startdate1=a.From_Date__c;
    enddate1=a.To_Date__c;
    customer1=[select name,Billing_City__c ,Billing_Postal_Code__c,Createddate from customer__c where Createddate>=:startdate1 AND Createddate<=:enddate1];
    return null;
    }


    
}

Screenshot:

User-added image



Hope this helps you!
Best Regards,
Jyothsna