0

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 Class:

@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());
        
        
    }
}
1

Most likely, this is where your error is:

    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;

You are failing on a Filter validation rule for a lookup field

teacher.id references a Contact record that you inserted earlier in your testmethod. There is most likely some additional field you need to set on that record to allow the Class__c to be inserted.

Look at the filter validation rules on the Class__c.Instructor___c field to see what you need to add in this line:

Contact teacher = new Contact(FirstName = 'Teacher', LastName = 'Teaher', Email = 'teacher@teacher.com');
    
2
  • Thank you so much @cropredy, as you suggested there was a field with a data type of Checkbox that had to be set to True on the Contact object, when I added that to our Teacher record it worked. – Abara Jan 25 at 4:01
  • great - as a service to the Community, you should mark this answer as a solution – cropredy Jan 25 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.