+ Start a Discussion
mike_merinomike_merino 

Set MailingCountryCode in before contact trigger

We are integrated to a sql server database with some BAD data; now before I even begin I know the correct answer is "fix the $%^&*( data", but here is my problem

in the sql server system you can have a valid state and country is null
in our integration system, we map these two values to the new SFA country and state picklist values.

Occasionally we'll get an error like this:
Upsert Error for record id : 181576 - Error in fields: OtherState - 
    Error Message: A country must be specified before specifying a state value for field:
    Owner State/Province. Error Code: FIELD_INTEGRITY_EXCEPTION

and again, the "right" answer is fix the source data, but I thought I could be clever and use the SFA order of execution
3.Executes all before triggers.
4.Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules. The only system validation that Salesforce doesn't run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.

so I built a before trigger like this
==============================
trigger ContactBefore on Contact (before insert, before update) {
Trigger_on_off__c TriggerOnOff = Trigger_on_off__c.getInstance();

if (TriggerOnoff.Contact__c == true)
{
  
   for (Contact c: Trigger.new)
   {

     system.debug('### 2 '+c.MailingState+'--'+c.MailingCountry+'--'+c.MailingCountryCode);
     if (c.MailingCountry==null || c.mailingcountrycode==null)
     {
      c.MailingCountry='US';
      c.MailingCountryCode ='US';
     }
      if (c.otherCountry==null || c.otherCountrycode==null)
     {
      c.OtherCountry ='US';
      c.OtherCountryCode ='US';
     }
   }
}

}========================
here is what the debug log shows

12:38:41.402 (402058000)|CODE_UNIT_FINISHED|VerifyContactMPSEmail on Contact trigger event BeforeUpdate for [003C000001EeyZ0]
12:38:41.408 (408462000)|CODE_UNIT_STARTED|[EXTERNAL]|01qL00000004UEi|ContactBefore on Contact trigger event BeforeUpdate for [003C000001EeyZ0]
12:38:41.408 (408659000)|SYSTEM_METHOD_ENTRY|[22]|Trigger_On_Off__c.getInstance()
12:38:41.408 (408807000)|SYSTEM_METHOD_EXIT|[22]|Trigger_On_Off__c.getInstance()
12:38:41.408 (408867000)|SYSTEM_METHOD_ENTRY|[27]|LIST<Contact>.iterator()
12:38:41.408 (408891000)|SYSTEM_METHOD_EXIT|[27]|LIST<Contact>.iterator()
12:38:41.408 (408904000)|SYSTEM_METHOD_ENTRY|[27]|system.ListIterator.hasNext()
12:38:41.408 (408918000)|SYSTEM_METHOD_EXIT|[27]|system.ListIterator.hasNext()
12:38:41.408 (408968000)|SYSTEM_METHOD_ENTRY|[29]|MAP<Id,Contact>.get(Object)
12:38:41.408 (408994000)|SYSTEM_METHOD_EXIT|[29]|MAP<Id,Contact>.get(Object)
12:38:41.409 (409081000)|SYSTEM_METHOD_ENTRY|[30]|System.debug(ANY)
12:38:41.409 (409103000)|USER_DEBUG|[30]|DEBUG|### 2 CA--null--null
12:38:41.409 (409110000)|SYSTEM_METHOD_EXIT|[30]|System.debug(ANY)
12:38:41.409 (409204000)|SYSTEM_METHOD_ENTRY|[27]|system.ListIterator.hasNext()
12:38:41.409 (409220000)|SYSTEM_METHOD_EXIT|[27]|system.ListIterator.hasNext()

so you can see; the value of mailingState is CA (not null) and the value of mailingCountryCode is null
BUT the two statements in the IF statement are NOT executed ; why???
ShashForceShashForce
Hi,

Try addind a line like...

system.debug(c.MailingCountry+' '+c.MailingCountryCode);

...after the IF(), and you will notice that they are indeed being assigned, but there is no DML operation which occurs on the Contact to pre-populate these fields. Looks like the only workaround would be the painful one, cleaning data.

Thanks,
Shashank