function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mika ParidaMika Parida 

System.DmlException: Upsert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION

I'm receiving an error when I try to change a field on a custom object. When the field (application status) is changed to "confirmed", a Contact should be created. I'm receiving this error when I change the field: 

Error:Apex trigger LGRApplicationTriggers caused an unexpected exception, contact your administrator: LGRApplicationTriggers: execution of BeforeUpdate caused by: System.DmlException: Upsert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A validation rule is preventing the record from saving: Use one of these records?: []: Class.LGRApplicationTriggerHandler.handleAfterApplicationUpdate: line 134, column 1

We have 6 managed validation rules on the contact object, and I disabled all of them and still received this code. We don't have any other validation rules on any of the other objects that this object relates to. Does anyone have any ideas? Thanks!
Musunuru SurekhaMusunuru Surekha
Hello Mika Parida,

In the trigger handler, check the handleAfterApplicationUpdate method which might be causing the issue. It would be great if you can post the code snippet to futher debug the issue.
Mika ParidaMika Parida
Hi Musunuru Surekha, 

Here's the part of the code that contains the handleAfterApplicationUpdate: 

 public static void handleAfterApplicationUpdate(list<LGR_Application__c> updatedApplications, map<Id, LGR_Application__c> oldApplications)
  {
    if(!LGRApplicationTriggerHandler.run)
      return;

    List<Contact> contacts = new List<Contact>();
    Map<Id, Contact> applicationContactMap = new Map<Id, Contact>();
    List<LGR_Application__c> acceptedApplications = new List<LGR_Application__c>();
    Map<String, LGR_Program_Contact__c> programContacts = new Map<String, LGR_Program_Contact__c>();


I'm not sure if this is helpful, but here's the code around line 134, which is referenced in the error: 

   if(!contacts.isEmpty())
      upsert contacts;

    Map<String, RecordType> lgrProgramContactRecordTypeMap = new Map<String, RecordType>();
    for(RecordType recordType:[select Id, Name from RecordType where sObjectType = 'LGR_Program_Contact__c'])
    {
      lgrProgramContactRecordTypeMap.put(recordType.Name, recordType);
    }

    for(LGR_Application__c application:acceptedApplications)
    {
      application.Contact__c = applicationContactMap.get(application.Id).Id;

      String key = String.valueOf(application.Contact__c) + String.ValueOf(application.LGR_Program_Lookup__c);
      programContacts.put(key, new LGR_Program_Contact__c(Contact__c = applicationContactMap.get(application.Id).Id, LGR_Program__c = application.LGR_Program_Lookup__c, RecordTypeId = lgrProgramContactRecordTypeMap.get(application.Record_Type_Name__c).Id ));


Thank you!