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 bolded, 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 trigger itself : (this is working)

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);
    }

}

Any ideas ?

Thank you very much for your help.

  • 1
    Post your trigger's code as wel. – jmrjulian Jul 10 '13 at 20:11
  • Ok - I put it in there. Thank you. – Zoom_v Jul 10 '13 at 20:16

You need to set your test Accounts Auto_Formula_Record_Type__c to 'Ultimate Parent' for the triggers upsert to occur. Since this is a formula field based on the to the RecordTypeId, set the Record Type Id as required.

E.g.

// Generate Account
Account acc = new Account(Name = testUser.Id);
RecordType ultimateParentRecordType = [Select Id from RecordType where name = 'Ultimate Parent'];
acc.RecordTypeId = ultimateParentRecordType.Id;

Failing that, it is probably a DmlException being thrown in the trigger during the upsert that is causing the issue.


I don't know your requirements, but I'd suggest modifying your trigger so that the SubsidiariesAndBrands__c is only added to subToInsert if Auto_Formula_Record_Type__c == 'Ultimate Parent'. The the if condition before the upsert could just check the size of the list.

E.g.

trigger AutoCreateSubOnAccount on Account (after insert, after update) {

    List <SubsidiariesAndBrands__c> subToInsert = new List <SubsidiariesAndBrands__c>();

    for (Account o : Trigger.new) { 

        string type = o.Auto_Formula_Record_Type__c;

        System.debug('xxx type string xxx ' + type );
        if(type != 'Ultimate Parent') {
            continue;
        }


        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 (subToInsert.size() > 0) {
            upsert subToInsert; 
        }
    } catch (system.Dmlexception e) {
        system.debug(e);
    }

}
  • Daniel - I didn't even think of that. I didn't think that would be necessary. HOWEVER - now I have a new problem. That field is a formula field getting RecordType name ($RecordType.Name). I had to make it so I could refer to the RecordType by its name, not ID. But now Apex is not allowing me to automatically set that formula field in my test code. How should I get around this ? I don't know how to automatically get the RecordType name (not RecordTypeid) any way other than the formula field. Thank you again for your help. I really appreciate it. – Zoom_v Jul 11 '13 at 14:22
  • One other thing : I'm confused on what you're suggesting by adding SubsidiariesAndBrands__c to subToInsert. I'm already doing that with subToInsert.add(v); - aren't I ? The IF statement at the end of the trigger is working, so I'm confused on what you're suggesting, or what you're trying to accomplish ...? Thanks again. – Zoom_v Jul 11 '13 at 14:46
  • @Zoom_v Rather than trying to set the Formula field, set the Account RecordTypeId instead. – Daniel Ballinger Jul 11 '13 at 23:00
  • But if I use the AccountRecordTypeID then it won't work when I move the code over to production because the RecordTypeID will change - won't it ? That's why I want to use the RecordType Name. – Zoom_v Jul 12 '13 at 13:01
  • I'm not suggesting you hard code the RecordTypeId. See the SOQL query in my answer to find the RecordTypeId by name, and then use the resulting Id. This should work fine as long as the name matches in Dev and production. – Daniel Ballinger Jul 13 '13 at 8:52

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.