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
Malli GMalli G 

test class for After undelete event trigger

Hai Hall please help me get a code coverage
trigger afterundeletethecontact on Contact (After undelete) {
    list<Account>acc=new list<Account>();
    for(contact c:trigger.new)
    {
       
        Account a=new Account();
        a.id=c.AccountId;
        a.name=c.lastname;
        a.phone=c.phone;
    acc.add(a);
        
    
        
    }
    update acc;

}
testclass
@istest
public class Afterundeletecontacttest {
@istest
    static void testme(){
        Account a1=new Account(name='jjjj',phone='9999');
            insert a1;
        contact c=new contact(lastname='SSS',phone='1111',Accountid=a1.id);
        insert c;
        
        contact cf=[select lastname,phone from contact where id=:c.id];
        delete cf;
        contact ce=new contact(lastname='SSS',phone='1111',Accountid=a1.id);
        insert ce;
        Account af=[select id,name,phone from account where id=:a1.id];
        af.id=ce.AccountId;
        af.name=ce.lastname;
        af.phone=ce.phone;
        update af;
        system.assertEquals(af.id,ce.AccountId);
        system.assertEquals(af.name,ce.lastname);
        system.assertEquals(af.phone,ce.phone);
        
    }
}
 
Best Answer chosen by Malli G
BALAJI CHBALAJI CH
Hi Malli G,
Please find below modified Test Class which provides 100% code coverage:
@istest
public class Afterundeletecontacttest {
@istest
    static void testme(){
        Account a1=new Account(name='jjjj',phone='9999');
        insert a1;
        contact c=new contact(lastname='SSS',phone='1111',Accountid=a1.id);
        insert c;
        
        delete c;
        undelete c;
        
        contact ce=[select lastname,phone, AccountId from contact where id=:c.id];
        Account af=[select id,name,phone from account where id=:a1.id];

        system.assertEquals(af.id,ce.AccountId);
        system.assertEquals(af.name,ce.lastname);
        system.assertEquals(af.phone,ce.phone);
    }
}
Let us know if that helps you. Appreciate your response.

Best Regards,
BALAJI
 

All Answers

BALAJI CHBALAJI CH
Hi Malli G,
Please find below modified Test Class which provides 100% code coverage:
@istest
public class Afterundeletecontacttest {
@istest
    static void testme(){
        Account a1=new Account(name='jjjj',phone='9999');
        insert a1;
        contact c=new contact(lastname='SSS',phone='1111',Accountid=a1.id);
        insert c;
        
        delete c;
        undelete c;
        
        contact ce=[select lastname,phone, AccountId from contact where id=:c.id];
        Account af=[select id,name,phone from account where id=:a1.id];

        system.assertEquals(af.id,ce.AccountId);
        system.assertEquals(af.name,ce.lastname);
        system.assertEquals(af.phone,ce.phone);
    }
}
Let us know if that helps you. Appreciate your response.

Best Regards,
BALAJI
 
This was selected as the best answer
Malli GMalli G
Thank you Balaji