+ Start a Discussion
Chance AllredChance Allred 

Apex Test Class: Insert failed. INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY

Hello all,

I am having some trouble with an Apex Trigger test class I'm writing. I am using a developer sandbox and am getting the following error:
System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: a0D6A0000015UKi: []

What I am trying to do is update a Lookup field that looks up to a Rate Sheet based on certain criteria. (Rate_Sheet__c = API Name)

I wrote the trigger and had no errors that I could see but when writing the test class it kept giving me the Insufficient Access error. Not really sure what the problem is. Under sharing settings, my org wide defaults gives public read/write access to all objects needed.

Please see the following Apex Trigger and Test Class code:
trigger UpdateRatesheetLookup on Quote (before insert, before update) {

    for (Quote quo : Trigger.new) {
       
        if (quo.TotalPrice < 3000 && quo.Buyout_Type__c == 'FMV') {
            // Sets Rate Sheet Lookup
            quo.Rate_Sheet__c = 'a0D6A0000015UKi';
            
        } else if (quo.TotalPrice < 10000 && quo.Buyout_Type__c == 'FMV') {
            quo.Rate_Sheet__c = 'a0D6A0000015UKj';
            
        } else if (quo.TotalPrice >= 10000 && quo.Buyout_Type__c == 'FMV') {
            quo.Rate_Sheet__c = 'a0D6A0000015UKk';
            
        } else if (quo.TotalPrice < 3000 && quo.Buyout_Type__c == 'Promo FMV') {
            quo.Rate_Sheet__c = 'a0D6A0000015UKl';
            
        } else if (quo.TotalPrice < 10000 && quo.Buyout_Type__c == 'Promo FMV') {
            quo.Rate_Sheet__c = 'a0D6A0000015UKm';
            
        } else if (quo.TotalPrice >= 10000 && quo.Buyout_Type__c == 'Promo FMV') {
            quo.Rate_Sheet__c = 'a0D6A0000015UKn';
            
        } else if (quo.TotalPrice < 3000 && quo.Buyout_Type__c == '$1-out') {
            quo.Rate_Sheet__c = 'a0D6A0000015UKo';
            
        } else if (quo.TotalPrice < 10000 && quo.Buyout_Type__c == '$1-out') {
            quo.Rate_Sheet__c = 'a0D6A0000015UKp';
            
        } else if (quo.TotalPrice >= 10000 && quo.Buyout_Type__c == '$1-out') {
            quo.Rate_Sheet__c = 'a0D6A0000015UKq';
            
        } else {
            quo.Rate_Sheet__c = Null;
        }
    }
}

// Problem is happening at insertion of the myQuote instance.
@isTest
private class UpdateRateSheetLookupTest {
    
    @isTest static void updateQuote() {
        
        // Create an Account & Set Required Fields
        Account myAccount = new Account();
        myAccount.Name    = 'Sample Account';
        insert myAccount;
        
        // Create an Opportunity on the Account & Set Required Fields
        Opportunity myOpportunity = new Opportunity();
        myOpportunity.Name        = 'Sample Opportunity';
        myOpportunity.CloseDate   = Date.today();
        myOpportunity.StageName   = 'Proposal';
        myOpportunity.AccountId   = myAccount.Id; // Relates to id of account above.
        insert myOpportunity;
            
        // Fire the UpdateRatesheetLookup Trigger
        // Create a Quote associated with Opportunity & Set Required Fields
        Quote myQuote          = new Quote();
        myQuote.Name           = 'Sample';
        myQuote.OpportunityId  = myOpportunity.Id; // Relates to id of opportunity above.
        //myQuote.TotalPrice   = 2500;
        myQuote.Buyout_Type__c = 'FMV';
       	insert myQuote; // FLAG: PROBLEM HERE
        
        // Update the Quote
        myQuote.Description = 'Test';
       	update myQuote;
    }
}

Any help with this would be greatly appreciated. 
Thanks in advance!
 
Best Answer chosen by Chance Allred
Aman MalikAman Malik
Hi,
As you are using lots of hard coded Id's in your trigger. Your issue could be because your test method cannot access these data in your org or it could be a security issue.
Please refer below link for help:
http://salesforce.stackexchange.com/questions/12772/why-am-i-getting-insufficient-access-on-cross-reference-entity-error-on-code-tha

Please like the answer and mark it as best if it helps.

Thanks,
Aman

All Answers

Aman MalikAman Malik
Hi,
As you are using lots of hard coded Id's in your trigger. Your issue could be because your test method cannot access these data in your org or it could be a security issue.
Please refer below link for help:
http://salesforce.stackexchange.com/questions/12772/why-am-i-getting-insufficient-access-on-cross-reference-entity-error-on-code-tha

Please like the answer and mark it as best if it helps.

Thanks,
Aman
This was selected as the best answer
Nikhil Verma 6Nikhil Verma 6
Hi Chance, 
As Aman pointed out above that you have used a lot of hard coded values in your trigger. These should be avoided. You should query the relevant records in the trigger and then assign the IDs to the custom field accordingly. Once done, you will have to create the similar data in your test class in order for the test class to be able to return the data by the queries.
In case you want to stick with the hard-coded values, try using SeeAllData=true in your test class. That might resolve the issues. The test class annotation will become:
@isTest(SeeAllData=true)

Hope this helps.
Chance AllredChance Allred
Hi Aman & Nikhil,

Thank you for taking the time to respond, I appreciate it.

In regards to hard coded ID values, I thought that didnt matter when using Test Classes since I'm referencing just test data such as the myAccount, myOpportunity, & myQuote that I created within the test class?

Could either of you let me know where I can get more information on querying or an alternative approach to get this trigger to work? Sorry I'm still kind of new in the realm of Apex development so I want to make sure I'm following best practices to ensure my code doesnt break.

Thanks again,
Chance