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
kri 10kri 10 

Trigger for Undeleting Account records

Hi,
  I am trying to undelete account records using trigger when deleted using Helper class(future call outs) but ending with exception.Please help me out.
Trigger:
         
trigger AccountsDelete on Account (After delete) {
    if(trigger.isDelete)
        if(system.isFuture()==True){
            HttpRequest req = new HttpRequest();
                 req.setTimeout(2000);
            Accounts_Trigger_Helper.DeleteAccount();
        }
}
Helper Class:
                
global class Accounts_Trigger_Helper {
    @future
    global static void DeleteAccount() {
        HttpRequest req = new HttpRequest();
                 req.setTimeout(2000);
        list<account> deletedlist=new list<account>();
        if(trigger.isdelete){
            list<account> deletedacc=new list<account>();
            deletedacc = [select id,name,phone from Account where
IsDeleted=:true  ALL rows];
            system.debug('deletedacc'+deletedacc);
            system.debug('hi');
            /* for(account acclist:trigger.old){
deletedlist.add(acclist);
system.debug('deletedlist'+deletedlist);
}
}*/
            if(!deletedacc.isEmpty()){
                undelete deletedacc;
            }
        }
    }
}
               
Thanks in advance
Best Answer chosen by kri 10
kri 10kri 10
hi, I got the output for the above scenario this is the code which worked well .

Trigger:

      trigger AccountsDelete on Account (After delete) {
    list<Id> I=new list<Id>();
    for(account a:trigger.old){
        I.add(a.Id);
    }
    if(trigger.isDelete){
        system.debug('hello am in trigger');
        if(system.isFuture()==False){
            HttpRequest req = new HttpRequest();
            req.setTimeout(2000);
            Accounts_Trigger_Helper.DeleteAccount(i);
        }
    }
}

HelperClass:
            global class Accounts_Trigger_Helper {
    @future
    global static void DeleteAccount(list<ID> acclist) {
        system.debug('hello am in Helper');
        HttpRequest req = new HttpRequest();
        req.setTimeout(2000);
        system.debug('acclist'+acclist);
        list<account> deletedacc=new list<account>();
        deletedacc = [select id,name,phone from Account where
                      isDeleted=true and Id =:acclist ALL rows];
        system.debug('deletedacc'+deletedacc);
        if(!deletedacc.isEmpty()){
            undelete deletedacc;
        }
    }
}

Test Class:

@isTest
public class Accounts_Trigger_Test {

    public static  testmethod void DeleteAccountTest(){
        test.startTest();
        account acc=new account();
        acc.name='Krishna';
        acc.Phone='84615';
        insert acc;
        delete acc;
        System.assertEquals('Krishna',acc.Name,'Name is not equal');
        test.stopTest();
    }
}