+ Start a Discussion
Michael LudwigMichael Ludwig 

System.Assert for trigger continues to return null even thoug the record is queried

Hi,

I am running into some issues when running a test class in which I do a system.assert for a trigger on accounts I have written.
The trigger is relatively simple, it checks two fields on the account record and if changed or if a new account is created, it populates two other fields with corresponding values that I retrieve from a custom setting.

The trigger works fine when I test it in the UI, here is the code:
 
trigger BU_Update on Account (before insert, before update) {
    
    
    Map<String, String> DD1Values = new Map<String, String>(); //map to hold values from DD1 Custom Setting
    For(DD1_Definition__c dd1 : [SELECT DD1_Code__c, DD1_Business_Unit__c FROM DD1_Definition__c]){
        DD1Values.put(dd1.DD1_Code__c, dd1.DD1_Business_Unit__c);
    }
    Map<String, String> DD2Values = new Map<String, String>(); // map to hold values from DD2 Custom Setting
    For(DD2_Definition__c dd2 : [SELECT DD2_Code__c, DD2_Business_Unit__c FROM DD2_Definition__c]){
        DD2Values.put(dd2.DD2_Code__c, dd2.DD2_Business_Unit__c);
    }
    
    //Compare old and new values for DD1 and DD2 codes, retrieve new value if changed
    For (Account a : Trigger.New){
        
        IF(Trigger.isInsert && Trigger.isBefore && a.RecordTypeId == Schema.SObjectType.Account.getRecordTypeInfosByName().get('Account').getRecordTypeId()){
            
            a.Industry_Code_Description__c = DD1Values.get(a.DD1_Industry_Business_Code__c);
            a.Business_Unit_Level_2__c  = DD2Values.get(a.DD2_Industry_Business_Code__c);
            
        }   
        
        IF(Trigger.isUpdate && Trigger.isBefore && a.RecordTypeId == Schema.SObjectType.Account.getRecordTypeInfosByName().get('Account').getRecordTypeId()){
            
            IF (a.DD1_Industry_Business_Code__c != Trigger.OldMap.get(a.Id).DD1_Industry_Business_Code__c){
                a.Industry_Code_Description__c = DD1Values.get(a.DD1_Industry_Business_Code__c);
            }
            IF (a.DD2_Industry_Business_Code__c != Trigger.OldMap.get(a.Id).DD2_Industry_Business_Code__c){
                a.Business_Unit_Level_2__c  = DD2Values.get(a.DD2_Industry_Business_Code__c);
            }
            
        }
    }
    
}
Here is my test class:
 
@isTest
public class Account_BU_Update_Test {
    
    
    
    static testmethod void testall(){
        ClsTestCustomSettingHelper.getAllSettings();
        ClsTestCustomSettingHelper.getPMV_Sales_Stage_Setting();
        ChannelinsightData_test.ChannelinsightData_test();
        ClsTestCustomSettingHelper.getTrigger_Configuration();
        
        RecordType rt = ClsTestHelp.createTestRecordType8();
        Account acc2 = ClsTestHelp.createTestAccounts();
        acc2.RecordTypeId =rt.Id;
        acc2.Account_Manager__c = UserInfo.getUserId();
        acc2.DD1_Industry_Business_Code__c = '001';
        acc2.DD2_Industry_Business_Code__c = '105';
        acc2.Name = 'Test account Michael';
        insert acc2;
        
        
        Test.startTest();
        
        acc2=[Select id, Industry_Code_Description__c, Business_Unit_Level_2__c FROM Account WHERE ID =:acc2.Id];
        System.assertEquals('Automotive', acc2.Industry_Code_Description__c);
        
        Test.stopTest();
        
    }
    
    
}
I need to include a few helper classes at the beginning and I am also creating a new account record with a helper class.
If I don't do a system assert, code coverage is fine, but I don't get why the system.assert would return:

System.AssertException: Assertion Failed: Expected: Automotive, Actual: null

I create a test record, insert it, query it again, including the field Industry_Code_Description__c, but the value for it continues to be null.
I verified that the account has the correct record type for the trigger and if I do the same test in the UI it works without any problems.
For the test data above I set the DD1_Industry_Business_code to '001' and in my custom setting the corresponing value would be 'Automotive'.

Any ideas where I might miss something ?

Thanks.
 
Best Answer chosen by Michael Ludwig
Gopal AgrawalGopal Agrawal
Hi,

Yes, I got your issue.
 In Test class, if you will use @istest(Seealldata=true), then only u can access custom setting data.
 

All Answers

Gopal AgrawalGopal Agrawal
Hi Michael,

I have just cross checked your code.

Could you please let me know, the relationship between Account and DD1_Definition__c?

As you are trying to do:

a.Industry_Code_Description__c = DD1Values.get(a.DD1_Industry_Business_Code__c); in line number 18.

Might be a.DD1_Industry_Business_Code__c is null or corresponding value of a.DD1_Industry_Business_Code__c is null.

This is why 

in System.assert, a.Industry_Code_Description__c is coming as null

 
Michael LudwigMichael Ludwig
Hi Gopal,

Thanks for taking the time to check the code.
DD1_Definition__c and DD2_Definition__c are two custom settings, they are of type list and simple contain some value pairs, e.g. '001' = 'Automotive'.
If somebody sets up an account of record type 'Account' and chooses '001' as value for field Account.DD1_Industry_Business_Code__c, the trigger would update the field Account.Industry_Code_Description__c by checking the value '001' in the map DD1Values which has the value pair 001 - Automotive.
I did system-debug the map before and the value pair exists, that's why I am surprised that the system.assert returns null.
Also, testing the trigger in the UI works without any issues.

Thanks
Gopal AgrawalGopal Agrawal
Hi,

Please replace line 18 & 19 with 

 a.Industry_Code_Description__c = DD1Values.get(a.DD1_Industry_Business_Code__c).DD1_Business_Unit__c ;
 a.Business_Unit_Level_2__c = DD2Values.get(a.DD2_Industry_Business_Code__c).DD2_Business_Unit__c;

If this will help you, please let me know and Mark as best answer 
Michael LudwigMichael Ludwig
Hi again,

this does not work I'm afraid, I get:
Initial term of field expression must be a concrete SObject: String
Michael LudwigMichael Ludwig
It appears strange that the test method fails while in the UI the trigger works perfectly well, retrieving all the values it should get.
Gopal AgrawalGopal Agrawal
Hi,

Yes, I got your issue.
 In Test class, if you will use @istest(Seealldata=true), then only u can access custom setting data.
 
This was selected as the best answer
Michael LudwigMichael Ludwig
.....aaaahhh, of course :-)
That solves the issue, thanks a lot, well spotted !