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:

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

                //Outputs: 'Angola'
                System.Debug(c.MailingCountry);

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

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

Failing Test (Also fails when creating record manually):

@isTest
public with sharing class TestSetRegion {
    public static testMethod void TestSetRegionContactUpdate(){     
        Contact c = new Contact(LastName='x',MailingCountry='Angola');
        insert c;
        System.assertEquals('Africa',c.Region__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 Jan 27 '16 at 18:01
  • Throw an exception if a Contacts MailingCountry is null, or empty. – battery.cord Jan 27 '16 at 18:43
  • 1
    Are you using State & Country picklists in your system? If you are, you should probably use MailingCountryCode. – smckitrick Jan 27 '16 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. – techbusinessman Jan 27 '16 at 20:01
up vote 3 down vote accepted

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];
    System.assertEquals('Africa',cRes.Region__c);

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 – Matthew Poer Sep 5 at 13:33

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.