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
Marie Pardun 3Marie Pardun 3 

Please help!! Test Class for Contact Trigger not returning values after insert

My test case for a before insert/update trigger on Contact should be very simple, which means I am missing something obvious...the trigger itself takes the City, State Code, and Zip and pulls the county name from a custom county object.  This trigger is working as intended and populates the county name with no issue.  

What I can't figure out is why my test class does not return a value for the county after the insert.  (The insert code and requery works perfectly when running in an Execute Anonymous window). 

Below is the code for the insert test.  After inserting a new contact, when I try to requery for the new hed__Mailing_County__c value, the value is null, but it does pull the new contact id.  Any help is much appreciated!  :-)
 
@isTest
private class Contact_County_Test {
    static testMethod void TestInsertContact() {
        // Create contact with city, state, zip populated
        Contact con = new Contact(FirstName = 'TestInsert', 
                                  LastName = 'TestContact', 
                                  MailingCity = 'My City', 
                                  MailingStateCode = 'WI', 
                                  MailingPostalCode = '12345',
                                  MailingCountryCode = 'US',
                                  hed__Mailing_County__c = '');
        Insert con;

        //Requery to pull in new county name
        con = [SELECT hed__Mailing_County__c FROM Contact WHERE Id = :con.Id LIMIT 1];

         //Verify County Name is populated correctly
         System.assertEquals('County Name', con.hed__Mailing_County__c);
    }
}

 
Best Answer chosen by Marie Pardun 3
Maharajan CMaharajan C
Hi Marie,

First create county record in test class as like your org data. Then insert the contact record in test class.

Test class won't consider the org actual data so you need to create the county data seperately in test class before contact insert.
 
static testMethod void TestInsertContact() {
       
        //Create the County data here
        //  insert county;
    
        // Create contact with city, state, zip populated
        Contact con = new Contact(FirstName = 'TestInsert', 
                                  LastName = 'TestContact', 
                                  MailingCity = 'My City', 
                                  MailingStateCode = 'WI', 
                                  MailingPostalCode = '12345',
                                  MailingCountryCode = 'US',
                                  hed__Mailing_County__c = '');
        Insert con;

        //Requery to pull in new county name
        con = [SELECT hed__Mailing_County__c FROM Contact WHERE Id = :con.Id LIMIT 1];

         //Verify County Name is populated correctly
         System.assertEquals('County Name', con.hed__Mailing_County__c);
    }



Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Marie,

First create county record in test class as like your org data. Then insert the contact record in test class.

Test class won't consider the org actual data so you need to create the county data seperately in test class before contact insert.
 
static testMethod void TestInsertContact() {
       
        //Create the County data here
        //  insert county;
    
        // Create contact with city, state, zip populated
        Contact con = new Contact(FirstName = 'TestInsert', 
                                  LastName = 'TestContact', 
                                  MailingCity = 'My City', 
                                  MailingStateCode = 'WI', 
                                  MailingPostalCode = '12345',
                                  MailingCountryCode = 'US',
                                  hed__Mailing_County__c = '');
        Insert con;

        //Requery to pull in new county name
        con = [SELECT hed__Mailing_County__c FROM Contact WHERE Id = :con.Id LIMIT 1];

         //Verify County Name is populated correctly
         System.assertEquals('County Name', con.hed__Mailing_County__c);
    }



Thanks,
Maharajan.C
This was selected as the best answer
Marie Pardun 3Marie Pardun 3
Hi Maharajan.C,

Told you it was somthing simple...THANK YOU SO MUCH!  :-)