function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rabbani sayyed 8Rabbani sayyed 8 

How do I write a code that deletes the right records if the record is idle for more than 180 days. Can anyone share me the code for this synario ??

How do I write a code that deletes the right records if the record is idle for more than 180 days. Can anyone share me the code for this synario ??
SYED MOOSA NAZIR T NSYED MOOSA NAZIR T N

Hi,

Please refer the below sample code. Save this class, and Schedule this class to RUN DAILY.
To Schedule, Click setup --> Apex Class --> Click the Button "Schedule Apex" and choose this class name and provide the other required details.

global class SchClass_DeleteUnUsedRecords implements Schedulable {
    global void execute(SchedulableContext ctx) {
        Date DateCriteria = Date.Today().addDays(-180);
        List<Account> ListofAccounts = new List<Account> ();
        ListofAccounts = [SELECT id, lastModifiedDate FROM Account WHERE lastModifiedDate <: DateCriteria];
        if(!ListofAccounts.isEmpty()){
            system.debug('ListofAccounts'+ListofAccounts);
            Delete ListofAccounts;
        }
    }   
}

 

Hope this addressed your Query.
And one more suggestion is, Please dont misuse this community by just asking the Code..

Thanks
Syed Moosa Nazir TN
smartmoosa@gmail.com

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Rabanni, 

I would suggest you to write a batch and shedule that batch to run eveyday to avoid any issues - 
You can go through this link, it may be useful to you -
 https://developer.salesforce.com/forums/?id=906F00000009357IAA