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
Nithin NeelaNithin Neela 

Why SalesForce does not have Before Undelete in Trigger Operations???

Hi

 Iam so Much Confused With Delete and undelete trigger operations .Can any one tell the relationship between them .Also why dont salesforce BEFORE UNDELETE operation ...
Best Answer chosen by Nithin Neela
JyothsnaJyothsna (Salesforce Developers) 
Hi,

First, you need to understand the difference between "Before " trigger and "After" trigger.
 
Before trigger:
  • BEFORE triggers are usually used when validation needs to take place before accepting the change. They run before any change is made to the database. Let's say you run a database for a bank. You have a table accounts and a table transactions. If a user makes a withdrawal from his account, you would want to make sure that the user has enough credits in his account for his withdrawal. The BEFORE trigger will allow to do that and prevent the row from being inserted in transactions if the balance in accounts is not enough.
 
After Trigger:
  • AFTER triggers are usually used when information needs to be updated in a separate table due to change. They run after changes have been made to the database (not necessarily committed). Let's go back to our back example. After a successful transaction, you would want the balance to be updated in the accounts table. An AFTER trigger will allow you to do exactly that.
 

Is Delete:
 If this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.

Is Undelete:
If this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)


Conclusion:

An Update or Insert can have a Before functionality as the record exists.
Whereas there is no Record before deleting the Actual record. 
Hence only after deleting we can have the Undelete event, whereas every record merely exists before deleting.


Best Regards,
Jyothsna


All Answers

JyothsnaJyothsna (Salesforce Developers) 
Hi,

First, you need to understand the difference between "Before " trigger and "After" trigger.
 
Before trigger:
  • BEFORE triggers are usually used when validation needs to take place before accepting the change. They run before any change is made to the database. Let's say you run a database for a bank. You have a table accounts and a table transactions. If a user makes a withdrawal from his account, you would want to make sure that the user has enough credits in his account for his withdrawal. The BEFORE trigger will allow to do that and prevent the row from being inserted in transactions if the balance in accounts is not enough.
 
After Trigger:
  • AFTER triggers are usually used when information needs to be updated in a separate table due to change. They run after changes have been made to the database (not necessarily committed). Let's go back to our back example. After a successful transaction, you would want the balance to be updated in the accounts table. An AFTER trigger will allow you to do exactly that.
 

Is Delete:
 If this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.

Is Undelete:
If this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)


Conclusion:

An Update or Insert can have a Before functionality as the record exists.
Whereas there is no Record before deleting the Actual record. 
Hence only after deleting we can have the Undelete event, whereas every record merely exists before deleting.


Best Regards,
Jyothsna


This was selected as the best answer
VineetKumarVineetKumar
Delete : When you try to delete a record from salesforce
Undelete : When you try to resotre the record from the recycle bin, back to salesforce

In salesforce there is no BEFORE UNDELETE.
Usually the before and after trigger, have the different context of a record in before and after.
BEFORE triggers are used for validation check before actually commiting the record. They run before any change is made to the database.

In Undelete operation, the record is in recycle bin, not in saleforce and validation is on salesforce. So, no point in having a before context for undelete.

Let me know if that explanation helped.
Nithin NeelaNithin Neela
Yes Now i got it ..Thank you For Explaination