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 = '[email protected]',
LastName = 'Test',
FirstName = 'Jane',
CompanyName = 'TestCo',
Title = 'Developer',
Email = '[email protected]',
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.