I am using an SOQL in a webservice that is querying the Contact object based on the BirthDate field. The webservice request has the date in the format of 'yyyy-MM-dd',i.e.,1962-01-29 as an example.
I am not able to use this value to query the Contact Object. I tried converting this String Date into Date object. However, it always has 00:00:00 at the end. I tried the below options.
String strDate = '1962-01-29';
Date birthDate = Date.valueOf(strDate) ==> This results in 1962-01-29 00:00:00
String strDate = '1962-01-29';
Date dob = Date.valueOf(strDate)
Datetime dt = Datetime.newInstance(dob.Year(),dob.Month(),dob.day());
Date birthDate = dt.date() ==> This also results in 1962-01-29 00:00:00
How do I construct a Date object (without time) from the string date '1962-01-29'?
I did search for solutions in various forums and most of them are suggesting the above two solutions only, which does not work. I am not sure whether this behavior was changed in some Salesforce's releases.
Below is the full query that I am using.
String query = 'SELECT id,' +
'LastName,' +
'FirstName,' +
'BirthDate,' +
'MiddleName__c,' +
'MailingAddress,' +
'OtherAddress,' +
'EmploymentStatus__c,' +
'Suffix__c,'+
'Investment_Experience__c ' +
'FROM Contact WHERE';
// Fetching the details of contact based on LastName,FirstName,BirthDate
if(string.isNotEmpty(lastName)){ //Checking for last name value
if(lastName.contains('*') || lastName.contains('%')){
lastName = (lastName).replace('*','%');
query += ' LastName LIKE \''+lastName+'\' AND';
}
else{
query += ' LastName = \''+lastName+'\' AND';
}
}
if(string.isNotEmpty(firstName)){
if(firstName.contains('*') || firstName.contains('%')){
firstName = (firstName).replace('*','%');
query += ' FirstName LIKE \''+firstName+'\' AND';
}
else{
query += ' FirstName = \''+firstName+'\' AND';
}
}
if(string.isNotEmpty(dateOfBirth)){
//Datetime dt = Date.valueOf(dateOfBirth);
//Datetime dob = Date.newInstance(dt.year(), dt.month(), dt.day());
query += ' BirthDate = ' + Date.valueOf(dateOfBirth) + ' AND';
}
query += ' Account.OrgNumber__c IN (\''+(string.join(lstOrganizationNumbers,'\',\''))+'\') ';
query += ' ORDER BY lastName,firstName,BirthDate';
query += ' LIMIT 50';
system.debug('Query:: '+query);