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
Vikas Kumar 135Vikas Kumar 135 

First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION error when creating test class

Hi All,

I wrote a test class for QuotLineItem and I am getting the below error.
Method Name    getQLI
Pass/Fail    Fail
Error Message    System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: []
Stack Trace    Class.QuoteLineItemResourceTest.getQLI: line 36, column 1


The Test which I am using:

@isTest
public class QuoteLineItemResourceTest {
    public static testMethod void getQLI() {
    
        Account objaccount=new Account();
        objaccount.Name='TestAccount';
        insert objaccount;
        
        PriceBook2  objPricebook2= new PriceBook2();
        objPricebook2.Name='Standard Price Book1';
        objPricebook2.IsActive=true;
        insert objPricebook2;
        
           List<Pricebook2> objPricebook = [Select id,Name from PriceBook2 where isStandard =: true];
           Id standardPB = Test.getStandardPricebookId();
        
               
                 
         Product2 objProduct2=new Product2();
         objProduct2.Name='ABC';
         objProduct2.CurrencyIsoCode='USD';
         insert objProduct2;
         
         PricebookEntry objpricebookentry =new PricebookEntry();
         objpricebookentry.Product2ID = objProduct2.id;
         objpricebookentry.UnitPrice=23.50;
         objpricebookentry.UseStandardPrice=true;
         objpricebookentry.Pricebook2ID=standardPB;
         insert objpricebookentry;
         
         Opportunity objopportunity=new Opportunity();
        objopportunity.AccountID=objaccount.id;
        objopportunity.Name='TestOpportunity';
        objopportunity.StageName='Prospecting';
        objopportunity.CloseDate=Date.today();
        objopportunity.Pricebook2Id = standardPB;
        insert objopportunity;
         
         Quote q1 = new Quote();
         q1.Name = 'Quote 1';
         q1.Budget_Estimate__c = 100;
         q1.OpportunityId = objopportunity.Id;
         q1.Pricebook2ID=standardPB;
         insert q1;
         
         QuoteLineItem objQLI = new QuoteLineItem(QuoteId = q1.Id,
                                                Quantity = 5,
                                                UnitPrice = 10,
                                                PricebookEntryId=objpricebookentry.id,
                                                Product2ID=objProduct2.id,
                                                Operating_System_Id__c = '1;2', 
                                                Third_Party_Provider_Id__c = '3;4',
                                                Third_Party_Audience_Id__c = '5;6',
                                                Country_Id__c = '7;8',
                                                State_Id__c = '4;5',
                                                DMA_Id__c = '6;7',
                                                Content_Limitation_Id__c = '4;5',
                                                Carriers_Id__c = '1;3',
                                                Weather_Temperatures_Id__c = '2;3',
                                                Weather_Conditions_Id__c = '4;5',
                                                Geosegment_Id__c = '2;3',
                                                Start_Date__c = Date.today().addDays(1),
                                                End_Date__c = Date.today().addDays(10),
                                                Planned_Cost__c = 100,
                                                Rate__c = 10,
                                                Planned_Unit__c = 100,
                                                Cost_Method__c = 'CPM');
        insert objQLI;
    

       
       Test.startTest();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/QuoteLineItem/'+objQLI.Id; 
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
        QuoteLineItemResource.doGet();
       
   Test.stopTest();   
   
   
    }
}

I would Appreciate if anyone can help me what can cause this error?


​​​​​​​Thanks
Deepali KulshresthaDeepali Kulshrestha
Hi Vikas ,

Please add this line in your code to make your price book entry active:
  objpricebookentry.isActive=true;
Make sure this will be the code to insert pricebook entry:
  PricebookEntry objpricebookentry =new PricebookEntry();
         objpricebookentry.Product2ID = objProduct2.id;
         objpricebookentry.UnitPrice=23.50;
         objpricebookentry.UseStandardPrice=false;
         objpricebookentry.Pricebook2ID=standardPB;
        objpricebookentry.isActive=true;
         insert objpricebookentry;  

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Vikas Kumar 135Vikas Kumar 135
Hi Deepali,

Thanks for the help. But I am getting a different error now.

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The price book entry is in a different price book than the one assigned to the Quote, or Quote has no price book assigned.: [PricebookEntryId]

Regards,
Vikas
Deepali KulshresthaDeepali Kulshrestha
Hi Vikas,

The exception you have mention only occur if you have not assigned any pricebook id to the "quote".
Please make sure that you have added  the line in your code:
q1.Pricebook2ID=standardPB;
When I commented this line I can see the same error you are getting when I un-comment it the code working fine.

So, Please check your code that you have associated the pricebook with quote or not.
If it doesn't help you then please share your most updated code so I can review.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Deepali Kulshrestha
Vikas Kumar 135Vikas Kumar 135
Hi Deepali,

Thanks for the help.

Yes, I have added the line q1.Pricebook2ID=standardPB to the Quote. I have highlighted it in bold but I am still getting the same error.

@isTest
public class QuoteLineItemResourceTest {
    public static testMethod void getQLI() {
    
        Account objaccount=new Account();
        objaccount.Name='TestAccount';
        insert objaccount;
        
        PriceBook2  objPricebook2= new PriceBook2();
        objPricebook2.Name='Standard Price Book1';
        objPricebook2.IsActive=true;
        insert objPricebook2;
        
           List<Pricebook2> objPricebook = [Select id,Name from PriceBook2 where isStandard =: true];
           Id standardPB = Test.getStandardPricebookId();
                      
                 
         Product2 objProduct2=new Product2();
         objProduct2.Name='ABC';
         objProduct2.ProductCode = 'Pro-D';
         insert objProduct2;
         
              PricebookEntry objpricebookentry =new PricebookEntry();
          objpricebookentry.Product2ID = objProduct2.id;
         objpricebookentry.UnitPrice=23.50;
         objpricebookentry.UseStandardPrice=false;
         objpricebookentry.Pricebook2ID=standardPB;
        objpricebookentry.isActive=true;
         insert objpricebookentry;  
         
         Opportunity objopportunity=new Opportunity();
        objopportunity.AccountID=objaccount.id;
        objopportunity.Name='TestOpportunity';
        objopportunity.StageName='Prospecting';
        objopportunity.CloseDate=Date.today();
        objopportunity.Pricebook2Id = standardPB;
        insert objopportunity;
         
         Quote q1 = new Quote();
         q1.Name = 'Quote 1';
         q1.Budget_Estimate__c = 100;
         q1.OpportunityId = objopportunity.Id;
         q1.Pricebook2ID=standardPB;
         insert q1;
         
         QuoteLineItem objQLI = new QuoteLineItem(QuoteId = q1.Id,
                                                Quantity = 5,
                                                UnitPrice = 10,
                                                PricebookEntryId=objpricebookentry.id,
                                                Product2ID=objProduct2.id,
                                                Operating_System_Id__c = '1;2', 
                                                Third_Party_Provider_Id__c = '3;4',
                                                Third_Party_Audience_Id__c = '5;6',
                                                Country_Id__c = '7;8',
                                                State_Id__c = '4;5',
                                                DMA_Id__c = '6;7',
                                                Content_Limitation_Id__c = '4;5',
                                                Carriers_Id__c = '1;3',
                                                Weather_Temperatures_Id__c = '2;3',
                                                Weather_Conditions_Id__c = '4;5',
                                                Geosegment_Id__c = '2;3',
                                                Start_Date__c = Date.today().addDays(1),
                                                End_Date__c = Date.today().addDays(10),
                                                Planned_Cost__c = 100,
                                                Rate__c = 10,
                                                Planned_Unit__c = 100,
                                                Cost_Method__c = 'CPM');
        insert objQLI;
    

       
       Test.startTest();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/QuoteLineItem/'+objQLI.Id; 
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
        QuoteLineItemResource.doGet();
       
   Test.stopTest();   
   
   
    }
}
Vikas Kumar 135Vikas Kumar 135
Also, how should I use the system. assert method here to make sure that I am getting the expected output.

Regards,
Vikas