• Yasa Kusuma
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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. 
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.