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
Padmini S 4Padmini S 4 

test class issue fro OpportunitylineItem

My trigger is Working Fine, Apex Class is Working Fine, Only Test Class is Problem
This is my Test class Code:
@isTest
    private class testcountOppLineItmsCount {
    static testMethod void testcountOppLineItmsCount() {
        List<OpportunityLineItem > lstoplitm= new list<OpportunityLineItem >();
       Opportunity o = new Opportunity(name='test opp1', CloseDate=date.today(), StageName='Closed Own');
       insert o;
      
           
      OpportunityLineItem op=new OpportunityLineItem (quantity=1,Opportunityid=o.id, TotalPrice=100  );
      insert op;
     lstoplitm.add(op);
     
      insert lstoplitm;
      countOppLineItmsCount.Change(lstoplitm);
       }
       }

Error is:

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]

--

I am New for this PricebookEntry.. psl correct my code or tell me wht shall i do?

thaks 

Best Answer chosen by Padmini S 4
Gokul  BharatiGokul Bharati
Hi Padmini,

Add PricebookentryId is a opportunityLineItem Field.

OpportunityLineItem op=new OpportunityLineItem (quantity=1,Opportunityid=o.id,TotalPrice=100,PricebookEntryId =pbe.Id );

Thanks,
Gokul

All Answers

Sri549Sri549
Hello Padmini,
Could Please post your trigger and Apex class once.

Thanks
Srinivas
Gokul  BharatiGokul Bharati
Hi Padmini,

Each opportunity Line item must have price book Entry ID.
Call the below utility method which creates Price book entry
// Utility method that can be called by Apex tests to create price book entries.
@isTest
public class PriceBookTest {
static testmethod void addPricebookEntries() {
Product2 prod = new Product2(Name = 'Laptop X200',
   Family = 'Hardware');
        insert prod;
Id pricebookId = Test.getStandardPricebookId();//This is available irrespective of the state of SeeAllData.
PricebookEntry standardPrice = new PricebookEntry(
Pricebook2Id = pricebookId, Product2Id = prod.Id,
UnitPrice = 10000, IsActive = true);
insert standardPrice;
 }
}
PricebookEntry a=[SELECT Id FROM PricebookEntry where isActive=true LIMIT 1];
op.PricebookEntryId =a.Id;
insert op;

P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.

Thanks,
Gokul
Padmini S 4Padmini S 4

This is My Modified Code, Gokul Bharithi

@isTest(seeAllData=TRUE)
    private class testcountOppLineItmsCount {
    static testMethod void testcountOppLineItmsCount() {
        List<OpportunityLineItem > lstoplitm= new list<OpportunityLineItem >();
        Account a = new Account(name ='testac1');
        insert a;
        
        
        
Product2 prod = new Product2(Name = 'Laptop X200',Family = 'Hardware');
insert prod;

Id pricebookId = Test.getStandardPricebookId();//This is available irrespective of the state of SeeAllData.
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id,UnitPrice = 10000, IsActive = true);
insert standardPrice;
 

PricebookEntry pbe=[SELECT Id FROM PricebookEntry where isActive=true LIMIT 1];

        
        
        
        
       Opportunity o = new Opportunity(name='test opp1', CloseDate=date.today(), StageName='Closed Own', AccountId=a.id,PricebookEntryId =pbe.Id);
       insert o;
    
      OpportunityLineItem op=new OpportunityLineItem (quantity=1,Opportunityid=o.id, TotalPrice=100  );
      insert op;
     lstoplitm.add(op);
     
      insert lstoplitm;
      countOppLineItmsCount.Change(lstoplitm);
       }
       }
 

The Error is :

Invalid field PricebookEntryId for SObject Opportunity at line 24 column 139  

That is , Here
 

Opportunity o = new Opportunity(name='test opp1', CloseDate=date.today(), StageName='Closed Own', AccountId=a.id,PricebookEntryId =pbe.Id);
       insert o;
Help me in this issue..!!

 

Gokul  BharatiGokul Bharati
Hi Padmini,

Add PricebookentryId is a opportunityLineItem Field.

OpportunityLineItem op=new OpportunityLineItem (quantity=1,Opportunityid=o.id,TotalPrice=100,PricebookEntryId =pbe.Id );

Thanks,
Gokul
This was selected as the best answer
Padmini S 4Padmini S 4
thank u Ghokul , 100% code coverage 
Praveen NPraveen N
Hi,
you need to create product ,price book entry and populate price book entry id on to the opportunity line item. Makes sure that opportunity currerncy and price book entry currency is same. just follow the below example.

Pricebook2 price= [select id, name, isActive from Pricebook2 where isStandard=True];

    Product2  prod=new Product2(Name='abc',isActive = True);
    prod.CanUseRevenueSchedule=true;
        insert prod;
        
        
        PricebookEntry  priceEntry=new PricebookEntry  (product2id=prod.Id,isActive=true,pricebookentryid=price.Id,UnitPrice=100,CurrencyIsoCode ='USD');
        priceEntry.UseStandardPrice=true;
        insert priceEntry;
        
        
        
        OpportunityLineItem OppLineItem = new opportunitylineitem(Discount=10.00,Quantity=3,UnitPrice=1000,opportunityid=opportunityid,pricebookentryid=priceEntry.id,ServiceDate =System.Today()+40);
        insert OppLineItem;

Thanks
Praveen