0

So I've written an Apex trigger for LiveChatTranscript, as well as a class that I think tests it; however Salesforce disagrees, and says my test has 0% coverage of my trigger. The trigger runs perfectly fine, and passes the test class, too, which must mean that the test I've written isn't relevant to the trigger I've written. I can't figure out how my test is improperly testing the trigger- y'all got any ideas?

Apex Trigger:

trigger GetPathways on LiveChatTranscript (before update) {
for (LiveChatTranscript transcript: Trigger.new) {
    if(transcript != null){
    transcript.dialogPathway__c = GetPathways.GetPathways(transcript);
   }
  }
 }

Testing Class:

    @isTest static void testUpdateTrigger(){
    List<LiveChatTranscript> testTranscripts1=[SELECT Id FROM LiveChatTranscript WHERE Name = '00000089'];
    if(testTranscripts1.size()>0){
        LiveChatTranscript testTranscript1 = testTranscripts1[0];
        String before = testTranscript1.dialogPathway__c;
        testTranscript1.Platform='Ubuntu';
        update testTranscript1;
        Test.starttest();
        String after = testTranscript1.dialogPathway__c;
        Test.stoptest();
        System.assertEquals(before, after);
    }
 }

Thanks for the help!

1 Answer 1

3

By default your test class can't read the records of your org. So in your method, if(testTranscripts1.size()>0) returns false and nothing happens.

To allow it to see the record, you have to annotate your method with @isTest(SeeAllData=true).

However, using SeeAllData is not a good idea since if anyone deletes the record named 00000089 in the org, your test will fail. You'd rather use a @testSetup method (see https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm) to insert a temporary LiveChatTranscript record before you run your test.

[ edit ] As pointed by PhilW in comments below, you can also insert the record directly in your test method if the record is not (and will never be) useful in other tests of the class. I think however it's a godd practice to separate test data insertion and tests themselves every time you can. Otherwise when you'll add tests you'll have to refactor (or duplicate) it.

More information here : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data.htm?search_text=seealldata


By the way, the line if(transcript != null) in your trigger is unnecessary, you'll never get a null value in a Trigger.new list.

6
  • 1
    Worth noting that you can create the test data directly in your test method if it doesn't need to be shared across multiple test methods. Also with noting that the testMethod keyword is deprecated in favour of the @IsTest annotation.
    – Phil W
    Feb 7, 2021 at 10:57
  • 1
    @PhilW You're right on both points. About the testMethod keyword, I meant @testSetup, I'm going to correct it. Thanks for the comment. Feb 7, 2021 at 11:02
  • 1
    By the way @GordyNumberOne, the line if(transcript != null) in your trigger is unnecessary, you'll never get a null value in a Trigger.new list. Feb 7, 2021 at 11:14
  • I suggest you add that to the answer as well.
    – Phil W
    Feb 7, 2021 at 11:15
  • 2
    I'd put it there anyway, at the end of the answer. It's always best to provide full feedback on the code to help the OP improve their skills and knowledge. I tend to have something like "By the way, blah blah" as the form of explanation.
    – Phil W
    Feb 7, 2021 at 11:24

You must log in to answer this question.

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