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
augiewazaugiewaz 

TestMethod Error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY

We have a setup where there is a custom object called Competitor.
On our Opportunity page, we have a lookup table field called Primary Competitor (a required field) to the Competitor object called Primary Competitor.
 
I have written a trigger on the Opportunity object that does some evaluations when an Opportunity is inserted/updated.
 
I wrote up a test method to test the trigger. However, I get the following error:
INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id
 
 
Basically, this is the code of the test method:
Integer size = 5;
List<Opportunity> opp = new List<Opportunity>();
for (Integer i = 0; i < size; i++)  {
   Opportunity o = new Opportunity();
   o.ownerId = '00530000000kFvU';
   o.name = 'Opportunity Test: ' + i;
   o.Primary_Competitor__c = 'a1K50000000Caz4EAC';
   o.accountId = '0015000000L3FPk';
   o.closeDate = System.today() + 150;
   o.stageName = 'Quotation';
   opp.add(o);
}
insert opp; 
 
When I run the test method I get the error listed above. If I comment out the line that adds the Primary Competitor to the Opportunity, then I do not have the error.
 
I've been looking around as to why the error is happening but I'm not having any luck in solving it. Any advice would be appreciated.
 
Thanks in advance
Warren
Best Answer chosen by Admin (Salesforce Developers) 
charleshowardcharleshoward

Hey I just wanted to say that every time I've run into this error is was because I was trying to populate an id into a Lookup field of a record that did not exist.

 

The first time, I ran tests in a developer sandbox, where I had manually created an Account record to use, so the id of the manually created Account was different than the id of the same Account in production.  The test code still had the production id in it.

 

The second time I ran into it, I was auto-filling a Lookup to the Product table with the id of various product records depending on the record type of a custom object record.  The product I was referencing had been created in production after the last time that the sandbox was refreshed, so the referenced product didn't exist in the sandbox.

 

Hope this helps!

All Answers

JimRaeJimRae
Does the owner you are using to create your Opportunity (00530000000kFvU) have access to the Primary Competitor object you are specifying (a1K50000000Caz4EAC)?
See if you can accomplish the same task directly in the UI, create an Opportunity as that user, and try to relate that Primary Competitor. 
augiewazaugiewaz
The Id is actually my own ID. However, your suggestion helped me work out the problem and its now solved.
 
Thanks!!!
JimRaeJimRae

augiewaz wrote:
The Id is actually my own ID. However, your suggestion helped me work out the problem and its now solved.
 
Thanks!!!


Glad to hear it.  You should share your solution, so if anyone else runs into a similar issue they can benefit from your experience.
augiewazaugiewaz

The ID of the Competitor was wrong and this was why I was getting the error.

Put in a valid ID and it worked!

okaylah52okaylah52
I just want to share my experience with this exception during testing.

I create users, accounts, contacts, opportunities (including product line items and prices) in my testMethod routines. I spent almost half a day trying to figure out what caused the exception. Finally I nailed down the problem. The user profiles and user roles can cause the exception. Make sure you set the user's profile and role correctly.
charleshowardcharleshoward

Hey I just wanted to say that every time I've run into this error is was because I was trying to populate an id into a Lookup field of a record that did not exist.

 

The first time, I ran tests in a developer sandbox, where I had manually created an Account record to use, so the id of the manually created Account was different than the id of the same Account in production.  The test code still had the production id in it.

 

The second time I ran into it, I was auto-filling a Lookup to the Product table with the id of various product records depending on the record type of a custom object record.  The product I was referencing had been created in production after the last time that the sandbox was refreshed, so the referenced product didn't exist in the sandbox.

 

Hope this helps!

This was selected as the best answer
oorellanooorellano

Hello, I know this problem has been solved, but I wanted you to know I had the same error "INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY" and my problem was another.

 

In order to create an user on the test method, we need to set a profile to it.

I made the mistake to get the profile like this:

Profile pro = [select id from Profile limit 1];  // So, unfortunately I was getting a profile with insufficient access.

 

Now, I'm getting the profile like this: Profile pro = [select id from Profile where Name = 'System Administrator'];

But the name should change depending of what we need to test.

 

That's all, 

I hope I can help someone with my mistake.

 

Regards, 

Orlando

 

goabhigogoabhigo

That solved my problem. Thanks for sharing knowledge. 

Keep the good work.