I have a before insert trigger to update a custom field on the Contact object, Region__c. Debugging confirms that the field is set to the correct value on the sObject, but both the test asserting the field is set to the correct value and manually creating a new Contact shows that the field is still blank (after the transaction has completed.) However, the value is set correctly if the record is only updated (not inserted.) The trigger doesn't seem to be throwing an error but the field is still blank rather than the correct value. I've also confirmed the following:

  • There are no other triggers writing to the field on the Contact object.
  • There are no workflow rules writing to the field on the Contact object.


trigger SetRegion on Contact (before insert, before update) {
    for (Contact c :  Trigger.New){
        try {
            if (c.MailingCountry != null && c.MailingCountry != ''){

                //Outputs: 'Angola'

                //Returns: 'Africa' (Correct)
                c.Region__c = RegionHelper.GetRegion(c.MailingCountry);

                //Outputs: 'Africa' (Correct)
        catch(Exception e){
            System.Debug('Error setting region.');

Failing Test (Also fails when creating record manually):

public with sharing class TestSetRegion {
    public static testMethod void TestSetRegionContactUpdate(){     
        Contact c = new Contact(LastName='x',MailingCountry='Angola');
        insert c;
        delete c;

Test result: "System.AssertException: Assertion Failed: Expected: Africa, Actual: null"

Any insight would be appreciated, hoping this is just a newbie mistake :)

  • 1
    Post RegionHelper
    – EricSSH
    Commented Jan 27, 2016 at 18:01
  • Throw an exception if a Contacts MailingCountry is null, or empty. Commented Jan 27, 2016 at 18:43
  • 1
    Are you using State & Country picklists in your system? If you are, you should probably use MailingCountryCode.
    – smckitrick
    Commented Jan 27, 2016 at 18:49
  • If, as you've shown, c.Region__c is has the correct value in the debug then there is almost certainly something else updating the value after this trigger. Have you double checked all contact before/after trigger? All workflows? All process builder flows? All headless flows? I know you said so in the first sentence but you may want to check one more time. Commented Jan 27, 2016 at 20:01
  • c.MailingCountry != null && c.MailingCountry != '' can be substituted with String.isNotBlank(c.MailingCountry) Commented Jun 15, 2020 at 9:53

1 Answer 1


well, one issue is with your testmethod - you need to requery the Contact to get the value set in the trigger

    Contact c = new Contact(LastName='x',MailingCountry='Angola');
    insert c;
    Contact cRes = [select id, region__c from Contact where id = :c.id];

also, although not related to your issue per se, try-catch in your before trigger needs to do an addError on the offending record, unless eating the error is intended.

  • This seems so backwards to me, that the trigger is firing but the test method doesn't "retrieve" or "refresh" the object's updated field. There's no method to re-load the already instantiated object from the database, so we really do have to re-query to find the updated value. It seems to me that a lot of trigger-based things are going to be updating field values, so a lot of tests are going to check for that. Had to really dig for this answer, thanks @cropredy Commented Sep 5, 2018 at 13:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .