function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Constance RougeConstance Rouge 

Apex trigger: Test class Assertion failed

Hi All,

I am new to Apex triggers. Could you please help me with an issue I have with my test class?
I have a Master-Child relationship between Person account and a custom object called Staff_and_duty_travel__c (Person account being the master), but I am not always sure I'll have a value in the Associated_account__c field of Staff_and_duty_travel__c.
Therefore, I wrote an Apex Trigger before insert on this object.

The Trigger checks if there is a value in Associated_account__c.
If not, it uses the field Staff_email_address__c of the record to search in the emails fields of the Person Accounts (Customer_Email__pc and Secondary_Email__pc) if an account already exists with this email.
If yes, it gives the id of this account to the field Associated_account__c.
If not, it creates the account with the values contained in the record and it gives the id of this new account to the field Associated_account__c.

The trigger seems to work fine (I created several records to test every scenario), but my Test class fails.

Here is my trigger:
trigger SDT_Master_Child_Relation on Staff_and_duty_travel__c (before insert) {
    
    Set<String> staffEmailAdress = new Set <String> ();
    List<Account> accountsList = new List<Account> ();
    Id assocAccountId;
    RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account' Limit 1];

    
    //We create the list of all the email adresses from the bookings that are inserted
    for (Staff_and_duty_travel__c Travel : Trigger.New)
    {
        staffEmailAdress.add(Travel.Staff_email_address__c);
    }
    
    //We create the list of all the accounts containing one of the emails from the bookings that are inserted
    for(Account acc :[select Id, Customer_Email__pc, Secondary_Email__pc from Account where Customer_Email__pc IN:staffEmailAdress OR Secondary_Email__pc IN:staffEmailAdress])
    {
               accountsList.add(acc);
    }
        
   
    //For every booking that is inserted, we associate an account.
    for (Staff_and_duty_travel__c Travel : Trigger.New)
    {
        assocAccountId=Null;
        
        //We check if an account is already associated
        assocAccountId=Travel.Associated_account__c;
        
        //If not, we associate one.
        If (assocAccountId==Null){
            
            //We check if an account with the Staff email exists
            for (Account acc2 : accountsList){
                if(acc2.Customer_Email__pc.equals(Travel.Staff_email_address__c) || acc2.Secondary_Email__pc.equals(Travel.Staff_email_address__c)) {
                    assocAccountId=acc2.Id;
                }
            }
            //If not, we create thew account
            if (assocAccountId==Null){
                //Create the account
                Account newPersonAccount = new Account();
                newPersonAccount.FirstName = Travel.Staff_First_Name__c;
                newPersonAccount.LastName = Travel.Staff_Last_name__c;
                newPersonAccount.Customer_Email__pc = Travel.Staff_email_address__c;
                newPersonAccount.PersonHomePhone=Travel.Staff_phone_number__c;
                newPersonAccount.RecordTypeId = personAccountRecordType.Id;
                insert newPersonAccount;
                assocAccountId=newPersonAccount.Id;
            }
        }
        //We associate the account
        Travel.Associated_account__c=assocAccountId;
    }
    
}
Here is my Test class (I have put in bold the assertions that fail):
 
@isTest

public class SDT_Master_Child_Relation_Test {
    
    
    static testMethod void testAssociateAccount(){
        RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account' Limit 1];
        
        //Create a test account
        Account accTest=new Account (RecordTypeId = personAccountRecordType.Id,
                                     FirstName='Test',
                                     LastName='TestAccountTrigger', 
                                     Customer_Email__pc='testFirstEmail@trigger.com',
                                     Secondary_Email__pc='testSecondEmail@trigger.com');
        insert accTest;
        
        //Create four travels to test the four possibilities
        Staff_and_duty_travel__c travelTest1=new Staff_and_duty_travel__c (Staff_Last_name__c='AccountAlreadyAssociated', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testNoEmail@trigger.com',
                                                                           Associated_account__c=accTest.Id);
        Staff_and_duty_travel__c travelTest2=new Staff_and_duty_travel__c (Staff_Last_name__c='FirstEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testFirstEmail@trigger.com');
        Staff_and_duty_travel__c travelTest3=new Staff_and_duty_travel__c (Staff_Last_name__c='SecondEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testSecondEmail@trigger.com');
        Staff_and_duty_travel__c travelTest4=new Staff_and_duty_travel__c (Staff_Last_name__c='ThirdEmail', 
                                                                           Staff_First_Name__c='TestTrigger',
                                                                           Staff_phone_number__c='0486786',
                                                                           Staff_email_address__c='testThirdEmail@trigger.com');
        
        List<Staff_and_duty_travel__c> travels = new List<Staff_and_duty_travel__c>{travelTest1, travelTest2, travelTest3, travelTest4};
        
        //Run the test
        Test.startTest();
        insert travels;
        Test.stopTest();

        //We check for each of the travels if the account has been associated
        //For travelTest1:
        System.assert(travelTest1.Associated_account__c==accTest.Id);
        
        //For travelTest2:
        System.assert(travelTest2.Associated_account__c==accTest.Id);
        
        //For travelTest3:
        System.assert(travelTest3.Associated_account__c==accTest.Id);
        
        //For travelTest4, we have to check if an account has been created with the right values, then if it has been associated to the travel:
        Account accTest4=[select Id, FirstName,LastName,PersonHomePhone
                         from Account
                         where RecordTypeId = :personAccountRecordType.Id and Customer_Email__pc=:travelTest4.Staff_email_address__c ];
        System.assert(accTest4.FirstName==travelTest4.Staff_First_Name__c);
        System.assert(accTest4.LastName==travelTest4.Staff_Last_name__c);
        System.assert(accTest4.PersonHomePhone==travelTest4.Staff_phone_number__c);
        System.assert(travelTest4.Associated_account__c==accTest4.Id);   
    }
}

I get the "System.AssertException: Assertion Failed" error for the assertion I have put in bold.
I don't understand where is the problem coming from, so any help is appreciated.
Regards,
Constance Rouge
 
Best Answer chosen by Constance Rouge
Neetu_BansalNeetu_Bansal
Hi Constance,

I analysed the code, to check the assertions, you need to query the value that sets in trigger. In your case, before the assertion statements, query the Associated_account__c of the custom object. Your first assertion passed, because you have passed the account id at the time of creating the record in test class.

Also, I analysed your trigger code, you are using DML in for loop, which is not good practice.

Let me know, if you need any other help.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Constance,

I analysed the code, to check the assertions, you need to query the value that sets in trigger. In your case, before the assertion statements, query the Associated_account__c of the custom object. Your first assertion passed, because you have passed the account id at the time of creating the record in test class.

Also, I analysed your trigger code, you are using DML in for loop, which is not good practice.

Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer
Constance RougeConstance Rouge
Thank you for your answer Neetu, my test class works perfectly now.
For the DML in loop, I guess you're talking about the line 16 of my trigger? Because I saw this formulation on the Dreamforce Video :
https://youtu.be/LwDwmU0IkYk?t=885 at 14:45, line 16 (video I used to learn about how to make trigger, it was really helpful).
Neetu_BansalNeetu_Bansal
That's great. But I was reffering to line no 48. In case of bulk data, you will hit salesforce limits.

Anyways, I work on contract basis and if in future you need any help, you can contact me anytime. My contact details are mentioned in my profile.

Thanks,
Neetu