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?

2
  • 1
    It's not particularly clear what about the filter in your question does not work for you.
    – Adrian Larson
    Jul 31, 2020 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, 2020 at 14:58

1 Answer 1

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".

3
  • 1
    Haha I was about to post the same:P +1 Jul 31, 2020 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, 2020 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, 2020 at 16:47

You must log in to answer this question.

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