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
sfdc startsfdc start 

plz send a simple code to undelete opertion in triggers................

Best Answer chosen by sfdc start
SandhyaSandhya (Salesforce Developers) 
Hi,

Undelete operation in the trigger will be fired, if you are restoring deleted record from the recycle bin. Example. If you deleted an account record, then it will be moved to recycle bin, if you restore the deleted account record from recycle bin, then undelete event in account object will get triggered.
 
trigger Account_UnDelete on Account (after undelete) {
Account[] theAccount= [select undeleted__c from Account where id in :trigger.new]; 
for(Account undeletedAccount : theAccount){ 
undeletedAccount.undeleted__c = true;
 } 
update theAccount; 
}

Hope this helps you!

Thanks and Regards
Sandhya

All Answers

sandeep reddy 37sandeep reddy 37
trigger undeletecheack on Account (after delete, after undelete) {  
    List<Account> accountUpdateList = new List<Account>();
    if(trigger.isAfter && trigger.isdelete){
        for(account c : trigger.old){ 
            Account accountObject = new Account();
            accountObject.Id = c.Id;
            accountObject.srinivasreddy__deletedby__c = userinfo.getName(); 
            accountUpdateList.add(accountObject);
        } 
        if(!accountUpdateList.isEmpty()){
            update accountUpdateList;
        }
    }
    
    if(trigger.isafter && trigger.isundelete){
        for(account aa:trigger.new){         
            if(aa.srinivasreddy__deletedby__c == userinfo.getName()){ 
                aa.addError('You cannot undelete this record');
            } 
        } 
    } 
}
do this 
sfdc startsfdc start
plz send simple code of undelete opertion on account  object.......................
SandhyaSandhya (Salesforce Developers) 
Hi,

Undelete operation in the trigger will be fired, if you are restoring deleted record from the recycle bin. Example. If you deleted an account record, then it will be moved to recycle bin, if you restore the deleted account record from recycle bin, then undelete event in account object will get triggered.
 
trigger Account_UnDelete on Account (after undelete) {
Account[] theAccount= [select undeleted__c from Account where id in :trigger.new]; 
for(Account undeletedAccount : theAccount){ 
undeletedAccount.undeleted__c = true;
 } 
update theAccount; 
}

Hope this helps you!

Thanks and Regards
Sandhya
This was selected as the best answer
Ishwar ShindeIshwar Shinde
trigger AccountUndelete on Account (after undelete) {

List<Account> accList = new List<Account>();
for(Account undeletedAccount : trigger.new){ 
        undeletedAccount.Name = "Undeleted :" + undeletedAccount.Name;
        accList.add(undeletedAccount);
 } 

update accList;
}
Hope it will help!!
 
Amit Chaudhary 8Amit Chaudhary 8
Triggers and Recovered Records

The after undelete trigger event only works with recovered records—that is, records that were deleted and then recovered from the Recycle Bin through the undelete DML statement. These are also called undeleted records.

The after undelete trigger events only run on top-level objects. For example, if you delete an Account, an Opportunity may also be deleted. When you recover the Account from the Recycle Bin, the Opportunity is also recovered. If there is an after undelete trigger event associated with both the Account and the Opportunity, only the Account after undelete trigger event executes.

The after undelete trigger event only fires for the following objects:
  1. Account
  2. Asset
  3. Campaign
  4. Case
  5. Contact
  6. ContentDocument
  7. Contract
  8. Custom objects
  9. Event
  10. Lead
  11. Opportunity
  12. Product
  13. Solution
  14. Task
Trigger
trigger AccountUndeleteTrigger on Account (after undelete) 
{

	List<Account> listAccountToUpdate = new List<Account>();
	for(Account acc : trigger.new)
    { 
			acc.Name = 'Undeleted:' + acc.Name;
			listAccountToUpdate.add(acc);
	} 
	
	if(listAccountToUpdate.size() > 0 )
	{
		update listAccountToUpdate;
	}
	
}

Let us know if this will help you

Thanks
Amit Chaudhary