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
Saravana RavikumarSaravana Ravikumar 

Why there is no undelete for before trigger ?

AkhilMandiaAkhilMandia
User-added image
SwethaSwetha (Salesforce Developers) 
Hi Ravi,

There is no "before-undelete" event in Salesforce triggers because the undelete operation creates a new Id for the restored record. Allowing the "before" event means that changes can be made to the record before it is restored, which could result in unpredictable behavior. Therefore, Salesforce only provides an "after-undelete" event that fires after the record has been restored from the Recycle Bin

It's important to note that the "after-undelete" trigger event only works with recovered records, which are records that were deleted and then recovered from the Recycle Bin. If a record is deleted permanently, there is no way to restore it, and no trigger event will fire.

But After Undelete you will be having a record to update something with that record.

Related: https://salesforce.stackexchange.com/questions/148273/why-there-is-no-before-undelete-event-in-salesforce-triggers

https://www.salesforcecodecrack.com/2019/11/undelete-trigger-event-in-apex-trigger.html?m=1

If this information helps, please mark the answer as best. Thank you
Arun Kumar 1141Arun Kumar 1141

Hi Saravana,

In Salesforce, the before trigger context is executed before the records are saved to the database. The purpose of a before trigger is to perform validations, modify record values, or make related updates before the data is committed to the database.

The reason there is no undelete functionality available in a before trigger is that the before trigger operates on records that have not yet been persisted in the database. When a record is deleted, it is physically removed from the database, and its data is no longer available.

Since the before trigger executes before the record is saved, there is no way to directly undelete the record because it does not exist in the database at that point. The before trigger cannot access deleted records or restore them because it only operates on records that are in memory and have not been persisted.

If you need to handle the undeletion of records or perform additional logic related to deleted records, you can use an after trigger. The after trigger context is executed after the records have been saved to the database, and it can access the deleted records through the Trigger.old context variable. From the after trigger, you can perform actions such as restoring the deleted records or creating related records based on the deleted data.

To summarize, the absence of undelete functionality in a before trigger is due to the fact that the trigger operates on records that have not yet been saved to the database. If you need to handle deleted records, you can utilize an after trigger to access the deleted records through the Trigger.old context variable.

Hope this will be helpful.
Thanks!