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
aressaress 

test class for trigger on account

Need test class for following trigger to create clone of account record after insert.

-------------trigger---------------
trigger CloneAccountTrigger on Account (after insert) {
    
    if(Trigger.isAfter){
        if(Trigger.isInsert){
        CloneAccountTriggerHandler.cloneAccountRecord(Trigger.New);
        System.debug('Trigger');
    }
    }
    
}
--------------handler-------------------
public class CloneAccountTriggerHandler {

    public static Boolean cloneRecord = true;
    public static void cloneAccountRecord(Account[] newAccount){ 
        if(cloneRecord) {
            System.debug(newAccount);
            cloneRecord = false;
            List<Account> listAccount = new List<Account>();
            for(Account account : newAccount)
            {
                Account newCloneAccount = account.clone(false);
                listAccount.add(newCloneAccount);
            }
            Database.insert(listAccount,false);
        }
    }
}
Steven NsubugaSteven Nsubuga
@isTest
private class CloneAccountTriggerTest {

	@isTest static void test(){
		Account testAcct = new Account(Name = 'Test Account');
		insert testAcct;
		
		List<Account> accts = [SELECT Id FROM Account];
		System.assert(accts.size() > 0);
	}
}

 
Ajay K DubediAjay K Dubedi
Hi Ayisha,

Please Try this code:-
@isTest
private class CloneAccountTriggerHandler_Test {

    @isTest static void CloneAccounTest(){

        List<Account> accountList = new List<Account>();

        for(Integer i=0;i<10;i++)
        {
            Account accobj = new Account();
            accobj.Name='acc1';

            accountList.add(accobj);
        }
        insert accountList;


        test.starttest();
        List<Account> accountListnew = [SELECT Id FROM Account];
        accountListnew = [SELECT Id FROM Account];
        System.assertEquals(20,accountListnew.size());
        test.stoptest();
    }
}

Please mark as best answer if it helps you.

Thank You
Ajay Dubedi
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


Sample Test Class for you
@isTest
private class CloneAccountTriggerTest {

	static testmethod void  testUnit()
	{
		Account acc = new Account();
		acc.Name = 'Test';
		insert acc;
		
		List<Account> listAcc = [SELECT Id FROM Account];
		System.assert(listAcc.size() > 0);
	}
}


Let us know if this will help you