1

I want to query all the contact records that have not been modified for the past three years.

SELECT Id FROM Contact WHERE LastModifiedDate < LAST_N_YEARS:3

But as LAST_N_YEARS starts from previous year and goes back to n previous years. I want from current date to previous n years.

How can I do it?

  • 1
    It's not particularly clear what about the filter in your question does not work for you. – Adrian Larson Jul 31 at 14:47
  • I simple want to delete contact records that have not been modified for the last 3 years. Is the query written correct? For example if a record's (contact1) last modified date is today, 3 years from now I want it to be deleted. But LAST_N_YEARS starts from Jan 1 of previous years and goes back 3 years. So 3 years from now I use this query, I am couting from Jan. 1, 2022 - 3, therefore I end up Dec. 31, 2019 and therefore I cannot delete contact1. – nSv23 Jul 31 at 14:58
2

Your query is generally correct. Contacts will be between 3 years and 3 years and 364 (or 365 on leap years) days old assuming you run this query annually. If you want to be more precise, consider LAST_N_DAYS instead:

LastModifiedDate < LAST_N_DAYS:1095

This will generally find contacts that haven't been modified in the more usual sence of "last three years".

| improve this answer | |
  • 1
    Haha I was about to post the same:P +1 – rahul gawale Jul 31 at 14:57
  • @sfdcfox just out of curiosity am I right in thinking: LAST_N_YEARS starts from Jan 1 of previous years and goes back 3 years. So 3 years from now, I am counting from Jan. 1, 2022 - 3, therefore I end up Dec. 31, 2019? – nSv23 Jul 31 at 15:11
  • 1
    @nSv23 That's correct. To be clear, it is the midnight between Dec 31, 2019 and Jan 1, 2020 (the New Year). – sfdcfox Jul 31 at 16:47

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.