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
TemesgennnTemesgennn 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION

We did a minor modification to our code in the If/Else statement in our class, now I am trying to reach 100% test coverage. As you can see in my test class below, i inserted a new contact as coTeacher and a new class as newCLass and populated the fields with the new contact Ids in newClass, but my test coverage is still at 84%, I am getting the following error:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, This person is not a qualified instructor. Please select a qualified instructor.: [Instructor__c]
What am I missing? any help would be much appreciated.

public class ClassSelectorController {
  public List<Class__c> classes { get; set; }
  String dayFormat = 'MM/DD';
  public Id classId { get; set; }
  public String userEmail { get; set; }
  public String userProfileId;
  public Profile gwCommunityProfile;
  public Id classType { get; set; }

  public ClassSelectorController() {
    userEmail = UserInfo.getUserEmail();
    userProfileId = UserInfo.getProfileId();
    gwCommunityProfile = [
      SELECT Id, Name
      FROM Profile
      WHERE Name = 'GW Community User'
      LIMIT 1
    ];
    System.debug(gwCommunityProfile);
    System.debug(gwCommunityProfile.Id);
    System.debug(userProfileId);
    getClasses();
  }
  public void getClasses() {
    // Limit visibility to GW community user
    if (userProfileId != gwCommunityProfile.Id) {
      classes = [
        SELECT
          id,
          name,
          class_nights__c,
          start_date__c,
          end_date__c,
          status__c,
          class__c,
          instructor__r.email,
          RecordTypeId
        FROM class__c
        WHERE status__c = 'Active'
      ];
      System.debug(classes);
    } else {
      classes = [
        SELECT
          id,
          name,
          class_nights__c,
          start_date__c,
          end_date__c,
          status__c,
          class__c,
          instructor__r.email,
          RecordTypeId
        FROM class__c
        WHERE
          status__c = 'Active'
          AND (instructor__r.Email = :userEmail
          OR Co_Teacher__r.Email = :userEmail
          OR Teachers_Assistant__r.Email = :userEmail)
      ];
      System.debug(classes);
    }
  }

  public Pagereference newPage() {
    Id cohortRecordTypeId = Schema.SObjectType.class__c.getRecordTypeInfosByName()
      .get('Cohort')
      .getRecordTypeId();
    String url = '/apex/BWRoster?id=' + classId;
    if (classType == cohortRecordTypeId) {
      url += '&type=Cohort';
    }

    return new PageReference(url);
  }
}


Test Coverage

@isTest
public class ClassSelectorTest {
    
    static testMethod void testGetClassesGWProfile() {    
        
        Profile gwCommunityProfile = [SELECT Id FROM Profile WHERE Name = 'GW Community User' LIMIT 1];
        Contact teacher = new Contact(FirstName = 'Teacher', LastName = 'Teaher', Email = 'teacher@teacher.com');
        insert teacher;

        Contact coTeacher = new Contact(FirstName = 'co-teacher', LastName = 'co-teacher', Email = 'co-teacher@co-teacher.com');
        insert coTeacher;

         // Contact assistant = new Contact(FirstName = 'assistant', LastName = 'asistant', Email = 'assistant@assistant.com');
         // insert assistant;


        // User gwCommunityUser = new User(FirstName='Teacher', LastName='Teacher', ContactId = teacher.Id, Email='teacher@teacher.com', ProfileId= gwCommunityProfile.Id);
        // insert gwCommunityUser;
    
        // System.debug(gwCommunityUser);

        class__c newClass = new class__c(name = 'Test Class', Instructor__c = teacher.Id, Co_Teacher__c = coTeacher.Id,
        class_nights__c = 'Monday / Wednesday', start_date__c = date.today() - 10, end_date__c = date.today() + 45);
        insert newClass;

        

        ClassSelectorController pc = new ClassSelectorController();
        pc.userEmail = 'teacher@teacher.com';
        
        pc.userProfileId = gwCommunityProfile.Id;
        
        pc.getClasses();
        
        System.assertEquals(1, pc.classes.size());
    }
 @isTest
  public static void testURLIsCohort(){
    String CohortRecordTypeId = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
    class__c newClass = new class__c(name = 'Test Class', RecordTypeId = CohortRecordTypeId, class_nights__c = 'Monday / Wednesday', start_date__c = date.today() - 10, end_date__c = date.today() + 45);
    insert newClass;
    
    CLassSelectorController gwClass = new ClassSelectorController();
    gwClass.classId = newClass.Id;
    gwClass.classType = newClass.RecordTypeId;
    PageReference rosterPage = gwClass.newPage();
    System.assertEquals('/apex/BWRoster?id='+newClass.Id + '&type=Cohort',rosterPage.getUrl());
        
        
    }
}

​​​​​​​
Best Answer chosen by Temesgennn
SwethaSwetha (Salesforce Developers) 
HI Temesgennn,
Based on the error message, you need to check to see if any filters are defined on the object and make sure the values passed in the test class records are adhering to the filter criteria.
See https://help.salesforce.com/articleView?id=000322729&type=1&mode=1 for more details.

You can add a System.debug to see what the record looks like that is being inserted in the test class.

Related: https://salesforce.stackexchange.com/questions/212457/field-filter-validation-exception-but-there-is-no-field-with-a-filter
https://salesforce.stackexchange.com/questions/135839/field-filter-validation-exception-when-ever-i-am-trying-to-popluate-some-value

​​​​​​​Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Temesgennn,
Based on the error message, you need to check to see if any filters are defined on the object and make sure the values passed in the test class records are adhering to the filter criteria.
See https://help.salesforce.com/articleView?id=000322729&type=1&mode=1 for more details.

You can add a System.debug to see what the record looks like that is being inserted in the test class.

Related: https://salesforce.stackexchange.com/questions/212457/field-filter-validation-exception-but-there-is-no-field-with-a-filter
https://salesforce.stackexchange.com/questions/135839/field-filter-validation-exception-when-ever-i-am-trying-to-popluate-some-value

​​​​​​​Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
This was selected as the best answer
TemesgennnTemesgennn
Thank you so much Swetha!