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
sunfishettesunfishette 

Writing Test Method with master-detail issue

I have a test class that I am writing, and I need to insert a record.  The records custom object has two master-detail relationships.

Custom Object = Invoice__c

Standard = Opportunity

Standard = Account

 

My insert statement is as such:

Invoice__c invoice = new Invoice__c(T_Invoice__c = '1234', Date_of_Purchase_Order_Authorization__c = testdate, Payment_Due_Date__c = testdate, Payment_Received_Date__c = testdate, Aging__c = 0, Your_Purchase_Order__c = '1234', Account_Name__c = testrenewal.accountId, Amount_w_o_Tax__c = 100.00, Amount__c = 100.00, MR_Opportunity__c = ???, Account_Id__c = ???);

When I try to add logic to get the "id" from the opportunity record, I dont seem to get any errors, but the account "id" keeps throwing errors at me like:

Missing required field (if I remove account_id__c)

Insufficient access on cross reference entity (if I try to grab the id from a test opportunity that I insert before this invoice)

Insufficient access on cross reference entity(if I try to grab the id from a test account that I insert before this invoice)

 

 

The idea is that I am creating an invoice from an opportunity, but it must have the Account Name and Opportunity Name on the invoice. The invoice is created from a button off the opportunity page (code for that is done and testing at 94%).

 

Not sure what else to do !!

 

Any help is appreciated.  Thank you.

JitendraJitendra

In Test class, the same user is trying to access the records?

 

What is the OWD settings of Account and Opportunity? If it is private then please check the sharing rules defined. As this is clearly the sharing related error, where the user trying to access the record which is not shared with him.

vriavmvriavm

Hi

If you are doing a query for Opportunityid and Account id please make sure you add @isTest(SeeAllData=true) to your @Test annotation.

If not the option is two reate your test account and opportunity and assign it like below:

 

Account acc = new Account(name='Test Account');
insert acc;
Opportunity opp = new Opportunity(name='Test opportunity', closedate = system.today(),AccountId = acc.Id);
insert opp;
Invoice__c invoice = new Invoice__c(T_Invoice__c = '1234', Date_of_Purchase_Order_Authorization__c = testdate, Payment_Due_Date__c = testdate, Payment_Received_Date__c = testdate, Aging__c = 0, Your_Purchase_Order__c = '1234', Account_Name__c = testrenewal.accountId, Amount_w_o_Tax__c = 100.00, Amount__c = 100.00, MR_Opportunity__c = opp.Id, Account_Id__c = acc.Id);

sunfishettesunfishette

Vriavm -

I thought the same as what you have written.

Here is the full test code:

@isTest
private with sharing class TestgetAllRecords 
{               
    static testMethod void myTest()
    {
        Test.startTest();

        List<Opportunity> oList = new List<Opportunity>{};
	date testdate = System.today().addDays(+1);
        Account testaccount = new Account(Name = 'test');
        insert testaccount;

        Contact testcontact = new Contact( LastName = 'test', AccountId = testaccount.Id);
        insert testcontact;

   Profile p = [select p.id, p.name from profile p where p.UserLicense.name like '%customer portal%' ].get(0);
    User u = new User(alias = 'port',
                email='portaluser@testorg.com',
                emailencodingkey='UTF-8',
                lastname='Testing',
                languagelocalekey='en_US',
                localesidkey='en_US',
                profileid = p.Id, 
                contactid = testcontact.Id,
                timezonesidkey='America/Los_Angeles',
                username='portaluser@testorg.com'
                );
    insert u;
   u.IsActive = true; u.IsPortalEnabled = true; update u;
          System.runAs(u)
          {    
	  
        	    Opportunity testrenewal =  new Opportunity(accountId = testaccount.Id, Amount = 100.00, Closedate = testdate, Description = 'test', Name = 'test', Stagename = 'Baseline', Probability = 100, OwnerId = UserInfo.getUserId(),
Coverage_Start_Date__c = System.today().addDays(-15), Coverage_End_Date__c = System.today().addDays(100));
               	Opportunity testrenewal2 =  new Opportunity(accountId = testaccount.Id, Amount = 100.00, Closedate = testdate.addDays(20), Description = 'test', Name = 'test', Stagename = 'Baseline', Probability = 100, OwnerId = UserInfo.getUserId(),
Coverage_Start_Date__c = System.today().addDays(-15), Coverage_End_Date__c = System.today().addDays(100));
                Opportunity testrenewal3 =  new Opportunity(accountId = testaccount.Id, Amount = 100.00, Closedate = testdate.addDays(50), Description = 'test', Name = 'test', Stagename = 'Baseline', Probability = 100, OwnerId = UserInfo.getUserId(),
Coverage_Start_Date__c = System.today().addDays(-15), Coverage_End_Date__c = System.today().addDays(100));
                Opportunity testrenewal4 =  new Opportunity(accountId = testaccount.Id, Amount = 100.00, Closedate = testdate.addDays(70), Description = 'test', Name = 'test', Stagename = 'Baseline', Probability = 100, OwnerId = UserInfo.getUserId(),
Coverage_Start_Date__c = System.today().addDays(-15), Coverage_End_Date__c = System.today().addDays(100));  
                Database.insert(testrenewal);
                Database.insert(testrenewal2);
                Database.insert(testrenewal3);
                Database.insert(testrenewal4);
               
        	Invoice__c invoice = new Invoice__c(Trident_Invoice__c = '1234', Date_of_Purchase_Order_Authorization__c = testdate, Payment_Due_Date__c = testdate, Payment_Received_Date__c = testdate, Aging__c = 0, Your_Purchase_Order__c = '1234', Account_Name__c = testrenewal.accountId, Amount_w_o_Tax__c = 100.00, Amount__c = 100.00, Maintenance_Renewal__c = testrenewal.id, Account_Id__c = testaccount.id);
        	
        	ApexPages.StandardController sc1 = new ApexPages.StandardController(testrenewal);
          	getAllRecords controller1 = new getAllRecords(sc1);
                controller1.getRecordDetail();
            	controller1.getDuePastDue();
            	controller1.getFiveThirty();
            	controller1.getOverSixty();
            	controller1.getThirtySixty();
            	controller1.getImageName();
            	controller1.getInvoices();
            	controller1.getThisYear();
            	controller1.getListInvoices();
          }
          Test.stopTest();
    }
}

 

 

The way I have it laid out, it makes sense to me, but with this code I get the 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: []

 

Whats happening?