0

I have an after insert/update trigger that on an object called Call_Report__c. The after trigger (upon save) sets the field of the Call_Report__c object. I'm not sure how to test it since I don't know how to make sure the trigger is being fired. I saw something on trailhead Database.Delete Result = Database.delete(acct, false) but can this apply in my situation? Is there one for update and insert? How do I distinguish before/after?

This is what I have so far for my test:

@isTest
public with sharing class CallReportTriggerTest {

    @isTest static void testSetFieldsMethod() {
        Call_Report__c callReport = new Call_Report__c();
        callReport.Loan_Apps_to_Relate__c = 'a071F00000A99Z4QAJ,a071F00000A99YpQAJ';
        Loan_Application__c loanAppRelate = new Loan_Application__c(Id = 'a071F00000A99Z4QAJ');
        Loan_Application__c loanAppRelate1 = new Loan_Application__c(Id = 'a071F00000A99YpQAJ');
        insert callReport;

        System.assertEquals(callReport.Id, loanAppRelate.Call_Report__c);
        System.assertEquals(callReport.Id, loanAppRelate1.Call_Report__c);
    }
}

Currently the tests fail saying actual is null.

2 Answers 2

2

Seeing your trigger logic would help quite a bit, but It's worth noting that hard-coding Ids in your tests is typically a bad idea.

Your best bet would be to actually create and insert your Loan_Application__c records before inserting your Call_Report__c record.

Then, you'll have to re-query your Call_Report__c record to get any field updates that happened in your trigger.

2
  • Could you elaborate more on what you mean by re-query?
    – Carly
    Jul 15, 2021 at 14:57
  • Nevermind, got it, thank you :)
    – Carly
    Jul 15, 2021 at 15:23
0

Test methods by default cannot see pre-existing records in an org. The test method itself should insert the records it needs. So this code:

callReport.Loan_Apps_to_Relate__c = 'a071F00000A99Z4QAJ,a071F00000A99YpQAJ';
Loan_Application__c loanAppRelate = new Loan_Application__c(Id = 'a071F00000A99Z4QAJ');
Loan_Application__c loanAppRelate1 = new Loan_Application__c(Id = 'a071F00000A99YpQAJ');
insert callReport;

will not work. Instead you will need to insert the Loan_Application__c records and set the actual Id values in the Loan_Apps_to_Relate__c field.

Also note that one:many relationships are usually implemented by the many side holding a reference (via an Id) of the one side, rather than the one side holding a collection of references (via Ids) to the many side.

You must log in to answer this question.

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