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
ManishKSinghManishKSingh 

Issue in unit test class of QuoteLineItem

I am creating unit test class for quote line item and it execute without any exception in sandbox but when i validate it in the production organization of salesforce, it's fail with following error "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [UnitPrice]: [UnitPrice]
Stack Trace: Class.Test_TriggerRollupPriceAndCost.testRollupPriceCost: line 48, column 1".

but [UnitPrice] field is already defined in the below class.

@isTest(seeAlldata=true)
public class Test_TriggerRollupPriceAndCost {
    static testmethod void testRollupPriceCost(){
    
        Account a=new Account(Name='Test');
        insert a;
        
        Opportunity o=new Opportunity();
        o.Name='TestOpp';
        o.AccountId=a.Id;
        o.StageName='Test';
        o.CloseDate=System.Today().addMonths(1);
        o.LeadSource='test';
        o.District_Sales_Rep__c=[Select Id from User where Name='Salesforce Uploader' limit 1].id;
        o.Product_Line__c='Tet';
        o.Type='Test';
        o.Vertical_Market__c='Test';
        o.Comm_Split_Primary__c=100.00;
        insert o;       
        
        Product2 p1 = new Product2();
        p1.Name='Test8';
        p1.Productcode='PO16';
        p1.IsActive = true;        
        insert p1;      

        Pricebook2 standardPB = [Select Id from Pricebook2 where isStandard=true limit 1];
        
        PricebookEntry pbe = new PricebookEntry();
        pbe.IsActive = true;
        pbe.Product2ID = p1.Id;
        pbe.Pricebook2Id = standardPB.Id;
        pbe.UnitPrice = 200;        
        //pbe.UseStandardPrice = false;
        insert pbe;
                
        Quote q=new Quote();
        q.Name='Test';        
        q.OpportunityId=o.Id;
        q.Pricebook2Id=standardPB.Id;
        insert q;
       
        QuoteLineitem q1=new QuoteLineItem();        
        q1.PricebookEntryId=pbe.Id;
        q1.Quantity=1;        
        q1.UnitPrice=pbe.UnitPrice;
        q1.QuoteId=q.Id;
        insert q1;      // exception is here..
        delete q1;
    }   
}

Plz help me to resolve this issue

Thanks in advance.
Best Answer chosen by ManishKSingh
pconpcon
My guess is that something with your seeAllData is messing this up.  You should try to refrain from using that whenever possible.  As of Summer '14 you do not need it to access Pricebooks [1] and can just use Test.getStandardPricebookId().  I would try that first.  Typically when you see testing errors only when pushing to production it is either because of seeAllData or because there recently has been changes made directly to production that were not propegated to the sandboxes.  So if removing seeAllData from the test does not work, I would verify that there were no validation rules or workflows that were recently added that were not added to your sandbox.

[1] http://releasenotes.docs.salesforce.com/en-us/summer14/release-notes/rn_apex_price_books_in_tests.htm

All Answers

pconpcon
If you set the UnitPrice field to 200 instead of to the value of pbe.UnitPrice does it work?  If you define the entier QuoteLineItem inside the new does it work:
 
QuoteLineitem q1 = new QuoteLineItem(
    PricebookEntryId = pbe.Id,
    Quantity = 1,
    UnitPrice = pbe.UnitPrice,
    QuoteId = q.Id
);
insert q1;
ManishKSinghManishKSingh
Thanks for replying.
i am try the both scenario but it only works in sandbox, fail in the production organization withgiven exception
pconpcon
My guess is that something with your seeAllData is messing this up.  You should try to refrain from using that whenever possible.  As of Summer '14 you do not need it to access Pricebooks [1] and can just use Test.getStandardPricebookId().  I would try that first.  Typically when you see testing errors only when pushing to production it is either because of seeAllData or because there recently has been changes made directly to production that were not propegated to the sandboxes.  So if removing seeAllData from the test does not work, I would verify that there were no validation rules or workflows that were recently added that were not added to your sandbox.

[1] http://releasenotes.docs.salesforce.com/en-us/summer14/release-notes/rn_apex_price_books_in_tests.htm
This was selected as the best answer
ManishKSinghManishKSingh
Once again , thanks for replying..
My apex class successfully deployed . The issue is in the workflow rather than unit test class. But your suggestion is very helpful for my future reference.
Thanks