Skip to main content
I created a trigger to update a custom field (Mailing County) before insert of a Contact.  The trigger takes the MailingPostalCode entered by the user and finds the County in which that zip code is found.  This is done via a custom object (Zip_Code__c) in which the Postal Codes are stored along with their respective Counties.

I would love any feedback on how to make this trigger better, but specifically I am confused because one of my test methods is not working (TestContactWithValidZip).  This particular test gets caught by my addError clause of my trigger.

Trigger:

trigger CountyLookupByZip on Contact (before insert) {

for (Contact c : Trigger.New) {

if(c.MailingPostalCode != null) {

List<Zip_Code__c> contactCounty = new List<Zip_Code__c>();

contactCounty = [SELECT Id,

County__c,

Postal_Code__c

FROM Zip_Code__c

WHERE Postal_Code__c = :c.MailingPostalCode];

if(contactCounty.size() != 0){

c.Mailing_County__c = contactCounty[0].County__c;

} else {

c.MailingPostalCode.addError('Postal Code not found. Please ensure the postal code is a valid Virginia zip code.');

}

}

}

}

Test Clas:

@isTest

public class TestCountyLookupByZip {

@isTest

public static void TestContactWithNullZip() {

// Test data setup

// Create a contact without a Mailing Postal Code

// and check to ensure County is also null

Contact contactNullZip = new Contact(LastName = 'Test');

insert contactNullZip;

// Perform Test

System.assertEquals(contactNullZip.Mailing_County__c, null);

}

@isTest

public static void TestContactWithValidZip() {

// Test data setup

// Create a contact with a valid VA Mailing Postal Code

// and check to ensure County is updated correctly

Contact contactValidZip = new Contact(LastName = 'Test', MailingPostalCode = '20101');

insert contactValidZip;

// Perform Test

System.assertEquals(contactValidZip.Mailing_County__c, 'Loudoun');

}

@isTest

public static void TestContactWithInvalidZip() {

// Test data setup

// Create a contact with an invalid Mailing Postal Code

// and throw an error

Test.startTest();

try {

Contact contactInvalidZip = new Contact(LastName = 'Test', MailingPostalCode = '17602');

insert contactInvalidZip;

// Perform Test

} catch(Exception e) {

System.Assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));

System.Assert(e.getMessage().contains('Postal Code not found. Please ensure the postal code is a valid Virginia zip code.'));

}

Test.stopTest();

}

}

 
답변 6개
  1. 2020년 1월 22일 오전 1:41
    Hi Stephen

    You still get the error as you have not re-queried the record that you have created.  

    If you go to this thread  https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IKZFQA4

    and search for "why we grab the written record in our Apex Test Classes" you will see a post that will explain what is required and why.

    But if you want the short version, update your method to:

    @isTest

    public static void TestContactWithValidZip() {

    // Test data setup

    // Create a contact with a valid VA Mailing Postal Code

    // and check to ensure County is updated correctly

    Zip_Code__c zipCode = new Zip_Code__c(Postal_Code__c = '20101', County__c = 'Loudoun');

    insert zipCode;

    Contact contactValidZip = new Contact(LastName = 'Test', MailingPostalCode = '20101');

    insert contactValidZip;

    Contact insertedContact = [SELECT Id, Mailing_County__c FROM Contact WHERE Id = :contactValidZip.Id LIMIT 1];

    // Perform Test

    //general format for asserts is expected v actual so

    System.assertEquals('Loudoun', insertedContact.Mailing_County__c, );

    }

    Regards

    Andrew
0/9000