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
Don Schueler 6Don Schueler 6 

Using Dynamic Date Literals in SOQL for Loop

I have a for loop over a SOQL select using a date field with a date literal e.g. LAST_MONTH

I would like to make this more generic by providing the ability to pass in the datevalue dynamically as a variable into the SOQL Quert e.g. :monthFilter

Is there a good way to do this?
Nagendra ChinchinadaNagendra Chinchinada
Hi Don,
Try this method by using Database.query() method,
 
string monthFilter ; // Assign value to this variable to pass to SOQL

// Form your query as a string, u can use concatinations also here
string dynamicQuery = 'SELECT Id,Name FROM CONTACT WHERE CREATEDDATE = :monthFilter ';

List<contact> results  = Database.query(dynamicQuery );// Execute dynamically formed query

Let me know if it works.

Thanks,
Nagendra 

 
Don Schueler 6Don Schueler 6
I am already dozing this..the issue is the for loop
Amit Chaudhary 8Amit Chaudhary 8
Please post your code so that we can help you