+ Start a Discussion
Peter BölkePeter Bölke 

Cross reference id executing apex class with anonymous code


i try to execute an apex class with anonymous code during scratch org setup via sfdx-cli. But it fails with following error.
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:
Is it executed with admin rights so the recordtype should be accessible.

What might be the issue?

VinayVinay (Salesforce Developers) 
Hi Peter,

Profiles have record type settings. This error means that the user has a profile for which that record type is not enabled.

You need to make sure the profile you're using has access to the desired record type, or that the value is being set in a "without sharing" class.

Also, the record type ID ended up being different in the sandbox than it was in Production. Please check the id's as well.

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Vinay Kumar