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
hemant ranahemant rana 

Soql query with today date in where clause

hi when iam writing simple query---
{select Next_Contact_Date__c , Contact_Name__r.name from engagement__c where  Campaign__r.id=:cId AND Next_Contact_Date__c>=:system.today() ORDER BY Next_Contact_Date__c ASC}

It's working fine but due to some reasons i had to make it dynamic but after making it dynamic it's giving error i think may be of system.today()---

queryString = 'select Next_Contact_Date__c , Contact_Name__r.name from engagement__c where  Campaign__r.id='+'\''+cId+'\'' + ' AND Next_Contact_Date__c>=:' + system.today() + ' ORDER BY Next_Contact_Date__c ASC';

please help i tried every thing from converting string to date format any everything
Best Answer chosen by hemant rana
AshlekhAshlekh
Hi

Try below code
String s = String.valueOf(systemtoday()); 
queryString = 'select Next_Contact_Date__c , Contact_Name__r.name from engagement__c where  Campaign__r.id='+'\''+cId+'\'' + ' AND Next_Contact_Date__c>=' + s + ' ORDER BY Next_Contact_Date__c ASC';


IF it helps you than please mark it as a solution and ENJOY APEX

All Answers

James LoghryJames Loghry
SOQL provides a set number of functions for working with dates.  See the following document: http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_date_functions.htm

I
n particular, take a look at using Next_Contact_Date__c >= TODAY
AshlekhAshlekh
Hi

Try below code
String s = String.valueOf(systemtoday()); 
queryString = 'select Next_Contact_Date__c , Contact_Name__r.name from engagement__c where  Campaign__r.id='+'\''+cId+'\'' + ' AND Next_Contact_Date__c>=' + s + ' ORDER BY Next_Contact_Date__c ASC';


IF it helps you than please mark it as a solution and ENJOY APEX
This was selected as the best answer
Deepak Kumar ShyoranDeepak Kumar Shyoran
Use below code as Salesforce automatically converts variable value to appropriated field in Dynamic query and you don’t need to worry about that …also need not to put single quotes i.e ‘’  before Id or name field value.

Date todayDate = Date.today() ;
String queryString = 'select Next_Contact_Date__c , Contact_Name__r.name from engagement__c where  Campaign__r.id=:cId AND Next_Contact_Date__c>=: todayDate ORDER BY Next_Contact_Date__c ASC';

Please mark it as best solution to your problem if it does solve your problem.
James LoghryJames Loghry
It's too late now, but System.today() is not the Best Answer.  The best answer is to use the SOQL date functions like I pointed out.  With System.today() you're forced into using either another variable or Dynamic SOQL.