7

Can any one help me out with this Error as :

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception:

PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id, others must specify product id): [PricebookEntryId, unknown]

Test class :

@istest
Public class TestInvoiceController {

    static Testmethod void Invoicemethodtest() {

        Date closeDt = Date.Today();

        //list<opportunity> opp = new list<opportunity>();

        Account a = new Account();
        a.Name = 'icrm test acc';

        insert a;

        opportunity op = new opportunity(Name = 'test DIE 4/6/2015', AccountId = a.Id, StageName = 'Prospecting',
            CloseDate = closeDt,
            Bill_of_Lading__c = 'This is waybill1 this is waybill 2 waybill3 -34958309458 waybill - 44570375');

        insert op;

        // list<opportunitylineitem> ol = new list<opportunitylineitem>(); 

        OpportunityLineItem OPplineitem = new OpportunityLineItem(Quantity = 2, OpportunityId = op.Id, UnitPrice = 0.01, PriceBookEntryId = '01ud0000004YWFqAAO');

        insert OPplineitem;
    }

    static Testmethod void Invoicemethodtest1() {

        Date closeDt = Date.Today();

        //list<opportunity> opp = new list<opportunity>();
        Account a = new Account();
        a.Name = 'icrm test acc';

        insert a;

        opportunity op = new opportunity(Name = 'test NonDIE 4/6/2015', AccountId = a.Id, StageName = 'Prospecting',
            CloseDate = closeDt,
            Bill_of_Lading__c = 'This is waybill1 this is waybill 2 waybill3 -34958309458 waybill - 44570375');

        insert op;

        //list<opportunitylineitem> ol = new list<opportunitylineitem>(); 

        OpportunityLineItem OPplineitem = new OpportunityLineItem(Quantity = 2, OpportunityId = op.Id, UnitPrice = 1, PriceBookEntryId = '01ud0000004YWFqAAO');

        insert OPplineitem;

    }
}

2)Why do the code coverage displayed as None.

Any help very much appreciated.

2
  • Are you sure you have the API version of the class set properly?
    – Eric
    Commented Jun 14, 2015 at 20:20
  • Have you double checked that the pricebookentry with id "01ud0000004YWFqAAO" exists within your salesforce instance? Also, please confirm when are you getting this error.
    – Anshul
    Commented Jun 15, 2015 at 21:42

5 Answers 5

5

Try the Below code to create OpportunityLineItem and everything related to it in Test class.

Product2 prod = new Product2(Name = 'Laptop X200', Family = 'Hardware');
insert prod;

Id pricebookId = Test.getStandardPricebookId();

PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true);
insert standardPrice;

Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;

PricebookEntry customPrice = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = prod.Id, UnitPrice = 12000, IsActive = true);
insert customPrice;

Account acc = new Account(Name='TestAccount');
insert acc;

Opportunity opp = new Opportunity(Name='TestOpportunity', AccountId=acc.Id, CloseDate=Date.Today(), StageName='Suspect', Pricebook2Id=customPB.Id);
insert opp;

OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = opp.Id, Quantity = 5, PricebookEntryId = customPrice.Id, TotalPrice = 500);
insert oli;

Hope this will solve the Issue as this is the Best way to create a OpportunityLineItem.

2

This error comes when you are populating lookup field with wrong Id. That means, Object_1.Lookup_field = object_2.Id;

From error you mentioned above, it seems that you are putting value other than PricebookEntryId in actual class and not test class Check your main class, whether lookup fields are populated correctly.

0

PriceBookEntryId exists only for those organizations that have Products enabled as a feature. Assuming, it's enabled for you, please check if you are assigning correct Id to this field in the test class.

The second part of the error simply means that ProductId field is unavailable as of version 3.0 and is only provided for backward compatibility. Use the PricebookEntryId field instead, specifying the ID of the PricebookEntry record.

0

I just had this error come up. It happened when the Value of the PriceBookEntryID on the OLI I was trying to insert was NULL so check using debug to ensure that the PriceBookEntryID is NOT Null AND is Active and valid when you are inserting the Line Item

0

1) Create Price Book Entries in Apex Tests - the idea is to create a PricebookEntry with Pricebook2Id = Test.getStandardPricebookId() and only then you create PricebookEntry with your custom Pricebook. (this is duplicate of AKSHAY Dhiman's answer)
2) Code coverage displayed as None because your test fails. As soon as it runs successfully it will give you coverage.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .