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
Yasa KusumaYasa Kusuma 

Apex Test Class - Trigger not firing

HI, total noob here. 
I have a n object called Preference__c, has an account field, and has a contact field. What I am trying to do is simple, fill in the contact, the account is auto filled in. It works fine on the regular UI, but when doing the test method, thr trigger doesn't seem to be firing. Does it need a delay of some sort? 


The Trigger
=====================================================

trigger AutoPopulateCompany on Preference__c (before insert, before update)
{
    for(Preference__c pref: trigger.new){
        if(pref.Contact__c != null && pref.Account__c == null){
            //Instantiate the Preference class
            Preference.autoPopulateCompany(pref);
        }
    }   
}

Preference Class
=====================================================

public with sharing class Preference   
{
    //Used to populate the company of the account automatically if it is not filled in by the user
    public static void autoPopulateCompany(Preference__c preference)
    {
        set<Id> conIdSet = new set<Id>();
       
        conIdSet.add(preference.Contact__c);
       
        //Query for the account associated with the preference contact and place in a map
        map<id, contact> conMap = new map<id, contact>([SELECT id, accountid from contact where Id in: conIdSet]);
       
        //check if map is not empty (the contact's account is not null)
        if(!conMap.isEmpty())
        {
            preference.Account__c = conMap.get(preference.Contact__c).accountId;           
        }
    }
}

Test Class
=====================================================

@isTest
private class TestPreference
{
    private static testmethod void testAutoPopulateCompany()
    {
       //Test Data Prep
       //----------------------------------------------------
      
       //Create 2 test accounts
       Account acc1 = new Account();
       acc1.Name = 'Acc1-ACX';
       insert acc1;
      
       Account acc2 = new Account();
       acc2.Name = 'Acc2-ACX';
       insert acc2;
          
       //Create test contact with account 1
       Contact con1 = new Contact();
       con1.FirstName = 'con1';
       con1.LastName = 'ACX';
       con1.Account = acc1;
       insert con1;
      
       //Create test contact with account 2
       Contact con2 = new Contact();
       con2.FirstName = 'con2';
       con2.LastName = 'ACX';
       con2.Account = acc2;
       insert con2;
      
       //Create test contact with no account
       Contact con3 = new Contact();
       con3.FirstName = 'con3';
       con3.LastName = 'ACX';
       insert con3;
      
       //Test Create
       //----------------------------------------------------
      
       //Test Create with contact with account NOT filled in
       Preference__c pref = new Preference__c ();
       pref.Name = 'Test Pref 1';
       pref.Contact__c = con1.Id;
       insert pref;
      
       //Assert - Expected result: Preference Account filled in with Acc1
       pref = [SELECT Id, Account__c FROM Preference__c WHERE Id = :pref.Id];
       System.assertEquals(Acc1.Id, pref.Account__c);            
    }
}

The result
=================================================================

System.AssertException: Assertion Failed: Expected: 001F0000015bMbpIAE, Actual: null

The trigger should have populated pref.Account__c...but I get null. When I test on the actual UI, it works fine. 

Thanks in advance. 
Best Answer chosen by Yasa Kusuma
Eli Flores, SFDC DevEli Flores, SFDC Dev
The problem is in the contact creation section

con1.Account = acc1; /// this doesn't work
con1.AccountID = acc1.id;//this will.

For some reason that first line doesn't report an error but it doesn't do what you think it will. The real field is AccountID and that's the one you need to populate.

All Answers

Eli Flores, SFDC DevEli Flores, SFDC Dev
The problem is in the contact creation section

con1.Account = acc1; /// this doesn't work
con1.AccountID = acc1.id;//this will.

For some reason that first line doesn't report an error but it doesn't do what you think it will. The real field is AccountID and that's the one you need to populate.
This was selected as the best answer
Yasa KusumaYasa Kusuma
Thanks so much Eli! That works!