Do you have grants or other reporting requirements based on county? Ever wish you could save your staff time and prevent errors by automatically providing the name of the county using the zip/postal code? This tutorial tells you how.
Summary:
We’ll start by finding the postal codes and counties we need, then we’ll import them into a custom object in Salesforce. After that, we’ll write our trigger and test class!
The Steps:
Step 1
Determine which counties you need and search the web for a database of counties and postal codes. For example, if you only operate in the state of Virginia, download a table of Virginia counties by postal code. You can either download them as a CSV or simply copy and paste them into a new CSV file. I used this website to find the postal codes I needed.
Step 2
Create a custom object to store your postal codes and counties. Name the object Zip Code and create two custom fields:
- Text Field – “Zip Code” to store the postal code
- Text Field – “County” to store the name of the corresponding county
- BONUS! You can also create a text field to store the name of the corresponding city if you’d like to expand this trigger to automatically update the city as well.
Step 3
Use the Data Import Wizard to import the database (CSV file) containing the postal codes and their corresponding counties and cities in step #1.
Step 4
Write the trigger!
trigger CountyLookupByZip on Contact (before insert) {
for (Contact c : Trigger.New) {
if(c.MailingPostalCode != null) {
List<Zip_Code__c> contactCounty = [SELECT Id,
County__c,
Postal_Code__c
FROM Zip_Code__c
WHERE Postal_Code__c = :c.MailingPostalCode
LIMIT 1];
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.');
}
}
}
}
Step 5
Write your test class!
@isTest
private class TestCountyLookupByZip {
@isTest
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
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
System.assertEquals('Loudoun', insertedContact.Mailing_County__c);
}
@isTest
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();
}
}
Have you solved this problem in a different manner? How would you make this trigger better? Share and comment below!