I wrote the code for the condition: delete data where LastModifiedDate is older than 30days from today.
Code:
data= [SELECT Id, Name, Type, Status, LastModifiedDate FROM Account ];
for(Account account : data)
{
Integer daysCount = (system.today()).daysBetween(Date.valueOf(account.LastModifiedDate));
if(daysCount>30){
//Purge data
delete account ;
}
}
But should write above logic in the SOQL query itself to get data which is older than 30days from now to delete. I tried with the below SOQL but it gives all records even latest records too.
data = [SELECT Id, LastModifiedDate FROM Accont where LastModifiedDate = LAST_N_DAYS: 30];
WHERE LastModifiedDate > LAST_N_DAYS:30
. What you have there,WHERE LastModifiedDate = LAST_N_DAYS:30
shouldn't get all records, but only those modified within the last 30 days.>
should be<
, by the way.