You need to sign in to do that
Don't have an account?

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;
}
}
}
}
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;
}
}
}
}
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.
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')
Trigger
Let me know, if this helps.
Was my solutions helpful?
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!
Would you mind sharing how you are running this code? I mean trigger.