+ Start a Discussion
mahi68mahi68 

Why the salesforce is not having "before undelete" event in triggers?

Hi

 

 Why the salesforce is not having "before undelete" event in triggers?

any one could you please explain?

Ryan UptonRyan Upton
Triggers fire as the result of an action on an object . With an object that is deleted there are really no before actions you can take, you can only undelete it. The only action available on a deleted object is after you undelete it hence the after undelete.
Ashish_SFDCAshish_SFDC

Hi Mahi68, 

 

An update or Insert can have a Before functionality as the record exists.

Where as there is no Record before deleting the Actual record. 

Hence only after deleting we can have the Undelete event, where as every record merely exists before deleting 

 

Regards,

Ashish

Roger WickiRoger Wicki
Triggers fire as the result of an action on an object . With an object that is deleted there are really no before actions you can take, you can only undelete it. The only action available on a deleted object is after you undelete it hence the after undelete."
 

But the undelete action is an action per se so why not have the possibility to alter a value during the undeletion process?

I mean let's look at an example. We have incomplete data with dummy contacts. With this dummy contact there were important emails saved as activities. Because of a raging clean up operation, this contact got deleted. In the mean time, I as admin created a trigger to update a field with a majestic Error sign when a contact is erraneous. Some time later, somebody recognizes that the deleted dummy contact had important information on it so they undelete. So at the point of this undeletion it is vital that the contact is branded with the majestic error sign. Because a before undelete is not possible, i need to undelete it and update the record with a DML in an after undelete trigger??? This just does not make sense to me. I mean the data of deleted records is available. It is there. We don't save something on a deleted record with a before undelete trigger, we only alter the state it is reanimated.
Sankaran Nepolean 6Sankaran Nepolean 6
still, i can't find answer for this question. If anyone argues that record won't exist before undelete, hence no 'before undelete', then why do 'after delete' exists ? :)
Nithin NeelaNithin Neela
I think i got the answer for this question. The logic is AFTER DELETE and BEFORE UNDELETE operations are same. for performing operation after deletion of record we use AFTER DELETE and befor getting record from bin ACtually we have to use BEFORE UNDELETE but the action of AFTER DELETE is the same .so We dont have BEFORE UNDEETE. If any suggestions plz let me know 
Suparna PodderSuparna Podder

I am a beginer in Salesforce, hence can any one help me understand how AFTER DELETE and BEFORE UNDELETE event is same? Suppose, I have already deleted account record with id="001as4656GF90r7". How can I apply AFTER DELETE on this record to perform some action?
Zachary CohanZachary Cohan

The limitation is that we can't modify fields in the after triggers. I actually have a use case where I need a Before Undelete because of the way Salesforce handles deletion of certain standard objects.
Contact Roles on Opportunities (api name OpportunityContactRoles) gets "deleted" by setting a flag called isDeleted equal to true. So here's the use case.
lets say we have an OpportunityContactRole (OCR), associated with Contact John Smith.
If we delete John Smith, then the OCR gets "deleted" (isDeleted=true) and if we Undelete John Smith, then the OCR gets "undeleted" by setting isDeleted=false. easy enough, right?

Well, we have a wrapper object (Custom Contact Roles [referred to as QCR]) around Opportunity Contact Roles that stores a reference to the OCR via the Salesforce ID of the OCR.
so if we were to insert a new QCR, a trigger makes sure that a parallel OCR gets created. IF we are to update the QCR, then the OCR gets updated. but if we are to delete the QCR, then we just have to set the isDeleted=true flag on the OCR and on undelete, set the flag equal to false, right?

Well, the isDeleted field is read-only.

So I MUST do a hard delete (deleting from the actual database-- not just setting an isDeleted flag) in order to make sure that when one gets deleted, so does the other.
Because I HAD to do a hard delete, we no longer have a reference to any of the info that was stored in the OCR.
Attempted solution: Insert a brand new OCR on undelete and Modify the reference in the QCR to the new Salesforce ID of the newly inserted OCR.
But the issue is that we aren't allowed to modify fields in an after context. So I am not allowed to rewrite the reference to the other object's ID.

Mind you, this is not a look up, this is just a text field that stores the SFID. Because you cannot store a lookup reference to a contact role on an opportunity. 
 

Shubham Tiwari 11Shubham Tiwari 11
As before Undelete Record is not exist in the actual org. It is still in the recycle bin. Save action of DML is still in progress. If you Query the record you will not find it. That's why no before undelete action is present. But After Undelete is exist because Record is saved back in the org and Id of record can be generated back again. We can query the actual record from database now.