I have got an apex trigger that sometimes errors with
System.DmlException: Update failed. First exception on row 0 with id 00Q0800000fhnM2EAI; first error: FIELD_INTEGRITY_EXCEPTION
The reasons for the DmlException vary, they can be the above or things like:
The Phone number must start with "+" or "00" and may only contain digits, space characters, "+", and "-".: []
Please complete "Country" or "State" field.: []
The existing country/territory doesn't recognize the state value for field: Billing State/Province Code: [BillingStateCode]
Please enter State in Physical Address: [BillingState]
There's a problem with this state, even though it may appear correct. Please select a state from the list of valid states.: Billing State/Province: [BillingState]
These look like user input errors so I decided to handle the exceptions gracefully. Here is my apex trigger:
trigger trgUpdateVCountField on V__c (after insert, before delete) {
List<Lead> leadsToUpdate = new List<Lead>{};
List<Account> accountsToUpdate = new List<Account>{};
if(trigger.isDelete) {
for (V__c vis : trigger.old) {
if(vis.Lead__c != null) {
Lead leadToUpdate = [SELECT Id, LV__V_Count__c FROM Lead WHERE Id = :vis.Lead__c LIMIT 1];
double calculatedVCount = [SELECT COUNT() FROM LV__V__c WHERE LV__Lead__C = :leadToUpdate.Id LIMIT 50000];
calculatedVCount = calculatedVCount - 1;
if(calculatedVCount != leadToUpdate.LV__V_Count__c) {
leadToUpdate.LV__V_Count__c = calculatedVCount;
leadsToUpdate.add(leadToUpdate);
}
}
if (vis.Account__c != null) {
Account accountToUpdate = [SELECT Id, LV__V_Count__c FROM Account WHERE Id = :vis.Account__c LIMIT 1];
double calculatedVCount = [SELECT COUNT() FROM LV__V__c WHERE LV__Account__C = :accountToUpdate.Id LIMIT 50000];
calculatedVCount = calculatedVCount - 1;
if(calculatedVCount != accountToUpdate.LV__V_Count__c) {
accountToUpdate.LV__V_Count__c = calculatedVCount;
accountsToUpdate.add(accountToUpdate);
}
}
}
}
else {
for (V__c vis : trigger.new) {
if(vis.Lead__c != null) {
Lead leadToUpdate = [SELECT Id, LV__V_Count__c FROM Lead WHERE Id = :vis.Lead__c LIMIT 1];
double calculatedVCount = [SELECT COUNT() FROM LV__V__c WHERE LV__Lead__C = :leadToUpdate.Id LIMIT 50000];
if(calculatedVCount != leadToUpdate.LV__V_Count__c) {
leadToUpdate.LV__V_Count__c = calculatedVCount;
leadsToUpdate.add(leadToUpdate);
}
}
if (vis.Account__c != null) {
Account accountToUpdate = [SELECT Id, LV__V_Count__c FROM Account WHERE Id = :vis.Account__c LIMIT 1];
double calculatedVCount = [SELECT COUNT() FROM LV__V__c WHERE LV__Account__C = :accountToUpdate.Id LIMIT 50000];
if(calculatedVCount != accountToUpdate.LV__V_Count__c) {
accountToUpdate.LV__V_Count__c = calculatedVCount;
accountsToUpdate.add(accountToUpdate);
}
}
}
}
if(!leadsToUpdate.isEmpty()) { update leadsToUpdate; } // LINE THAT THROWS THE EXCEPTIONS
if(!accountsToUpdate.isEmpty()) { update accountsToUpdate; }
}
The error gets thrown at the update leadsToUpdate;
line. I could wrap that with a try catch but I am unsure on what to put inside the catch section. I would like to log the error with as much detail as possible but I am not sure where or how to do that, I am struggling to find any examples of this.