I am moving an Apex Trigger from Sandbox to Production and this is the error I am getting in the Apex Test:

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: 0120v000000TPT9AAO: [RecordTypeId] Stack Trace: Class.RequireRejectionCommentTest.generateAndSubmitObject: line 166, column 1 Class.RequireRejectionCommentTest.testApprovalWithoutComment: line 49, column 1

It's telling me as the Salesforce Admin I do not have access to this record but I do I checked and I have access to these record types.

This is the code right here:

testBatchIS.add(new Case(Hours__c=15.12,RecordTypeId='0120v000000TPT9AAO',AccountId='0017000001XpguKAAR',
                           Status='New',Concession_Category__c='Scope Change',

I think it might have to do with the way I reference the Record Type as an ID.

instead of referencing it as RecordTypeID how would I reference it by the RecordTypeName instead?

  • Is the RecordTypeId referred in your test class valid in Production? Those are different in sandboxes. Aside from this, you may also like to verify if the Account with the Id specified exists or not.
    – Jayant Das
    Apr 24, 2019 at 13:30

2 Answers 2


Below is one the syntax to get it dynamically and then pass it into your apex code

Id recTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Core Deal').getRecordTypeId();

Also dont hard code account id, create an account and pass the ID dynamically


You are most likely facing this issue because of this in your test class : RecordTypeId='0120v000000TPT9AAO'. Record Ids are not same across sandboxes and are different. So it seems this Record Type Id doesn't exist in Production at all.

You should be fetching the RecordTypeId based on what you want to create in your test class. There are couple of ways to do so, you can refer DescribeSObjectResult Class or RecordTypeInfo Class to look at those.

Aside from this, you should also not hard code record ids in your test class, instead create relevant test data within the test class and refer those. You can take a look at How do I write an Apex unit test? to get more details around writing unit tests.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .