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
Sam LimSam Lim 

Action Plans V3 Unmanaged - Unable to undelete Accounts

I've been using "Action Plans V3 Unmanaged" for years.  However we just tried to undelete an account and found that "Action Plans V3" is blocking the undelete.  The error I get is this:
 
Unable to undelete 1 of the record(s) because:

The record may have already been undeleted or removed from the Recycle Bin
The record may have been orphaned by the deletion of the custom object
You may no longer have access to undelete the record(s)
A data integrity rule or Apex trigger would fail if the record was undeleted

  If I disable the trigger that came bundled with "Action Plans V3", I can undelete an account without any issues.

Has anyone else come across this problem?

Here's what the trigger code looks like:
 
trigger AccountTrigger on Account (before delete, after undelete) {
    
    set<ID>             aIds    = new set<ID>();
    List<String>        apIds   = new List<String>();
    List<ActionPlan__c> deletePermantently_apIds= new List<ActionPlan__c>();
    
    //Delete related action plans
    if ( trigger.isdelete ){
        for( Account a : trigger.old ){
            aIds.add( a.Id );
        }
    
        /* GET Action Plans to delete from recycle bin */
        deletePermantently_apIds = [ select Id, Name , LastModifiedDate from ActionPlan__c where Account__c in : aIds and isDeleted = true ALL ROWS ];
        
        if ( deletePermantently_apIds.size() >0 ){          
            Database.emptyRecycleBin(deletePermantently_apIds);
        }
    
        //Get all action plans associated with Accounts
        for( Account a : [Select (Select Id , isDeleted From Action_Plans__r) From Account a where Id in : aIds]){
            if (a.Action_Plans__r.size() >0 ){
                for(ActionPlan__c ap :a.Action_Plans__r ){                  
                    apIds.add(ap.Id);
                }
            }
        }
        if ( apIds.size() >0 ){     
            ActionPlansBatchDelete aPBatch = new ActionPlansBatchDelete(apIds, Userinfo.getUserId());
            Database.ExecuteBatch( aPBatch );
        }
    }
    
    //Undelete related action plans
    if ( trigger.isUnDelete ){
        Database.UndeleteResult[] unDel_errors;
        for( Account a : trigger.new ){
            aIds.add( a.Id );
        }
        list <ActionPlan__c> aPs = [ select Id, Name , LastModifiedDate from ActionPlan__c where Account__c in : aIds and isDeleted = true ALL ROWS ];
        
        try{
            if(ActionPlanObjectTriggerTest.isTest){
                //throw dmlException
                insert new Contact();   
            }
            
            unDel_errors =Database.undelete( aPs,false);
        } catch ( Dmlexception e ){             
            for (Account a: trigger.new){
                a.addError('You can not undelete an action plan whose related object is deleted.');
            }
        }
    }
    
}

Thanks in advance!  Any help would be greatly appreciated!

Sam.
v varaprasadv varaprasad
Hello Sam,

I think when we undelete parent records child records also undelete right(MD relationship).


Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Sam LimSam Lim
Hi Varaprasad. 

Yes, that is correct.  When I disabled the trigger, I could undelete the parent account and the children contacts would also get undeleted.

I just need to figure out how to do this with the trigger enabled.

Thanks!