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
Lionel KamdemLionel Kamdem 

Moving Apex Trigger From Sandbox To Production And Getting Error During Apex Testing

Hi 

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',                     Explanation_for_Business_Reason_Choice__c='stuff'));

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? 
Best Answer chosen by Lionel Kamdem
Ahmed Ansari 13Ahmed Ansari 13
For this You can Use 
Replace 'DeveloperNameOfRecordType' with Case Recortype Name
Id RecordTypeIdCase= Schema.SObjectType.Case.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();

and use RecordTypeIdCase in replace of '0120v000000TPT9AAO'

All Answers

Ahmed Ansari 13Ahmed Ansari 13
For this You can Use 
Replace 'DeveloperNameOfRecordType' with Case Recortype Name
Id RecordTypeIdCase= Schema.SObjectType.Case.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();

and use RecordTypeIdCase in replace of '0120v000000TPT9AAO'
This was selected as the best answer
Raj VakatiRaj Vakati
Change your code like below 
 
Account a = new Account();
a.Name ='Demo';
insert a ; 

Id RecordTypeIdCase= Schema.SObjectType.Case.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();



testBatchIS.add(new Case(Hours__c=15.12,RecordTypeId=RecordTypeIdCase,AccountId=a.Id,  
                             Status='New',Concession_Category__c='Scope Change', 
							 Explanation_for_Business_Reason_Choice__c='stuff'));

 
Deepali KulshresthaDeepali Kulshrestha
Hi Lionel,

You are most likely facing this issue because of this in your test class: RecordTypeId='0120v000000TPT9AAO'. Record Ids 

are not the 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 a couple of ways to 

do so, you can refer to the following links to get those done:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject_describe.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_RecordTypeInfo.htm

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 please refer to the following link:
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unit_tests.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha