+ Start a Discussion
VSK98VSK98 

After Undelete Trigger

Hi,

I have written the sample trigger on case object. But getting the error is Read Only Error

The code is below
trigger caserecundel on Case (after undelete) {

    if( trigger.isundelete){
    
    system.debug('Entering in the undelete*****');
    for(case c : trigger.new){
    system.debug('Entering in the Loop*****');

        c.After_Undelete__c = true;
        undelete c;
    
   }

  }
}

Any suggestions 

Adv Thnx
VSK
vikrant kumarvikrant kumar
Can u describe ur usecase?
 
JeffreyStevensJeffreyStevens
You wouldn't wan to do an undelete c; again - remember this is is a trigger on the AFTER UNDELETE event.  So your trigger.new already has the undeleted record.  

I think you'll need to select the un-deleted records with a SOQL, then change all of the After_Undelete__c flags to true, then do an update on them.
 
VSK98VSK98
Stevens 

Could you explain cleary bcz i am new to work on this

Adv Thnx,
VSK
VSK98VSK98
Vikranth

Nthng here jst update the undelete record .....

i did mistake Update C;

Regards,
VSK
JeffreyStevensJeffreyStevens

trigger caserecundel on Case (after undelete) {

    if( trigger.isundelete){

    //system.debug('Entering in the undelete*****');
    //for(case c : trigger.new){
    //system.debug('Entering in the Loop*****');
    //    c.After_Undelete__c = true;
    //    undelete c; 
    //}

      // Get Cases just un-deleted
      list<Case> casesUnDeleted = new list<Case>();
      casesUnDeleted = [SELECT id,After_Undelete__c FROM Case WHERE Id IN :trigger.newmap.keyset()];
  
      for(Case c :casesUnDeleted) {
        c.After_Undelete__c = true;
      }

      update casesUnDeleted;

    }

}

So, you see - the trigger is firing AFTER the case has been un-deleted.  By that time - you can not do anything with the buffer record.  So, you need to re-aquire the records that were un-deleted. (remember a trigger can be call with a list of upto 200 records).

So the SOQL get's the records that were just un-deleted, and the you loop through those records and update the After_Undelete__c field, and then update the list of re-aquired records.

Hope that helps