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
Samantha ReddySamantha Reddy 

How to create a Test Class for the Trigger , weather we need to add it so the Apex Class

trigger CopyAccountTriggerTest on Account (before insert) {
  
   
    if(checkRecursiveTest.runOnce()) {
        Set<Id> accountIds = new Set<Id>();
        for (Account acc : Trigger.new) {
            if (acc.ParentId != null){
                accountIds.add(acc.ParentId);
            }
            if(accountIds.size() > 0){  
                  List<Account> accounts = [select id,Name,Phone from account where id in :accountIds ];
                
                for (Account acc2 : Trigger.new) {
                    if (acc2.ParentId != null){
                        acc2.Name = 'Duplicate'+accounts[0].Name;
                        acc2.Phone= 'Duplicate'+accounts[0].Phone;
                    }
                }
            }
        }
 }

  
 
    
}

ApexClass:
 
public class checkRecursiveTest {
    private static boolean run = true;
    public static boolean runOnce(){
     if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

UseCase In Account Object:

Created a Trigger when the ParentAccount is selected in account object ,need to copy  the phone number and name and insert the record.
how can i add the Trigger in the Apex Class and Write Test Class...

Thanks 
Samantha
 
Best Answer chosen by Samantha Reddy
Abdul KhatriAbdul Khatri
Hi Samantha,

Here is the code for you reference with the Test class 100% Coverage. You do not need checkRecursiveTest.runOnce since you are running on before Insert so not including that in my code.
 
trigger CopyAccountTriggerTest on Account (before insert) {
    
    Map<Id, Account> qualifiedAccounts = new Map<Id, Account>();
    List<Account> qualifiedAccount = new List<Account>();
    for(Account acc : Trigger.new)
    {
        if(acc.ParentId != null)
        {
            qualifiedAccounts.put(acc.ParentId, acc);
        }
    }
    
    if(!qualifiedAccounts.isEmpty())
    {
        
        Map<Id, Account> parentAccountMap = new Map<Id, Account>([SELECT Name, Phone FROM Account WHERE Id = :qualifiedAccounts.keySet()]);
        
        for(Account acc2 : qualifiedAccounts.values())
        {
            if(parentAccountMap.get(acc2.ParentId) != null){
                acc2.Name = 'Duplicate'+parentAccountMap.get(acc2.ParentId).Name;
				acc2.Phone = 'Duplicate'+parentAccountMap.get(acc2.ParentId).Phone;
            }
        }       
    }

}

Test Class
@isTest
public class CopyAccountTriggerTest {

    static testMethod void testCopyAccountTrigger() {
        
        Account parentAccount = new Account (Name = 'Test Parent Account', Phone = '11122233333');
        Insert parentAccount;

		Test.startTest();        
        Account account = new Account (Name = 'Test Account', ParentId = parentAccount.Id);
        insert account;
        Test.stopTest();
        
        Account accountAfterInsert = [SELECT Name, Phone FROM Account WHERE Id = :account.Id];
        System.assert(accountAfterInsert.Name.contains('Duplicate'));
        System.assert(accountAfterInsert.Phone.contains('Duplicate'));

    }
    
}

I hope this will help
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Samantha,

Can you check the below test class.
 
@istest
public class TestCopyAccountTriggerTest {

    @istest
    static void testmethod1(){
        Account ac= new Account();
        ac.name='sample accoount';
        ac.Phone='1234567809';
        insert ac;
        Account acc2= new Account();
        acc2.name='sample account2';
        acc2.phone='3456767688';
        acc2.parentid=ac.id;
        insert acc2;
        
        system.assertEquals('Duplicate sample accoount', acc2.name);
    }
}

If this solution helps, Please mark it as best answer.

Thanks,
 
Abdul KhatriAbdul Khatri
Hi Samantha,

Here is the code for you reference with the Test class 100% Coverage. You do not need checkRecursiveTest.runOnce since you are running on before Insert so not including that in my code.
 
trigger CopyAccountTriggerTest on Account (before insert) {
    
    Map<Id, Account> qualifiedAccounts = new Map<Id, Account>();
    List<Account> qualifiedAccount = new List<Account>();
    for(Account acc : Trigger.new)
    {
        if(acc.ParentId != null)
        {
            qualifiedAccounts.put(acc.ParentId, acc);
        }
    }
    
    if(!qualifiedAccounts.isEmpty())
    {
        
        Map<Id, Account> parentAccountMap = new Map<Id, Account>([SELECT Name, Phone FROM Account WHERE Id = :qualifiedAccounts.keySet()]);
        
        for(Account acc2 : qualifiedAccounts.values())
        {
            if(parentAccountMap.get(acc2.ParentId) != null){
                acc2.Name = 'Duplicate'+parentAccountMap.get(acc2.ParentId).Name;
				acc2.Phone = 'Duplicate'+parentAccountMap.get(acc2.ParentId).Phone;
            }
        }       
    }

}

Test Class
@isTest
public class CopyAccountTriggerTest {

    static testMethod void testCopyAccountTrigger() {
        
        Account parentAccount = new Account (Name = 'Test Parent Account', Phone = '11122233333');
        Insert parentAccount;

		Test.startTest();        
        Account account = new Account (Name = 'Test Account', ParentId = parentAccount.Id);
        insert account;
        Test.stopTest();
        
        Account accountAfterInsert = [SELECT Name, Phone FROM Account WHERE Id = :account.Id];
        System.assert(accountAfterInsert.Name.contains('Duplicate'));
        System.assert(accountAfterInsert.Phone.contains('Duplicate'));

    }
    
}

I hope this will help
 
This was selected as the best answer