0

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:

code coverage

Error:

error

10
  • Is your assertion failing? That should be the net result if this code is not covered. If the assertion is not failing, something else is going on.
    – sfdcfox
    Dec 23, 2021 at 16:19
  • Yes, the assertion is failing.
    – tonydigi1
    Dec 23, 2021 at 16:20
  • Can you try with this rewrite? You'll also need to change the trigger logic to: BusinessProcessTriggerHelper.financialPlannerUpdate(Trigger.newMap, Trigger.oldMap);
    – sfdcfox
    Dec 23, 2021 at 16:27
  • That code is definitely more efficient, and it works in the UI. However, It's still only 92% code coverage and the assertion in the test class is still failing.
    – tonydigi1
    Dec 23, 2021 at 16:46
  • 1
    You should never hard-code ids anywhere, for any reason. That said, if your assertion is failing, please edit your post to make that more clear.
    – Adrian Larson
    Dec 23, 2021 at 16:46

1 Answer 1

0

Might be related to sharing rules. Removing with sharing from the helper class would be an easy way to tell - though that probably should not be the end solution.

1
  • That did it - removing with sharing gave me 100% code coverage and the assertEquals passed. Thanks!
    – tonydigi1
    Dec 24, 2021 at 0:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .