function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
snyhansnyhan 

Test Class for Trigger to update Person Account System.AssertException: Assertion Failed

I'm pretty new to Apex and am trying to create a test class for a trigger that should update the status of the person account. When I ran the test, I got the following failure message:

 

System.AssertException: Assertion Failed: Expected: Needs Introduction Email, Actual: Active

 

I have no idea if I've written the trigger incorrectly or if the issue is in my test. I wrote a similar trigger that works perfectly to update the contact and then another to update the business account from the contact status. If you need to see those as well to help, let me know. It almost seems like the trigger is the issue since the status is not updating, but I'm not sure.

 

Here is the trigger:

trigger NeedsIntroductionEmailPrimary1PA on S_C__c (after update) {

List<id> paIds = new List<Id>();
for(S_C__c sc:trigger.new){
if(sc.E_o_T_Status__c == 'Needs Introduction Email' && sc.Primary_Contact1__c != null &&
sc.Primary_Contact_1_Is_Person_Account__c == 'Person Account'){
paIds.add(sc.Primary_Contact1__c);
system.debug('Name is' + sc.Primary_Contact1__c);
}
}

List<Account> paList = [SELECT Id, Status__pc FROM Account WHERE Id IN: paIds];
for(Account pa: paList){
system.debug('Found Person Account' + pa);
pa.Status__pc = 'Needs Introduction Email';
}

try{
update paList;
system.debug('Person Accounts to update list size' + paList.size());
}

catch(Dmlexception e){
system.debug('Person Account update failed' + e);
}
}

 

And here is the test:

@isTest
private class TestNeedsIntroductionEmailPrimary1PA {

static testMethod void testPAStatusChange() {

Account[] newpa = new Account[]{
//create two Person Accounts by filling in the Account's LastName field
new Account(FirstName='Bob', LastName='Smith', PersonMailingStreet='1 ABC Way',
PersonMailingCity='Boston', PersonMailingState='MA', PersonMailingPostalCode='02210',
PersonEmail='bsmith@me.com.sandbox', Phone='6029483917', Status__pc='Active'),
new Account(FirstName='Tracy', LastName='Jones', PersonMailingStreet='1 Polar Drive',
PersonMailingCity='Miami', PersonMailingState='FL', PersonMailingPostalCode='23984',
PersonEmail='tjones@me.com.sandbox', Phone='8243882346', Status__pc='Active')
};
insert newpa;

S_C__c[] sc = new S_C__c[]{
//create two new S Cs, add new PersonAccounts as Primary Contact 1
new S_C__c(Name='S 1', Status__c='PSO',
E_o_T_Status__c='None', Primary_Contact1__c=newpa[0].PersonContactId),
new S_C__c(Name='S 2', Status__c='PSO',
E_o_T_Status__c='None', Primary_Contact1__c=newpa[1].PersonContactId)
};
insert sc;

sc[0].E_o_T_Status__c = 'Needs Introduction Email';
Test.startTest();
update sc;
Test.stopTest();

newpa = [SELECT id, Status__pc FROM Account WHERE id IN: newpa];

{system.assertEquals('Needs Introduction Email', String.valueOf(newpa[0].Status__pc));
}
}
}

 

Thank you in advance for any help you might be able to provide,

 

Sam