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
Takshima Goyal 2Takshima Goyal 2 

Write a test class for this class

public class ProjectTriggerHandler {

    public static void updateProjectTypeOnAllRelatedRecords(Map<Id, Project__c> mapOfNewProject, Map<Id, Project__c> mapOfOldMap){
        
        if(mapOfNewProject != Null && mapOfOldMap != Null){
            Set<Id> proIds = new Set<Id>();
            
            for(Id newProID: mapOfNewProject.keySet()){
                Project__c newPro = mapOfNewProject.get(newProID);
                Project__c oldPro = mapOfOldMap.get(newProID);
                
                if(newPro.Type__c != oldPro.Type__c){
                    proIds.add(newProID);                    
                }
            }
            List<Timesheet_Entry__c> tsEntriesToUpdte = new List<Timesheet_Entry__c>();
            if(proIds.size()>0){
                List<Timesheet_Entry__c> tsEntries = [SELECT ID, Project_Type__c, Project__c FROM Timesheet_Entry__c 
                                                      WHERE Project__c IN:proIds WITH SECURITY_ENFORCED];
                for(Timesheet_Entry__c tsEntry:tsEntries){
                    tsEntry.Project_Type__c = mapOfNewProject.get(tsEntry.Project__c).Type__c;
                    tsEntriesToUpdte.add(tsEntry);
                }
            }
            if(tsEntriesToUpdte.size()>0){
            update tsEntriesToUpdte;
            }
        }
    }
}
PriyaPriya (Salesforce Developers) 

The developer community recommends providing any attempts/code you've started, any errors you're getting, or where exactly you're struggling in achieving this while posting a question.


 
Abdul KhatriAbdul Khatri
Hi Takshima,

The following code should help you with the coverage

Please Note I am expecting the trigger running for the handler on after update like at the bottom (May be not exactly). 

Note
I am not sure the how Custom OBject Project__c Name field is define (Text or AutoNumber). If AutoNumber then you can remove that line.(prj.Name = 'Test Project'). Similarly with the Custom Object Timesheet_Entry__c Name field. If not AutoNumber, you may need to add a line like (te.Name = 'Test Timesheet Entry')
@isTest
public class ProjectTriggerHandler_Test {
    
    private static testMethod void test_updateProjectTypeOnAllRelatedRecords() {
     
        Project__c prj = new Project__c();
        prj.Name = 'Test Project';
        insert prj;
        
        Timesheet_Entry__c te = new Timesheet_Entry__c();
        te.Project__c = prj.Id;
        insert te;
        
        prj.Type__c = 'A';
        update prj;           
            
        Project__c prjAfterUpdate = [SELECT Id, Type__c FROM Project__c WHERE Id = :prj.Id];
        Timesheet_Entry__c teAfterUpdate = [SELECT Id, Project_Type__c FROM Timesheet_Entry__c WHERE Project__c = :prj.Id];
        
        system.assert(prjAfterUpdate.Type__c == teAfterUpdate.Project_Type__c);       
    }

}

Trigger
trigger ProjectTrigger on Project__c (after update) {
    
    ProjectTriggerHandler.updateProjectTypeOnAllRelatedRecords(trigger.newMap, trigger.oldMap);

}



Let me know, if this helps.
Abdul KhatriAbdul Khatri
Hi,

Was my solutions helpful?
Takshima Goyal 2Takshima Goyal 2
It is covering only 50% code of trigger
AbhinavAbhinav (Salesforce Developers) 
Check this:

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines

Thanks!
Abdul KhatriAbdul Khatri
Hi Takshima,

Would you mind sharing how you are running this code? I mean trigger.