1

I need to create test class for quote and quotelineitem, but i still get error message like this "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [UnitPrice]: [UnitPrice]". Stack Trace are on line insert qliliner;.

Actually i have add UnitPrice when insert quoteLineItem. Can you please help me?

@isTest(SeeAllData=true)
private class quoteTemplateTest{
static testMethod void docTest() 
{
    //get standard pricebook
    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

    Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
    insert pbk1;

    Product2 prod = new Product2(Name='test1',Family='Liner', IsActive=true);
    insert prod;

    Product2 prod1 = new Product2(Name='test1',Family='Trim', IsActive=true);
    insert prod1;

    Product2 prod2 = new Product2(Name='test2',Family='Accessories', IsActive=true);
    insert prod2;

    PricebookEntry pbe = new PricebookEntry (Product2Id=prod.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
    insert pbe;
    PricebookEntry pbe1 = new PricebookEntry (Product2Id=prod1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
    insert pbe1;
    PricebookEntry pbe2 = new PricebookEntry (Product2Id=prod2.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
    insert pbe2;
    Account acc = new Account(name='Account');
    insert acc;

    Contact con = new Contact(lastname='test contact',email='test@gmail.com',phone='1284290842');
    insert con;

    Opportunity opp = new Opportunity(AccountId=acc.id,Pricebook2Id = pbk1.id,Name='test Oppty',CloseDate=system.today(),Next_Follow_Up_Date__c=system.today(),Send_Follow_Up_Email__c = true,StageName='Stage 3 - Estimate f/u - negotiations');
    insert opp;


    Quote qt = new Quote(Name='test quote',OpportunityId=opp.id,Pricebook2Id = standardPb.id, ContactId=con.id, ExpirationDate=system.today(), Preferred_Arrival_Date__c=system.today(), Shipping_From__c='Toronto', Packaging__c='Container - Crates', Shipping_Handling__c='Extra');
    insert qt;

    QuoteLineItem qliliner = new QuoteLineItem(Product2Id=prod.id,QuoteId=qt.id,PriceBookEntryID=pbe.id,Quantity=4, UnitPrice =50);
    insert qliliner;

    QuoteLineItem qlitrim = new QuoteLineItem(Product2Id=prod1.id,QuoteId=qt.id,PriceBookEntryID=pbe1.id,Quantity=4, UnitPrice =50);
    insert qlitrim;

    QuoteLineItem qliacc = new QuoteLineItem(Product2Id=prod2.id,QuoteId=qt.id,PriceBookEntryID=pbe2.id,Quantity=4, UnitPrice =50);
    insert qliacc;


}

}
  • try to remove `(SeeAllData=true)' also make sure there is no trigger running in background creating the data. – Tushar Sharma Dec 19 '16 at 10:21
  • Hi @TusharSharma, when i remove '(SeeAllData=true)', my standard pricebook query is not working. any suggestion? – Febrian Tarigan Dec 19 '16 at 12:06
  • You can get standard pricebookid in test class as Id pricebookId = Test.getStandardPricebookId(); – Tushar Sharma Dec 19 '16 at 13:19
0

try to change this into this

QuoteLineItem qliliner = new QuoteLineItem(Product2Id=prod.id,QuoteId=qt.id,PriceBookEntryID=pbe.id,Quantity=4, UnitPrice =pbe.UnitPrice);
insert qliliner;
  • Hi @bharath, still same error. i don't know how this can be happen as i already put unitprice in there. any another suggestion? – Febrian Tarigan Dec 19 '16 at 12:03
  • did you get the same error on the same line.. – bharath Dec 19 '16 at 12:06
  • yeah same error on the same line – Febrian Tarigan Dec 19 '16 at 12:07
  • Its working for me . Check your actual class. Do you have any validation or trigger on this. – bharath Dec 19 '16 at 12:21
  • Or the class your checking for test coverage might be updated with the unit price. – bharath Dec 19 '16 at 12:25
0

There are a couple of issues

  • Opportunity is using a different pricebook than the Quote
  • QuoteLineItem should always be added with a PricebookEntryId and never a Product2Id. PricebookEntry references a Product2 and upon insert, SFDC will populate the value in QuoteLineItem

The mocking of testdata can be simplified a bit to run faster:

Product2[] pList = new List<Product2> {
                   new Product2 (...),
                   new Product2 (...),
                   .. };
insert plist;

Pricebook2[] pbList = new List<Pricebook2> {
                              new Pricebook2(name = 'foo', isActive = true)
                          };
insert pbList; 

PricebookEntry[] pbeList = new List<PricebookEntry> {
                           new PriceBookEntry(pricebook2Id = Test.getStandardPricebookId(), 
                                              product2Id = pList[0].Id, 
                                              unitPrice = 50.0, isActive = true, ..),

                           new PriceBookEntry(pricebook2Id = pbList[0].Id, 
                                              product2Id = pList[0].Id, 
                                              unitPrice = 50.0, isActive = true, ..),
    ... repeat pattern for products [1] and [2]

insert pbeList;

and then your quote items

QuoteLineItem[] qiList = new List<QuoteLineItem> {
                           new QuoteLineItem(QuoteId = qtId, Quantity = 10, 
                                             PricebookEntryId = pbeList[1].Id), UnitPrice = 50.0
                          new QuoteLineItem(QuoteId = qtId, Quantity = 10, 
                                             PricebookEntryId = pbeList[3].Id), UnitPrice = 50.0
                          ...

insert qiList;

Of course, if every product and line item are meant to be identical, then you can use loops to build the objects rather than explicit enumeration - I provided that as an option here as it can be useful to run through unit test cases with different values for each mocked record if you have that kind of underlying code. And, of course, moving your mocked object construction to separate factory methods for reuse makes even more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.