1

I am setting a string qryDate = 'Last_N_Months:' + x - so it could be Last__Months:3.

I create a string qryString = 'SELECT field1 FROM Case WHERE CreatedDate=: qryDate' and then do: result = Database.query(qryString); I get this error: Invalid bind expression type of String for column of type Datetime

I know CreatedDate = Last_N_Months:3 works so why doesn't mine work?

3

It's a Date Literal. You need to merge it in as such. Rather than using a bind variable, you need to actually include the string so it results in your dynamic SOQL containing a Date Literal.

String soql = '... WHERE CreatedDate = LAST_N_MONTS:' + x;

Effectively, what you had would output:

SELECT ... FROM ... WHERE CreatedDate = 'LAST_N_MONTHS:42'

But what you really want (and the above provides) is:

SELECT ... FROM ... WHERE CreatedDate = LAST_N_MONTHS:42
  • BRILLIANT!!! Thanks so much - that worked.! – ellyb527 Dec 6 '19 at 16:04
  • @ellyb527 If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either. – Adrian Larson Dec 6 '19 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.