0

I'm learning Apex from David Liu's Apex Academy. I was reviewing my older triggers and noticed that this trigger no longer had coverage; I've updated my test class but it's not covering lines 5 - 8 where the related Case is created.

Account trigger

trigger AccountDedupe on Account (after insert) {
    for (Account acc : Trigger.new) {
        Case c = new Case(); //create new case record
        if (c.Product_Purchase_Date__c != null && c.Product_Has_Extended_Warranty__c != null) {
        c.OwnerId = acc.OwnerId; 
        c.Subject = 'Dedupe this account'; 
        c.AccountId = acc.Id;
        insert c;
        }
    }
}

test class

@isTest
public class AccountDedupeTest {
    
    @isTest static void createAccount() {
        Account acc = new Account();
        acc.Name = 'test name';
        insert acc;

        Case c = new Case();
        c.Product_Purchase_Date__c = Date.today();
        c.Product_Has_Extended_Warranty__c = false;
        c.Origin = 'Email';
        c.AccountId = acc.Id; 
        insert c; 
        
    }

    
    
}


code coverage image

2 Answers 2

1

Ignoring the test class and looking at your trigger, this line: Case c = new Case(); creates a new empty Case record in memory.

So when you reference it in your if conditions, Product_Purchase_Date__c and Product_Has_Extended_Warranty__c will always be null - because you haven't either...

  • Explicitly assigned them a value (e.g., c.Product_Purchase_Date__c = <someDate>)
  • Inserted the memory record into the database and then retrieved those field values*

*There could be default values set for those fields, so you might be able to insert the record without first assigning values to those fields and still see values in those fields when the record is queried.

Since both** of those values will always be null in your current code, your if condition always evaluates to FALSE, thus the code in your if block is never executed.

**Since you have the &&, if either of them are null then your if condition is FALSE


That explains technically why that code is not covered. But looking at the bigger picture...

I'm not sure of the full context of this code, but it seems to me that if you are going to create a Case to (seemingly) notify someone to deduplicate an Account, that your if conditions should be checking values of fields on the Account that triggered this code, not on the Case that you are creating.

1
  • Thanks @Moonpie - The AccountDedupe.trigger doesn't need to set those fields on the related Case for this particular trigger. The code is a bit of hodge podge as it's from different exercises in the Apex Academy learning from Pluralsight. One of the homework assignments that i've just reached is to update and fix some of these issues (which I didn't realise earlier when I was trying to fix it anyway!). I've also updated my previous answer so the code matches what I originally posted.
    – benji1304
    Mar 28, 2022 at 7:55
0

Found part of the problem! The if statement isn't correct. I've removed it for now.

   if (c.Product_Purchase_Date__c = !null || c.Product_Has_Extended_Warranty__c = !null) {

I've removed that and it works now. I need to fix the Case trigger, which was from another example .

3
  • Welcome to Salesforce Stack Exchange (SFSE)! The code snippet in this answer does not match the same line from your question. In your question you have ... != null while in this answer you have ... = null. Is that just a typo in this answer?
    – Moonpie
    Mar 27, 2022 at 2:10
  • oops, yes. I was changing it then commented it out to check my understanding. You're correct. Should I change my answer ?
    – benji1304
    Mar 27, 2022 at 2:16
  • Technically, removing that if statement would make the code be covered, but I'm not sure that is the route you want to take. (See my answer.) So you should edit this answer to change the code snippet to match the original code, and indicate that you removed it. Or maybe just delete this answer.
    – Moonpie
    Mar 27, 2022 at 2:38

You must log in to answer this question.

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