You need to sign in to do that
Don't have an account?
Michael M
Query to obtain all files related to Leads where the created date was last 7 days
Hi, my task is to schedule apex to delete all files related to Lead records after 7 days from the file's createddate.
How would I write that query/ DML?
How would I write that query/ DML?
ContentDocument.CreatedDate > Last_N_Days:7, I guess this will not return any records. Indirectly its seacrhing for future created date records.
Assume this is executing today which 2/18/20 the LAST_N_Days:7.
Lets say we have record whose createddate is 2/14/20 which is within last 7 days. Its checks if the date is greater than last 7 days (But the date is in range of last 7 days). Answer is no therefore this record is not fetched.
Consider other record which has closeddate 2/20/2020(future date) this is fetched because the day is greater than last 7 days. (In real time there is no record with future createddate).
Consider 2/1/20 which is far less than past 7 days so this is not returned.
However, if your requirement is to delete files which are older than 7 days (which is delete after 7 days of creation date).
Try using below query: Thanks
All Answers
Please try with below query. The query will give you all the fiels related to leads which are created with in last 7 days. Or to query all the files which are created one week ago you can use below: (which is 7 days from the file's createddate).
Hope this helps! Please mark as best if it does.
Thanks
ContentDocument.CreatedDate > Last_N_Days:7, I guess this will not return any records. Indirectly its seacrhing for future created date records.
Assume this is executing today which 2/18/20 the LAST_N_Days:7.
Lets say we have record whose createddate is 2/14/20 which is within last 7 days. Its checks if the date is greater than last 7 days (But the date is in range of last 7 days). Answer is no therefore this record is not fetched.
Consider other record which has closeddate 2/20/2020(future date) this is fetched because the day is greater than last 7 days. (In real time there is no record with future createddate).
Consider 2/1/20 which is far less than past 7 days so this is not returned.
However, if your requirement is to delete files which are older than 7 days (which is delete after 7 days of creation date).
Try using below query: Thanks
SELECT Id, Linkedentity.name, Linkedentity.id,ContentDocument.CreatedDate
FROM contentDocumentLink
WHERE Linkedentityid
IN (SELECT Id FROM lead)
AND ContentDocument.CreatedDate < Last_N_Days:7
Why isn't it working?
No the query will not fetch the records which is not older than 7 days. I have tested what you said uploading a file and queried with same but did not get any results and worked as expected. I am not sure what's going on in your case (Might be other files attached to the record)
Please make sure you are looking for the correct file. Also cross check the file's ContentDocument.CreatedDate field in the result record which should not be today's.
Thanks