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
Ali MeyerAli Meyer 

Need help with test code for Apex trigger

Hello,

 

I'm really new to Apex and to code in general, so please try to not laugh at my pathetic test code. The code itself works (I tested manually) and it's a really basic trigger; the test code fails, though, and I can't figure out why. Can anyone help me figure out what's going on? Thank you!!

 

Trigger:

 

trigger SpouseTrigger on Contact (before insert) {
    List<Contact> cons = new List<Contact>();
    List<Contact> consToInsert = new List<Contact>();
    
    for(Contact c:trigger.new){
        if(c.Sig_Other_First_Name__c != null && c.Sig_Other_Last_Name__c != null){
            cons.add(c);
        }
    }
    
    for(Contact c: cons){
        Contact newCon = new Contact();
        newCon.FirstName = c.Sig_Other_First_Name__c;
        newCon.LastName = c.Sig_Other_Last_Name__c;
        newCon.AccountId = c.AccountId;
        consToInsert.add(newCon);
    }
    
    insert consToInsert;
}

 

Here's the test code, where something isn't working:

@istest
private class SpouseTriggerTest {

    static TestMethod void Test1_TestInsertContact() {
        Account acc = new Account(Name = 'My Account', RecordTypeid = '012A0000000dlDz');
        insert acc;
    
        Contact testC1 = new Contact(LastName='Test1 Contact', Sig_Other_First_Name__c='John', Sig_Other_Last_Name__c='Smith', AccountId = acc.Id);
        insert testC1;
        
        //pull the account info for that contact
        Contact SecContact1 = [SELECT FirstName, LastName, AccountId FROM Contact WHERE Id = :testC1.id];
        
        //verify that the insert updated by creating another contact as in the trigger
        System.assert(SecContact1.Id != null);
        System.assertEquals(SecContact1.FirstName, testC1.Sig_Other_First_Name__c);
        System.assertEquals(SecContact1.LastName, testC1.Sig_Other_Last_Name__c);
        System.assertEquals(SecContact1.AccountId, testC1.AccountId);
        
        Contact testC2 = new Contact(LastName='Test1 Contact', Sig_Other_First_Name__c=null, Sig_Other_Last_Name__c='Smith', AccountId = acc.Id);
        insert testC2;
        
        //pull the account info for that contact
        Contact SecContact2 = [SELECT FirstName, LastName, AccountId FROM Contact WHERE Id = :testC2.id];
        
        //verify that the insert updated by creating another contact as in the trigger
        System.assert(SecContact2.Id != null);
        System.assertNotEquals(SecContact2.FirstName, testC2.Sig_Other_First_Name__c);
        System.assertNotEquals(SecContact2.LastName, testC2.Sig_Other_Last_Name__c);
        System.assertNotEquals(SecContact2.AccountId, testC2.AccountId);
        
        Contact testC3 = new Contact(LastName='Test1 Contact', Sig_Other_First_Name__c='John', Sig_Other_Last_Name__c=null, AccountId = acc.Id);
        insert testC3;
        
        //pull the account info for that contact
        Contact SecContact3 = [SELECT FirstName, LastName, AccountId FROM Contact WHERE Id = :testC3.id];
        
        //verify that the insert updated by creating another contact as in the trigger
        System.assert(SecContact3.Id != null);
        System.assertNotEquals(SecContact3.FirstName, testC3.Sig_Other_First_Name__c);
        System.assertNotEquals(SecContact3.LastName, testC3.Sig_Other_Last_Name__c);
        System.assertNotEquals(SecContact3.AccountId, testC3.AccountId);
        
        Contact testC4 = new Contact(LastName='Test1 Contact', Sig_Other_First_Name__c=null, Sig_Other_Last_Name__c=null, AccountId = acc.Id);
        insert testC4;
        
        //pull the account info for that contact
        Contact SecContact4 = [SELECT FirstName, LastName, AccountId FROM Contact WHERE Id = :testC4.id];
        
        //verify that the insert updated by creating another contact as in the trigger
        System.assert(SecContact4.Id != null);
        System.assertNotEquals(SecContact4.FirstName, testC4.Sig_Other_First_Name__c);
        System.assertNotEquals(SecContact4.LastName, testC4.Sig_Other_Last_Name__c);
        System.assertNotEquals(SecContact4.AccountId, testC4.AccountId);
        
    }
}

 

Here's the error message I receive:

 

ClassSpouseTriggerTest
Method NameTest1_TestInsertContact
Pass/FailFail
Error MessageSystem.AssertException: Assertion Failed: Expected: null, Actual: John
Stack TraceClass.SpouseTriggerTest.Test1_TestInsertContact: line 16, column 1

 

Best Answer chosen by Admin (Salesforce Developers) 
Ali MeyerAli Meyer

Just in case you're on the edge of your seat, I figured this out and deployed it into production. Thank you so much for helping me!

All Answers

Clap MasterClap Master

You didn't assign a FirstName for testC1 - that might be it

Jeff MayJeff May

Welcome to the world of Apex development.  Prepare for a ride....

 

I think the problem is with this snippet:

 

for(Contact c: cons){
        Contact newCon = new Contact();
        newCon.FirstName = c.Sig_Other_First_Name__c;
        newCon.LastName = c.Sig_Other_Last_Name__c;
        newCon.AccountId = c.AccountId;
        consToInsert.add(newCon);
    }
    
    insert consToInsert;

 

You are in a before insert trigger, working on Contact record 'c'.  In this snippet, you are creating a new Contact record and setting that Contact record's fields.  The test code is checking the FirstName of the original Contact 'c', since it is using the ID of the Contact being 'inserted' by the test.  

 

Since you are in a before insert trigger, I would use the following snippet:

 

for(Contact c: cons){
        c.FirstName = c.Sig_Other_First_Name__c;
        c.LastName = c.Sig_Other_Last_Name__c;
}
    

 

When the trigger code finishes executing, the Contact 'c' in the trigger will be inserted into the DB with your values.  

 

Success,

 

JeffM

 

 

 

 

Jerun JoseJerun Jose

Jeff is right.

 

Unless you are actually trying to insert duplicate contacts into the system, you shouldnt perform an insert in the trigger. The before insert field mapping should suffice.

Ali MeyerAli Meyer

Thanks Jeff and Jerun--that makes a lot of sense.

 

I changed the trigger as you suggested, but unfortunately when I tested it things got a little weird. I inserted a new contact and included sig other information, (to test manually) but when it saved, the original contact I saved just became the sig other contact. So the one I originally tried to create never actually got created.

 

Does this mean I should go back and include the "insert" part of the trigger?

 

And thank you so much for your help!

 

 

Ali MeyerAli Meyer

Just in case you're on the edge of your seat, I figured this out and deployed it into production. Thank you so much for helping me!

This was selected as the best answer
Jeff MayJeff May
Maybe I misunderstood what you are trying to accomplish. When a Contact gets added to your SFDC, are you trying to set that Contact FirstName and LastName fields with the values from your custom text fields "Other_FirstName" and "Other_LastName"? Or arae you trying to create a second contact that has the FirstName and LastName set from the first Contact? Here's an example of my question: A Contact record comes in with LastName="Me" and Other_LastName="ALast", and Other_FirstName='AFirst". Do you want to end up with 2 Contact records: 1 with a LastName="Me" 1 with a LastName='ALast" and FirstName="AFirst" Or do you want to update the original Contact record (that had LastName="Me") with new values of LastName='ALast" and FirstName="AFirst"
Ali MeyerAli Meyer

I'm hoping to do the second thing in your example. For instance, if I insert a record with:

  • FirstName: John
  • LastName: Smith
  • Spouse_First: Sarah
  • Spouse_Last: Jones

Then I want it to create two contact records within the same account, one for John Smith and one for Sarah Jones. Make sense?