I have an Apex Trigger and Handler class on the Business Process object. When a lookup field (Financial_Planner__c) is updated on the Business Process, the system should query all Tasks associated to that Business Process and reassign all tasks that were assigned to the old Financial Planner to the new Financial Planner. (Note - it should only reassign tasks that are assigned to the Financial Planner, not all tasks associated to the Business Process, as there may be tasks assigned to other roles).
My Trigger works as expected in the UI, but I am unable to achieve 100% code coverage in my test class and my SystemAssertEquals fails. Thoughts?
Trigger:
trigger BusinessProcessTrigger on FSTR__Business_Process__c (before insert, after insert, before update, after update) {
if( Trigger.isAfter && Trigger.isUpdate ){
BusinessProcessTriggerHelper.financialPlannerUpdate(Trigger.newMap, Trigger.oldMap);
}}
Handler Class:
public with sharing class BusinessProcessTriggerHelper {
public static void financialPlannerUpdate(Map<Id, FSTR__Business_Process__c> newRecords, Map<Id,FSTR__Business_Process__c> oldRecords){
Set<Id> changedRecordIds = new Set<Id>();
Set<Id> oldPlannerIds = new Set<Id>();
for(Id recordId: newRecords.keySet()) {
FSTR__Business_Process__c newRecord = newRecords.get(recordId);
FSTR__Business_Process__c oldRecord = oldRecords.get(recordId);
if(oldRecord.Financial_Planner__c != newRecord.Financial_Planner__c && newRecord.Financial_Planner__c != null) {
changedRecordIds.add(recordId);
oldPlannerIds.add(oldRecord.Financial_Planner__c);
}
}
List<Task> taskLst = [SELECT Id, Subject, OwnerId, BP__c FROM Task WHERE BP__c IN :changedRecordIds AND OwnerId IN :oldPlannerIds];
for(Task record: taskLst) {
record.OwnerId = newRecords.get(record.BP__c).Financial_Planner__c;
}
update taskLst;
}}
Test Class:
@istest public class BusinessProcessTriggerTest {
@istest static void myUnitTest(){
String uid = 'samUser';
User u = new User(Alias = uid, Email= uid + '@myorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = userInfo.getProfileId(),
TimeZoneSidKey='America/New_York', UserName= uid + '@myorg.com');
insert u;
Account myAccA = new Account();
myAccA.Name = 'myAccA';
myAccA.OwnerId = UserInfo.getUserId();
myAccA.recordtypeid=Schema.SObjectType.Account.getRecordTypeInfosByName().get('Household').getRecordTypeId();
insert myAccA;
FSTR__Business_Process__c biz = new FSTR__Business_Process__c();
biz.RecordTypeId = Schema.SObjectType.FSTR__Business_Process__c.getRecordTypeInfosByName().get('Merit Workflows').getRecordTypeId();
biz.Workflow__c = 'Client Review - IM/FP';
biz.FSTR__Subject__c = 'Test Workflow';
biz.Financial_Planner__c = UserInfo.getUserId();
biz.FSTR__Due_Date__c = system.today();
biz.FSTR__Business_Account__c = myAccA.Id;
insert biz;
Task t = new Task();
t.recordtypeid = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Advisor Task').getRecordTypeId();
t.OwnerId = UserInfo.getUserId();
t.Status = 'Open';
t.Subject = 'Test Task';
t.ActivityDate = system.today();
t.WhatId = myAccA.id;
t.BP__c = biz.Id;
insert t;
Test.startTest();
biz.Financial_Planner__c = u.id;
update biz;
Test.stopTest();
biz = [SELECT Id, Financial_Planner__c FROM FSTR__Business_Process__c WHERE Id = :biz.Id];
t = [SELECT Id, OwnerId, BP__c FROM Task WHERE BP__c =: biz.id];
system.assertEquals(biz.Financial_Planner__c, t.OwnerId, 'No Match');
}}
Code Coverage:
Error:
BusinessProcessTriggerHelper.financialPlannerUpdate(Trigger.newMap, Trigger.oldMap);