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
Eli Flores, SFDC DevEli Flores, SFDC Dev 

Problem with testing record types

I'm running into a baffling problem in my test class. I can't figure out why it keeps faiing the validation check on what should be relatively straightforward code.

 

It's got to be something simple I'm just not seeing.

 

 

Here's the relevant code:

 

        //create test contact
        testContact.Account = testAccount;
        testContact.LastName = 'Test Last Name';
        testContact.RecordType = [select Id from RecordType where Name = 'Team Contacts' and SobjectType = 'Contact'];       
        System.debug('Record type: '+testContact.RecordType);
        insert testContact;


        //create test time record
        testTimeClock.Start_Time__c = date.valueOf('2011-12-20T23:01:01Z');
        testTimeClock.End_Time__c = date.valueOf('2011-12-20T23:45:01Z');
        testTimeClock.Rate_My_Day__c = 'I had a great day!';
        testTimeClock.Team_Member_Employee__c = testContact.Id;
        insert testTimeClock;

 Now the testTimeClock.Team_Member_Employee__c field has a lookup filter that requires the contact record to of the Team Contacts record type.

 

Whenever I run this code, it fails on the insert testTimeClock; with the following message:

 

08:17:46.303 (303452000)|EXCEPTION_THROWN|[27]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Team_Member_Employee__c]
08:17:46.309 (309540000)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Team_Member_Employee__c]

 

If there's someone who can help me see the error of my way, i'd be v. appreciative.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

You want to be writing

 

testContact.RecordTypeId = [select Id from RecordType where Name = 'Team Contacts' and SobjectType = 'Contact'].Id; 

All Answers

Andy BoettcherAndy Boettcher

I see you have a debug statement in there - in the log, is the right RecordType being outputted?

 

From coding my own test classes - looking at your code I would do two things to try and get around whatever may be tripping you up:

 

1)  Pull the SOQL out of the inline variable set:

 

RecordType rtTeamContacts = [select....];

 

2) Reference the RecordType variable in your Contact object

 

//create test contact
...
...
testContact.RecordTypeId = rtTeamContacts.Id;

 

 

 

 

 

Ritesh AswaneyRitesh Aswaney

You want to be writing

 

testContact.RecordTypeId = [select Id from RecordType where Name = 'Team Contacts' and SobjectType = 'Contact'].Id; 
This was selected as the best answer
Eli Flores, SFDC DevEli Flores, SFDC Dev

Thanks all... It was the assignment to RecordType instead of RecordTypeId that was causing the problem.