I'm trying to write a trigger and having some real trouble in making it work.
Background: we adopted the Country/State picklist "Feature" last year. All the cleanup wasn't done. We have some records that currently violate the validation rule.
Trigger: my trigger is one that updates a contact when it's lookup account is changed. Basically, I have a field on the account and contact that mirrors, with the one at the account level taking prescedence. (Trigger on Account (after insert, after update))
Problem: If the contact has invalid country/state values, then the field validation exception is thrown when using DML statement.
update accounts;
This isn't a problem for one-offs, but has obvious problems during bulk update/insert.
Question: So, I've decided to allow a partial update. Here's the code (pretty boiler plate from what I've seen):
Database.SaveResult[] lsr = Database.update(updateContacts, false);
for(Database.SaveResult sr : lsr){
if (!sr.isSuccess()) {
for(Database.Error err : sr.getErrors())
{
System.debug('The following error has occurred' + sr.getId());
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
}
}
Now, when I implement this, the field validation exception is no longer thrown (or is handled somehow) and all the records update without fail.
Does this sound right? Is there a way that the database class somehow avoids the validation error that dml statements are subject to?
Any advice would be helpful. Thanks in advance.