+ 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){
            List<Timesheet_Entry__c> tsEntriesToUpdte = new List<Timesheet_Entry__c>();
                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;
            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). 

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')
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 ProjectTrigger on Project__c (after update) {
    ProjectTriggerHandler.updateProjectTypeOnAllRelatedRecords(trigger.newMap, trigger.oldMap);


Let me know, if this helps.
Abdul KhatriAbdul Khatri

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



Abdul KhatriAbdul Khatri
Hi Takshima,

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