0
String s = 'LAST_N_DAYS:7';
Date d = date.valueOf(s);
List<genesis__Applications__c> appList = [SELECT Id,Name,Branch_Name__c FROM genesis__Applications__c WHERE createDdate <= : d];

Not working

5
  • Thats not the correct way of doing. whats your actual requirement? Commented Aug 9, 2018 at 8:39
  • from front end i will get 'LAST_N_DAYS:30' this value and based on it i need to query with createdDate will be that value Commented Aug 9, 2018 at 8:40
  • let suppose i have a string variable s= 'LAST_N_DAYS:30' i want to query based on the value of the variable. in query if i directly use LAST_N_DAYS:30 then it is working properply. Like createdDate<LAST_N_DAYS:30 is working fine, but if i use createdDate<s where s = 'LAST_N_DAYS:30' not working Commented Aug 9, 2018 at 8:42
  • "It's not working" is not a useful statement. Even in simple cases like this, if you don't include sufficient details, you'll find yourself receiving answers that may not address your actual issue. We're not mind readers here, and we only know the information that you provide. Instead of saying "it's not working", tell us about what you expect to happen, what is currently happening, and what the difference between them is. If you're getting an error message, it's never a bad idea to include the full text of the error verbatim. You may not understand the error, but someone else might.
    – Derek F
    Commented Aug 9, 2018 at 14:09
  • Your two comments (at time of writing) are examples of things that it's more appropriate to add to your question via an edit. Comments are best thought of as temporary, and the length and formatting options are restricted in comments. A better use for comments might be to inform the person (or people) asking for clarification that you have edited your question.
    – Derek F
    Commented Aug 9, 2018 at 14:13

2 Answers 2

0

You need to use Database.query(string) to execute dynamic query.

Refer Dynamic SOQL

String s = 'LAST_N_DAYS:7';

String str = 'SELECT Id,Name,Branch_Name__c FROM genesis__Applications__c WHERE createDdate <=' + s;

List<genesis__Applications__c> appList = database.query(str);
1
  • I've downvoted this answer because it doesn't go into any detail about how OP's syntax is incorrect (which I argue is the main issue of the question).
    – Derek F
    Commented Aug 9, 2018 at 14:33
0

The "dynamic" portion of your question appears to have thrown some people off. The term that it appears you're looking for is date literal.

The syntax that you were using is incorrect, and should likely have been giving you a compile-time error when you tried to save your code.

/* Incorrect syntax example */

String s = 'LAST_N_DAYS:7';

// You should get an error at this line saying "Invalid Date"
Date d = date.valueOf(s);

Like I've hinted at, what you're trying to use here is called a "Date Literal".

There is documentation on date literals (and date formats) in the official soql/sosl reference.

The long and short of it is that date literals are used directly in your query (for both static and dynamic SOQL queries). A brief attempt also reveals that date literals cannot be stored in strings and later used in a query via variable binding

/* Also incorrect syntax */

String s = 'LAST_N_DAYS:7';

// We receive an "Invalid bind expression type of String for column of type 
//   Datetime" error if we try this
system.debug([SELECT Id FROM Account WHERE CreatedDate >= :s]);

The correct syntax uses the date literal directly in the query

/* Correct syntax */
// For a static query
system.debug([SELECT Id FROM Account WHERE CreatedDate >= LAST_N_DAYS:7]);

// For a dynamic query
String dateLiteral = 'LAST_N_DAYS:7';
String myQuery = 'SELECT Id FROM Account WHERE CreatedDate >= ' + dateLiteral;
system.debug(database.query(myQuery));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .