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
GoForceGoGoForceGo 

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY when running TestMethod during install

   
In a testMethod, I have following code:

Account_Auditor__c Account_auditor = new Account_Auditor__c(Account__c = a.id, auditor__c = u.id, Audit_Type__c = 'Garbage');
insert Account_auditor;
The intent is to execute the following trigger and give the "Auditor" read rights to the Account .

The testMethod runs fine in developer org, it croaks in the org that it is being installed it - it croaks on the Insert statement with the message:

caused by: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id

trigger grantAuditorReadAccessToAccount on Account_Auditor__c (after insert) {

Account_Auditor__c[]  Account_auditor_list = trigger.new;

for (Account_Auditor__c Account_auditor: Account_auditor_list) {
AccountShare ashare = new AccountShare();
ashare.AccountAccessLevel = 'Read';
ashare.OpportunityAccessLevel = 'None';
ashare.CaseAccessLevel = 'None';
ashare.AccountId = Account_auditor.Account__c;
ashare.UserorGroupId = Account_auditor.Auditor__c;
Insert ashare;
}
}

I suspect it has something to do with the user rights - I am fetching an active user from the database in the test method (u.id). In the org that I am installing, I have two users, one is system admin, another is a test user I created, so that i could install. I assume I am installing as a


Best Answer chosen by Admin (Salesforce Developers) 
okaylah52okaylah52
I encountered this exception too when trying to transfer an opportunity to another user. Make sure the profile and role can access the object.

Hope this helps.

All Answers

A_SmithA_Smith
Is the Account's sharing model in the installing org is set to "Private"?
GoForceGoGoForceGo
Yes it is set to private, both in development org and the test org - that's why I need the sharing rule.


GoForceGoGoForceGo
I meant it is set to private in development org and the org where I am installing the App.


A_SmithA_Smith
I bet it has something to do with the user's profile you are setting the test.  Do these users have profile CRUD access enabled to account, contact, opp, and case?

One other possibility - did you by chance set any access controls on the API when you uploaded the package?  It's a setting on the package version.
GoForceGoGoForceGo
That's the most likely explaination - user's profie (I just the first active user from the database, since testMethods can't create the users).

However, I have only two users in my place where I am installing the Application. One is Admin. The other one does seem to have CRUD access to account, contact, opportunity and case (well no delete access on case).

No API restrictions.

I seemed to get around the problem by getting the Admin user instead of the first active user (i.e not getting the other user)


A_SmithA_Smith
What happens if you give the other user Modify All Data on their profile and then try and install the app?  It would be interesting to see if you still get the same issue.
okaylah52okaylah52
I encountered this exception too when trying to transfer an opportunity to another user. Make sure the profile and role can access the object.

Hope this helps.
This was selected as the best answer
NaniNani

Hi,

 

I'm new to SF and my first assignment is to write Apex code for some requirements.

The scenario is complex but I'll try to simplify it like this:

Person X creates an Opportunity and changes the Owner to Y. But, X would still like to view the Opportunity. We know that whenever the Owner of a record is changed then all the sharing rules are deleted. So, I'm trying to write an Apex code in 'After Update' trigger of Opportunity to create a record in Opportunity_Share object with Parent Id = Opty Id and UserOrGroupId = X Id. But, I'm getting INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY error.

 

Because X is no more the owner of the record, I guess this error is shooting up.

 

Is there a way I can run the code as Admin which makes the Opty accessable all the time irrespective of who is modifying the Opty?

 

Thanks,

Nani.

Bharat.VHSBharat.VHS

Hi Nani,

 

Did u get the solution of your problem as i m facing the same issue. Please reply if you have any information on this.

AthiSachiAthiSachi
Hi @Nani and @bharat,

Did you guys find any solution. Please reply..Thanks