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
JonSimmonsJonSimmons 

After Undelete Trigger Problem

I'm trying to create a trigger on Asset, after Undelete.

 

I simply want to set a flag to denote that the record has been undeleted.

 

 I expect that I should be able to do something like the following, but I get an "Attempt to de-reference a null object" error on the for loop line.

 

 

trigger Asset_Intercept_UnDelete on Asset (after undelete)

{

 

    for(Asset undeletedAsset : trigger.old)

  {

     Asset theAsset = [select undeleted__c from Asset where id in :trigger.old];

     theAsset.undeleted__c = true;

     update theAsset;

  } 

}

 

 

Any help or pointers are greatly appreciated.

 

 

Thanks

Jon

 

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

Try this:

 

 

trigger Asset_Intercept_UnDelete on Asset (after undelete) { //Move the query outside the for loop to avoid any issues with governor limits Asset[] theAsset = [select undeleted__c from Asset where id in :trigger.new]; for(Asset undeletedAsset : theAsset){ undeletedAsset.undeleted__c = true; } //Move the update DML statement outside the for loop update theAsset; }

 

 

 

All Answers

aalbertaalbert

Try this:

 

 

trigger Asset_Intercept_UnDelete on Asset (after undelete) { //Move the query outside the for loop to avoid any issues with governor limits Asset[] theAsset = [select undeleted__c from Asset where id in :trigger.new]; for(Asset undeletedAsset : theAsset){ undeletedAsset.undeleted__c = true; } //Move the update DML statement outside the for loop update theAsset; }

 

 

 

This was selected as the best answer
JonSimmonsJonSimmons

 

Hey, that worked great!

 

Thanks alot!

 

 

Jon

 

sandeep reddy 37sandeep reddy 37
this trigger id not working why

trigger afterundeletecheck on Account (after undelete) {
    if(trigger.isafter&&trigger.isundelete){
list<account> a=[select id,name,srinivasreddy__undeleted__c from account where id =:trigger.new];
        for(account b:a){
            
                    b.srinivasreddy__undeleted__c=true;
            a.add(b);
            }
            update a;
        }
    
}
BR UserBR User

Hello i am write one trigger on custom object but tigger is not working so please help me

trigger AfterUndeleteUser on UserDetails__c (after upsate)
{

    List<UserDetails__c> ud = [select Id, Name, Is_Active__c from UserDetails__c where id in:trigger.new];
    
    for(UserDetails__c UndeleteUser : ud){
    UndeleteUser.Is_Active__c = true;
    }
    Update ud;

    
}

sumit choubey 5sumit choubey 5
Use this code block :-
trigger AfterUndeleteUser on UserDetails__c (before update){
  for(UserDetails__c UndeleteUser : trigger.new){
    UndeleteUser.Is_Active__c = true;
  }   
}
Neha NehaNeha Neha
Hi. Im trying to write an after undelete trigger on a custom object property. Can you please help me write an after undelete trigger , also updating the fields in the related object after undeleting the property record.