+ Start a Discussion
ShadowlessKickShadowlessKick 

RecordTypeID Invalid in Test Class

I have an Apex Controller that works but I cannot get the test class to compile. It blows with the following error.

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user: : [RecordTypeId]

The user I am using is authorized to record types "CAD", "Install"  

Name      Id
CAD        012W00000004S1FIAU
Install     012W00000004S1AIAU

When I run the test scripts I pull out the user ID and query for the record type I want to use.

The Debug logs are returning the correct ID's
User: 00550000001J8elAAC
RecordTypeID: 012W00000004S1AIAU

The hardcoded record type id does not work either.

The Test Script Looks like this:

@isTest
public with sharing class createProjectFromProject_Test {
// Method for testing
     public static testmethod void testCreateProjectFromProject_Test()   
     {     
     //Start Test
     Test.startTest();     
           
     // Get User Settings       
     User UserSetting = [select name, TimeZoneSidKey,
          LocaleSidKey,
          EmailEncodingKey,
          ProfileId,                            
          LanguageLocaleKey
          from User where name = 'Administrator' LIMIT 1];
         
     // Create Test User       
     User theTestUser1 = new User (FirstName = 'Test',
                            LastName = 'User',
                            Username = 'Test.User@ki.com',
                            Email =  'Test.User@ki.com',
                            Alias = 'TestUser',
                            TimeZoneSidKey = UserSetting.TimeZoneSidKey,
                            LocaleSidKey = UserSetting.LocaleSidKey,
                            EmailEncodingKey = UserSetting.EmailEncodingKey,
                            ProfileId = UserSetting.ProfileId,
                            LanguageLocaleKey = UserSetting.LanguageLocaleKey);
                            insert theTestUser1;
                           
                           
                            Id theAdminUserID = [SELECT Id FROM  User where name = 'Administrator' LIMIT 1].id;
                           
// Create Account   
      Account theTestAccount = new Account(Name = 'Test Opp. To Project  Account', Type = 'End User', Site = 'Test Account City', MARKET_CODE__C = 'University & College Market – 1', SALES_REP__C = 'Zakowski, Ted', STATE__C = 'WI', DISTRICT__C = '125',  Region__c = '1', FIELD_SALES_REP_NUMBER__C = '12542', DISTRICT_NAME__C = 'KI-New York', DISTRICT_TYPE__C  = 'Direct',   Owner = theTestUser1);       
      insert theTestAccount;
        
      RecordType theRecordType = [SELECT Id FROM RecordType where Name = 'Install' and SobjectType = 'tenrox__c' Limit 1];
  

// Create Project     
       Tenrox__c TheOrigProject = new Tenrox__c (Name =  'Test',
      Description__c =  'Test',
      Start_Date__c = Date.Today(),
      Account__c = theTestAccount.id,
     Finish_Date__c =  date.valueOf('2039-12-31'),
     OwnerId = theAdminUserID,
     RecordTypeId = therecordtype.id);       
        insert TheOrigProject;       
       theProjectID = TheOrigProject.id;
 
       /*****************************************************************/       
       /*                   Data Setup Complete                         */       
       /*****************************************************************/                
       /*****************************************************************/       
       /*                       Test Scenario                          */       
       /*****************************************************************/
      // Test Controller        
  
      ApexPages.StandardController controller1 = new ApexPages.StandardController(TheOrigProject);
 
   createProjectFromProject controller2 = new createProjectFromProject(controller1);
   
     controller2.autorun();
   
  PageReference pageRef = Page.createProjectFromProject;
  pageRef.getParameters().put('id', String.valueOf(TheOrigProject.id));
  Test.setCurrentPage(pageRef);
  controller2.autorun();
  
  Test.stopTest();
    
   }
}
Best Answer chosen by ShadowlessKick
crop1645v2crop1645v2
Are you saying that the DML exception occurs on the statement: insert TheOrigProject; ?

If yes, the running user of the test is you, not theAdminUserId.  You should wrap at least the creation of the Tenrox__c record with a system.runAs(theAdminUserId) {..} if the recordType you want to use is usable only by the User you are creating in the testmethod

All Answers

crop1645v2crop1645v2
The error:

<blockquote>
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user: : [RecordTypeId]
</blockquote>

is telling you that the DML insert of some statement has null for a recordTypeId. You don't indicate which DML statement is failing - is it in the test setup or in the controller2.autoRun() method.  More debug statements on your end should trace where you are losing the recordTypeId.
ShadowlessKickShadowlessKick
The value is in the test setup... RecordTypeId = therecordtype.id.  The id of the record type is there.  I can see it in the debug logs.  It says the record type is not valid for the user but it is when I use it in the front end with the same user.
crop1645v2crop1645v2
Are you saying that the DML exception occurs on the statement: insert TheOrigProject; ?

If yes, the running user of the test is you, not theAdminUserId.  You should wrap at least the creation of the Tenrox__c record with a system.runAs(theAdminUserId) {..} if the recordType you want to use is usable only by the User you are creating in the testmethod
This was selected as the best answer
ShadowlessKickShadowlessKick
Thank you.  That did the trick.