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
ANITHA BEEMU 2ANITHA BEEMU 2 

Hi can anyone help me to write a test class for the following delete trigger


trigger TriggerTo_Delete_AccountLookup on Account (before delete) {

     

    //To store parent ids

    list<id> AccountIds=new list<id>();

    for(Account accountVar:trigger.old)

    {

        AccountIds.add(accountVar.id);

    } 

    //Collecting all child records related to Parent records

    list<AccountLookup__c> listOfAccountLookup=[select Account_Name__r.Id from AccountLookup__c where Account_Name__r.Id in :AccountIds];

    system.debug('listOfAccountLookup'+listOfAccountLookup);

    //deleting child records

    delete listOfAccountLookup;
Best Answer chosen by ANITHA BEEMU 2
Shruti SShruti S
Try the below code:
@isTest
private class TriggerToDeleteAccountLookup_Test {

	private static testMethod void test() {
        Account acc = new Account();
        
        acc.Name = 'Test Account';
        
        INSERT acc;
        
        AccountLookup__c accLookup = new AccountLookup__c();
        
        accLookup.Account_Name__c = acc.Id;
        
        INSERT accLookup;
        
        DELETE acc;
	}

}

All Answers

Raj VakatiRaj Vakati
Try this code
 
@isTest 
public class TriggerTo_Delete_AccountLookupTest 
{
    static testMethod void testMethod1() 
	{
			Account acc = new Lead() ;
			acc.Name = 'Acc';
			 insert acc;

			 
			 AccountLookup__c accL = new AccountLookup__c();
			 accL.Name ='Test' ;
			 accL.Account_Name=acc.Id ;
			 insert accL;
			try
			{
				Delete 	acc;
			}
			catch(Exception ee)
			{}
    }
}

 
Shruti SShruti S
Try the below code:
@isTest
private class TriggerToDeleteAccountLookup_Test {

	private static testMethod void test() {
        Account acc = new Account();
        
        acc.Name = 'Test Account';
        
        INSERT acc;
        
        AccountLookup__c accLookup = new AccountLookup__c();
        
        accLookup.Account_Name__c = acc.Id;
        
        INSERT accLookup;
        
        DELETE acc;
	}

}
This was selected as the best answer
Niraj Kr SinghNiraj Kr Singh
@istest
Private class DeleteAccountTest {
    static testMethod void testMethod1() {
        Account objAccTest = new Account ();
        objAccTest.Name = 'TestAccount';
        // Add other required fields here of account...
        
        insert objAccTest;

        AccountLookup__c  objaccLookup = new AccountLookup__c ();
        objaccLookup.Name = 'TestAccLookup';
        objaccLookup.Account_Name__c = objAccTest.Id;
         insert objaccLookup;
       
         AccountLookup__c  objaccLookupRec = [Select Account_Name__c From AccountLookup__c Where Id =: objaccLookup.Id ] ;
        System.assertEquals(objaccLookupRec.Account_Name__c, objAccTest.Id);
         
         Test.starttest();
               delete objAccTest;
          Test.stoptest();
     }
}

TRY THIS CODE AND LET ME KNOW IF IT WORKS FOR YOU
THANKS
NIRAJ