+ Start a Discussion
VamsiVamsi 

how to use custom labels with datetime fields

Hi,

I have a soql query where I am using custom labels to specify the date range in string example : THIS_WEEK or something else in future and binding it to the lastmodified date time. when I bind it throws me the below error any thoughts would be appreciated.
In apex

soql query :
  Select status ,CaseNumber,Priority from case where LastModifiedDate =:Label.Thisweek (Thisweek contains a value : THIS_WEEK)

error : Invalid bind expression type of String for column of type Datetime 
 
Best Answer chosen by Vamsi
Rajnish Bishnoi 13Rajnish Bishnoi 13
Hi Vamshi,

Please find below details:-

String str='THIS_WEEK';
String q='Select status ,CaseNumber,Priority,Response_Due_Date__c,Response_time_elapsed__c from case where RecordTypeID = \'012300000067hjh\' AND (Status = \'Unclaimed\' OR Status =\'Escalated\' OR Status =\'Rep On\') AND Product_Type__c = \'Test\' AND OwnerID = \'00G30000003hjg\' AND LastModifiedDate = '+str +' LIMIT 50000';

List<case>= Database.getQueryLocator(q);

Thanks,
Rajnish

All Answers

Rajnish Bishnoi 13Rajnish Bishnoi 13
Put label in a String and then convert it into Datatime object and then pass it into query.
use below Datetime class for your reference.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm
SandhyaSandhya (Salesforce Developers) 
Hi Vamshi Krishna,

You need to typecast to convert the string to DateTime.
 
String stringDate = '2013-10-05 20:03:20';

Datetime myDate = datetime.valueOf(stringDate);

Thanks and Regards
sandhya


 
VamsiVamsi
Thank you for the quick reponse but how to covert my string "THIS_WEEK" to datetime instance in apex and bind that to lastmodifieddate in soql 
Rajnish Bishnoi 13Rajnish Bishnoi 13
It is not possible directly in SOQL, you can follow the below approach.
Pass your string value in String query and then put it into get query method. It will return you the list.
string query='create your query string here';
        List<>= Database.getQueryLocator(query);


Hope this will help you.
Contact me on bishnoi.rajnish@gmail.com for further assistance.
Thanks
Rajnish Bishnoi 13Rajnish Bishnoi 13
Hi Vamshi,

Please find below details:-

String str='THIS_WEEK';
String q='Select status ,CaseNumber,Priority,Response_Due_Date__c,Response_time_elapsed__c from case where RecordTypeID = \'012300000067hjh\' AND (Status = \'Unclaimed\' OR Status =\'Escalated\' OR Status =\'Rep On\') AND Product_Type__c = \'Test\' AND OwnerID = \'00G30000003hjg\' AND LastModifiedDate = '+str +' LIMIT 50000';

List<case>= Database.getQueryLocator(q);

Thanks,
Rajnish
This was selected as the best answer
VamsiVamsi
Hi Rajnish,

Thank you, The above query is working ..