+ Start a Discussion
Zoom_VZoom_V 

Test Class for Trigger

I've got an after insert\after update trigger running off the Account object which is upsertting a new child record (object name : "SubsidiariesAndBrands__c"). It's running fine, but for some reason I can't get this test class to work properly. 

 

Here is the whole test class : 

 

public with sharing class AutoCreateSubsOnAccountTriggerTest {

    static testMethod void testAutoCreateSubsOnAccountTriggerTest() {
        // Set up test User.
        User testUser = generateTestUser();

        // Generate Account
        Account acc = new Account(Name = testUser.Id);
 
        // Start test and insert sub
        Test.startTest();
        insert acc;
        Test.stopTest();

        // Get the subs from the DB to ensure they were created
        List<SubsidiariesAndBrands__c> subs = [SELECT
                                                 Id
                                             FROM
                                                 SubsidiariesAndBrands__c
                                             WHERE
                                                 Parent_Account__c = :acc.Id
        ];

        // There should be 1 sub
        System.assert(1 == subs.size(), 'One sub object is expected');
    }

    private static User generateTestUser(){
        // using the System Admin profile because it will always be there
        Profile sysAdmin = [SELECT 
                               Id 
                            FROM 
                               Profile 
                            WHERE 
                               Name = 'System Administrator'
                            LIMIT 1
        ];
 
        // Generate the user
        User newUser = new User(
            UserName = 'unit.tester@example.com',
            LastName = 'Test',
            FirstName = 'Jane',
            CompanyName = 'TestCo',
            Title = 'Developer',
            Email = 'unit.tester@example.com',
            ProfileId = sysAdmin.Id,
            emailencodingkey = 'UTF-8',
            languagelocalekey = 'en_US',
            localesidkey = 'en_US',
            timezonesidkey = 'America/Los_Angeles'
        );

        // Insert that user
        insert newuser;

        return newuser;
    }
}

 It's error-ing out on the line I underlined, with the error message of : "System.AssertException: Assertion Failed: One sub object is expected"


I guess for some reason the sub record isn't being created. I can't figure out why. 

 

 

Here is the actual trigger if you need it : 

 

 

trigger AutoCreateSubOnAccount on Account (after insert, after update) {
         public string type{get;set;}
List <SubsidiariesAndBrands__c> subToInsert = new List <SubsidiariesAndBrands__c> ();
      
    for (Account o : Trigger.new) { 
         
        type = o.Auto_Formula_Record_Type__c;
          System.debug('xxx type string xxx ' + type );
                   
        SubsidiariesAndBrands__c v = new SubsidiariesAndBrands__c (); //instantiate the object to put values for future record
        
                  
        v.Parent_Account__c= o.id; 
        v.Name = o.Name;
        v.SubName__c = o.Name;
        v.Industry__c = o.Industry; 
        v.Subsidary_Brand_Account_Number__c = o.Account_Number__c;
        v.ParentRecName__c = type;
                
        subToInsert.add(v);
      
    }
    
    try {
       if (type == 'Ultimate Parent') 
        upsert subToInsert SubName__c; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
    
}

 

 

Thank you very much for your help and input.

 

ArunDavidNSIArunDavidNSI

The insert statment for the subs is only firing if the type happens to be ultimate parent. But in the test class the account being inserted does not explicitly state what the record type of the account is. You should try and query the specific record type ID and assign it to the account before you insert it in your test class. 

 

Arun

Zoom_VZoom_V

Ah yes, you're right -  thank you Arun. 

 

However, now I have another problem : I need to set the Record Type name of the account to 'Ultimate Parent' in the test in order to make sure the subsidiary gets created in the test. How can I do that ? I don't know how to set the name (not the id) of a Record Type.

 

...or do you have a better solution for making the test work ?

 

Thank you.