Here's a simple test class that illustrates an issue I'm having with a test class that I have been putting together:
@isTest
public class ForeignKeyConstraintTest
{
@isTest
public static void testReference(){
list<Contact> cs = [
SELECT Id
FROM Contact
WHERE Id = '003p0000006PLuG' ];
list<Opportunity> op = [
SELECT Id
FROM Opportunity
WHERE Id = '006p0000004w3Q8' ];
insert new OpportunityContactRole(
ContactId = '003p0000006PLuG'
, OpportunityId = '006p0000004w3Q8' );
list<OpportunityContactRole> rs = [
SELECT Id
, ContactId
, OpportunityId
FROM OpportunityContactRole ];
System.assertEquals( 0 , cs.size() );
System.assertEquals( 0 , op.size() );
System.assertEquals( 1 , rs.size() );
System.assertEquals( '003p0000006PLuG' , rs[0].ContactId );
System.assertEquals( '006p0000004w3Q8' , rs[0].OpportunityId );
}
}
This works fine in the sandbox, and since this test class is not SeeAllData=true, so I assumed it would work in production as well, since there shouldn't be any data dependencies.
Unfortunately, when the test runs on a deployment to production, I've been getting an error:
INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY
Presumably this is because the referenced records are in the sandbox org, and even though the referenced records aren't visible themselves in the test class, they are somehow available for reference.
Before I change my approach to testing, I wanted to ask the group:
- Is there any way to get the referenced record loaded into the test environment with the correct Id value?
I've tried Test.loadData()
, but it looks like that doesn't preserve Ids.
- Is there some test execution setting to allow lookup fields to have Ids that ignore these foreign key constraints?