[SalesForce] Error in Test Class as :FIELD_INTEGRITY_EXCEPTION

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.

Best Answer

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.

Related Topic