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
Guru sfdcGuru sfdc 

Insert failed.FIELD_INTEGRITY_EXCEPTION.

Hi All,

Facing the test class problem for Partner Object."System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: OpportunityId, AccountToId (account to cannot be opportunity account): [OpportunityId, AccountToId]"

  Partner thisPartner = new Partner();
        thisPartner.Role = 'Test Role';
        thisPartner.IsPrimary = true;
        thisPartner.OpportunityId = opp.id;
        thisPartner.AccountToId = acc.id;
        insert thisPartner;
    }
Please help me on this.
Thanks

 
Leo10Leo10
Hi 
Can you share your test class?

Thank you.
Leo10Leo10
Hi
Use this code. If you are getting error plz let me know
@isTest
private class CheckBoxTest{    
    static testMethod void CheckBox(){
        
        Id profileId = [select Id from Profile where Name = 'Standard User'].Id;
        User u =[select id from user where id !=: userinfo.getUserid() limit 1];
        User u1 =[select id from user where id !=: userinfo.getUserid() AND id !=: u.id  limit 1];
        
        Account acc = new Account();
        acc.Name='TestAcc';
        acc.salesAreaList__c = '';
        acc.BillingCountry = 'SLOVAKIA';
        acc.BillingCity = 'test city';
        acc.BillingStreet = 'test street';
        acc.ShippingCountry = 'SLOVAKIA';
        acc.SAP_Customer_Number__c = '123';
        acc.CSN__c='CSN1234';
        insert acc;

        Account accNew = new Account();
        accNew.Name='TestAcc2';
        insert accNew;
        
        Opportunity Opp = TestFactory.getOpportunity();           
        Opp.Name = 'Opportunity record';       
        Opp.CloseDate = system.today()-3;
        Opp.Amount = 100;
        Opp.accountid = acc.id;
        Opp.StageName =  ' Closed Won';
        Opp.won_Loss_Reason__c = 'Test';  
        Opp.CategoryName = 'Best Case';
        insert Opp;

        Opp.ForecastCategoryName = 'Commit';
        update Opp;

        OpportunitySplit oppSplit1 = new OpportunitySplit();
        oppSplit1.OpportunityId = opp.id;
        oppSplit1.SplitNote = 'test';
        oppSplit1.SplitPercentage= 10;
        oppSplit1.Role_Name__c = 'Account Manager'; 
        
        OpportunityTeamMember oppTeam = new OpportunityTeamMember();
        oppTeam.Userid = u.Id;
        oppTeam.OpportunityId = opp.Id;
        oppTeam.TeamMemberRole = 'Account Manager';
        insert oppTeam;
        
        Sales_Scorecard__c SalesScore = new Sales_Scorecard__c();
        SalesScore.Name = 'Test';
        SalesScore.CurrencyIsoCode = 'USD';
        SalesScore.Quota__c = 1000;
        SalesScore.Manager_Quota__c = 2000;
        Insert SalesScore;       
       
       
        Partner thisPartner = new Partner();
        thisPartner.Role = 'Test Role';
        thisPartner.IsPrimary = true;
        thisPartner.OpportunityId = Opp.id;
        thisPartner.AccountToId = accNew.id;
        insert thisPartner;
    }
}

Thank you
Leo10Leo10
Hi,
Insert all field values which you have mentioned in your class and change  Opp.CategoryName = 'Best Case'; into  Opp.ForecastCategoryName = 'Best Case';. and also do bulk test for your class then you will get 100% coverage 

Please mark this as Best Answer if it helps!

Thank you
Guru sfdcGuru sfdc
Hi Nabeel,

I have inserted all required field values. Still my code coverage is showing 68%.Can you please help me on this.

Here is my test class:
        
    @istest(Seealldata=true)
private class CheckBoxTest{    
    static testMethod void CheckBox(){
        
      Id profileId = [select Id from Profile where Name = 'Standard User'].Id;
       
          User u =[select id,UserRole from user where id !=: userinfo.getUserid() limit 1];
          User u1 =[select id from user where id !=: userinfo.getUserid() AND id !=: u.id  limit 1];
           Id devRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Renewal').getRecordTypeId();
           
        Account acc = new Account();
        acc.Name='TestAcc';
        acc.salesAreaList__c = '';
        acc.BillingCountry = 'SLOVAKIA';
        acc.BillingCity = 'test city';
        acc.BillingStreet = 'test street';
        acc.ShippingCountry = 'SLOVAKIA';
        acc.Customer_Number__c = '123';
        acc.CSN__c='CSN1234';
        insert acc;
        
        Account accNew = new Account();
        accNew.Name='TestAcc2';
        accNew.salesAreaList__c = '';
        accNew.BillingCountry = 'SLOVAKIA';
        accNew.BillingCity = 'test city';
        accNew.BillingStreet = 'test street';
        accNew.ShippingCountry = 'SLOVAKIA';
        accNew.Customer_Number__c = '123';
        accNew.CSN__c='CSN1234';
        insert accNew;
        
        Opportunity Opp = TestFactory.getOpportunity();           
        Opp.Name = 'Opportunity record';       
        Opp.CloseDate = system.today()-3;
        Opp.Amount = 100;
        Opp.accountid = acc.id;
        //Opp.StageName =  'S8- Closed Won';
        Opp.Owner_Role__c = u.id;
        Opp.StageName = 'S3- Discovery';
        Opp.Funded_Opportunity__c = 'No' ;
        Opp.won_Loss_Reason__c = 'Test';  
        Opp.Big_Deal__c = False;
        Opp.ForecastCategoryName = 'Best Case';
         //test.starttest();
        insert Opp;
       /* Opp.Big_Deal__c =True;
        Opp.ForecastCategoryName = 'Commit';
        update Opp;*/
       // test.stoptest();
         list<OpportunitySplit> lstToInsert = new list<OpportunitySplit>();  
        OpportunitySplit oppSplit = new OpportunitySplit();
        oppSplit.OpportunityId = opp.id;
        oppSplit.SplitNote = 'test';
        oppSplit.SplitPercentage= 100;
      //  oppSplit.SplitAmount = 2000;
        oppSplit.SplitOwnerid= u.id;
        oppSplit.Role_Name__c = 'SW Channel Manager';
        lstToInsert.add(oppSplit);
        
          OpportunitySplit oppSplit1 = new OpportunitySplit();
          oppSplit1.OpportunityId = opp.id;
          oppSplit1.SplitNote = 'test';
          oppSplit1.SplitPercentage= 100;
       //  oppSplit1.SplitAmount = 2000;
          oppSplit1.SplitOwnerid= u.id;
          oppSplit1.Role_Name__c = 'SW Account Manager';
    
                  
        OpportunityTeamMember oppTeam = new OpportunityTeamMember();
        oppTeam.Userid = u.Id;
        oppTeam.OpportunityId = opp.Id;
        oppTeam.TeamMemberRole = 'SW Account Manager';
        oppTeam.TeamMemberRole = 'SW Channel Manager';
        insert oppTeam;
        
        
        Sales_Scorecard__c SalesScore = new Sales_Scorecard__c();
        SalesScore.Name = 'Test';
        SalesScore.CurrencyIsoCode = 'USD';
        SalesScore.Quota__c = 1000;
        SalesScore.Manager_Quota__c = 2000;
        Insert SalesScore;
        Opp.Big_Deal__c =True;
        Opp.ForecastCategoryName = 'Commit';
        update Opp;
        
        Partner thisPartner = new Partner();
        thisPartner.Role = 'Test Role';
        thisPartner.IsPrimary = true;
        thisPartner.OpportunityId = opp.id;
        thisPartner.AccountToId = accNew.id;
        insert thisPartner;
        
        
    }
}

Thanks





 
Leo10Leo10
Hi,
Share your class also.

Thank you