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
SFHelpMePleaseSFHelpMePlease 

trigger test class handler

Hello,
I have the below code and now need to write a test class to promote code to production.  I have attempted to write a test class but the account trigger is not getting any code coverage. Am I inserting the data in the correct place. Could you help me? 

    trigger AccountTrigger on Account (before insert, before update) 
    {
        TriggerHandler handler = new TriggerHandler();
    
        if(TriggerHandler.runTriggerOnce) {
            TriggerHandler.runTriggerOnce = false;
    
            if(Trigger.IsUpdate) {
                handler.accountBeforeUpdate();
            }    
       }
    }
    
    public class TriggerHandler
    {
        public static boolean runTriggerOnce= true; 
    
        public void accountBeforeUpdate()
        { 
            accountUpdates();
        }
    
        private void accountUpdates()
        {
            for(Account newAcc: (List<Account>)Trigger.New)
            {
                List<Other_Opp__c> opps = [Select Id, Name, Type__c From Other_Opp__c WHERE Opp_Name__c = :newAcc.Id ];
                if(opps.size() > 0){
                     For (Other_Opp__c f : opps) {
                        f.Type__c = newAcc.Field1__c;
                     }
                     update opps
                }
                
            }
    
    @isTest
    private class TriggerHandler_Test{
      @isTest static void test1() {
    Test.startTest();
    
            Account acc = new Account(Name = 'New Acct');
            insert acc;
    
            Opp_Name__c  opp = new Opp_Name__c (Name = 'New Opp');
            insert opp;
            
            
            Account a = [Select Id, Name, Field1__c from Account where Id = :acc.Id];
            a.Field1__c = 'test1';
            update a;
    
    Test.stopTest();
        }
    }
Steven NsubugaSteven Nsubuga
Your logic in the trigger seemed strange to me. I rewrote the trigger, handler and test. 

Trigger
trigger AccountTrigger on Account (after insert, after update) 
    {
        TriggerHandler handler = new TriggerHandler();
    
        if(TriggerHandler.runTriggerOnce) {
            TriggerHandler.runTriggerOnce = false;
    
            if(Trigger.IsUpdate) {
                handler.accountBeforeUpdate(Trigger.newMap);
            }    
       }
    }


Handler
public class TriggerHandler
    {
        public static boolean runTriggerOnce= true; 
    
        public void accountBeforeUpdate(Map<Id, Account> accts)
        { 
            accountUpdates(accts);
        }
    
        private void accountUpdates(Map<Id, Account> accts)
        {
			List<Other_Opp__c> opps = [Select Id, Name, Opp_Name__c, Type__c From Other_Opp__c WHERE Opp_Name__c IN :accts.keyset()];
			if(opps.size() > 0){
				
				for(Other_Opp__c opp: opps) {
					opp.Type__c = accts.get(opp.Opp_Name__c).Field1__c;
				}
				update opps;
			}
		}
	}


Test
@isTest
private class TriggerHandler_Test{
	
	@isTest static void test1() {
		
    
		Account acc = new Account(Name = 'New Acct');
		insert acc;

		Other_Opp__c opp = new Other_Opp__c (Name = 'New Opp', Opp_Name__c = acc.Id);
		insert opp;
		
		Test.startTest();
		
		Account a = [Select Id, Name, Field1__c from Account where Id = :acc.Id];
		a.Field1__c = 'test1';
		update a;
		Test.stopTest();
		
		Other_Opp__c opp2 = [Select Type__c from Other_Opp__c where Id = :opp.Id];
		System.assertEquals('test1', opp2.Type__c);
		
	}
}



 
Tejender Mohan 9Tejender Mohan 9
Hey,

On your code 
List<Other_Opp__c> opps = [Select Id, Name, Type__c From Other_Opp__c WHERE Opp_Name__c = :newAcc.Id ];
You have created a custom object Other_Opp__c which is having an account lookup named "Opp_Name__c". (I feel you should rename it to account_name , until unless it is not namd Opp Name for any requirement)
Now for the test class code coverage,
You have to execute the maximum number of lines of your Apex handler to get a proper coverage. (On dev console you can check the lines being executed by your test class. On the bottom of Developer console -> Tests Tab --> click on any class and then it will give you the uncovered lines in red.)
Check the Bottom of Developer console.

As per your test code, You are creating an opportunity but not associating it with the account, so that your class SOQL can retrieve it.
Try the below code : 
Test.startTest();
    
            Account acc = new Account(Name = 'New Acct');
            insert acc;
    
            Other_Opp__c  otherOpp = new Other_Opp__c (Name = 'New Opp'); otherOpp.Opp_Name__c = acc.id;        insert opp;
            acc.Field1__c = 'test1';
            update acc;
    
    Test.stopTest();



Thanks
Tejender Mohan
 
SFHelpMePleaseSFHelpMePlease
Thank you so much for your help!! Would you be able to guide me on the below? I need the incoming tasks subject and accountId, how can I retrieve that from the the “Map tasks”? Right now I get a null error, I believe that is because the trigger is run prior to saving so the id is not in the task object yet. private void taskCallReports(Map tasks) { Id accountIdSelected ; integer noOfDays; integer last30Days = 0; List test1 = [SELECT Id, Subject, CreatedDate, AccountId, TaskSubtype FROM Task where Id in :tasks.keyset()]; if(test1 .size() > 0){ for (Task tck: test1 ) { accountIdSelected = tck.AccountId; } } else { System.debug('No Call Tasks Found: ' + test1.size()); return; } }
SFHelpMePleaseSFHelpMePlease
I did some more digging and found Map tasks cannot be used on insert only on updates. Thanks From: Diane Gearon Sent: Saturday, July 28, 2018 6:34 PM To: 'reply' Subject: RE: (Salesforce Developers): New reply to your question. Thank you so much for your help!! Would you be able to guide me on the below? I need the incoming tasks subject and accountId, how can I retrieve that from the the “Map tasks”? Right now I get a null error, I believe that is because the trigger is run prior to saving so the id is not in the task object yet. private void taskCallReports(Map tasks) { Id accountIdSelected ; integer noOfDays; integer last30Days = 0; List test1 = [SELECT Id, Subject, CreatedDate, AccountId, TaskSubtype FROM Task where Id in :tasks.keyset()]; if(test1 .size() > 0){ for (Task tck: test1 ) { accountIdSelected = tck.AccountId; } } else { System.debug('No Call Tasks Found: ' + test1.size()); return; } }