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
dai tran 6dai tran 6 

How can auto delete record after 3 minutes?

I have a Custom object
I want after insert record 3 minutes, if Status=0 , This record will auto delete.
How can auto delete record after 3 minutes?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Dai,

I trust you are doing very well.

You can achieve your requirement using Process Builder and Invocable Apex Class. As you can't schedule a process builder in minutes, there is a workaround to 'Set Time for Actions to Execute' in minutes.

First, you have to create a formula field on sObject (in my case: Account). 
Object : Account
Custom Formula Field (Date/Time) : Add3__c (Formula: CreatedDate+63/1440) 

* From this formula you will get time 60mins + 3mins after the record created date. We will use this time in a process builder, to execute a process builder before 1 hour of this time.*

Now, create flow in process builder:
Object : Account
Start the process : only when a record is created

Criteria Name : No Criteria
Criteria for Executing Actions : No criteria—just execute the actions!

Schedule Actions : 
Set Time for Actions to
Execute : 1 Hours Before Add3__c  *(Suppose, record created time is 6:30 pm, so according to formula Add3__c will be 7:33 pm. So, process builder will execute on 6:33 pm i.e; 1 hour before 7:33 pm)*
Add Action : Call Apex
Action Name : Delete Account
Apex Class : TimeDeleteInvocable
Set Apex Variables : 
Field : accIds
Type : Field Reference
Value : [Account].Id

Process Builder Screenshot :
User-added image
1.
User-added image
2.
User-added image
3.
User-added image
4.
User-added image


Apex Class:
public class TimeDeleteInvocable {
    
    @InvocableMethod
    public static void accountDelete(List<Id> accIds)
    {
        List<Account> account =[SELECT Id FROM Account 
                                WHERE Id IN :accIds 
                                AND Rating = 'Warm'
                               ];
        DELETE account;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas