+ Start a Discussion
New_DeveloperNew_Developer 

Help with test class for inserting OppLineItem

HI,

 

Need help with insering the Oppline Item. There are lot of discussions in this board about this and i even followed them but still couldnt insert the Opportunity Line Item The error i get is 

 

FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]

 

Pricebook2 pb= [select Id from PriceBook2 where IsStandard=True limit 1];


Product2 product = new Product2();
product.Name='Test Product';
product.IsActive=true;
insert product ;

// Create a pricebook entry for custom pricebook
PricebookEntry pbe = new PricebookEntry();
pbe.UseStandardPrice = false;
pbe.Pricebook2Id=pb.id;
pbe.Product2Id=product.id;
pbe.IsActive=true;
pbe.UnitPrice=99;

insert pbe;

 

Opportunity objOpp1= new Opportunity();
objOpp1.Name = 'Testing12';
 objOpp1.StageName = 'Upside';
 objOpp1.CloseDate = System.Today();
 insert objOpp1;


OpportunityLineItem NewRec = new OpportunityLineItem();
NewRec.TotalPrice=99;
NewRec.Quantity=1;
NewRec.OpportunityId=objOpp1.Id;
NewRec.PricebookEntryId=pbe.id;
insert NewRec;

 

Thanks

kpeterskpeters

An easy fix for you would be to explicitly set the Pricebook on your Opportunity before you insert it (objOpp1.Pricebook2Id = pb.Id).

New_DeveloperNew_Developer

Nope, I tried that already , it didnt work :(

kpeterskpeters

Have you tried querying your Opportunity after you insert it to confirm the Pricebook2Id is what you expect?

New_DeveloperNew_Developer

I tried to query on the Opportunity and found out that tha pricebook id's are different

 

and i also tried it the way it is mentioned in this post http://forums.sforce.com/t5/Apex-Code-Development/unit-test-error-standard-price-not-defined/td-p/26...

 

still no luck :(

 

 

kpeterskpeters

Can you provide your updated code?  Do you have any triggers or any other functionality that would change the pricebook of an Opportunity after insert?

New_DeveloperNew_Developer

This is my upaetd code and there are triggers on OpportunityLineItems but i think they dont effect in inserting a opportunityLineitem

 

Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
insert pb;

 

Product2 product = new Product2();
product.Name='Test Product';
product.IsActive=true;
insert product ;

// Create a pricebook entry for custom pricebook
PricebookEntry pbe = new PricebookEntry();
pbe.UseStandardPrice = false;
pbe.Pricebook2Id=pb.id;
pbe.Product2Id=product.id;
pbe.IsActive=true;
pbe.UnitPrice=99;

insert pbe;

 

Opportunity objOpp1= new Opportunity();
objOpp1.Name = 'Testing12';
 objOpp1.StageName = 'Upside';
 objOpp1.CloseDate = System.Today();
 insert objOpp1;


OpportunityLineItem NewRec = new OpportunityLineItem();
NewRec.TotalPrice=99;
NewRec.Quantity=1;
NewRec.OpportunityId=objOpp1.Id;
NewRec.PricebookEntryId=pbe.id;
insert NewRec;

kpeterskpeters

You still aren't explicitly setting Pricebook on your Opportunity before you insert it (objOpp1.Pricebook2Id = pb.Id).

New_DeveloperNew_Developer

I did that. it did not work.

 

Now iam getting a different error 

 

System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

kpeterskpeters

You should go back to using the standard price book, because you created a new non-standard pricebook.  If you want to use a non-standard pricebook, you first have to put a price for that Product in the standard pricebook.

New_DeveloperNew_Developer

I tried that too

 

here is my code. still its giving the error  FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]. 

 

PriceBook2 objPriceBook =  [select Id from Pricebook2 where isStandard=true limit 1];
            
            Product2 objPro1 = new Product2(Name = 'Abc Software',Solution_Category__c = 'Access',IsActive = true);
            Insert objPro1;
            
            
            PricebookEntry objPriceBookEntry = new PricebookEntry(Pricebook2Id =objPriceBook.Id,Product2Id = objPro1.Id,UnitPrice = 100,IsActive = true,UseStandardPrice=false);
            insert objPriceBookEntry;
            
              Opportunity objOpp1= new Opportunity();
            objOpp1.Name = 'Testing12';
            objOpp1.AccountId = a.Id;
            objOpp1.StageName = 'SandBox';
            objOpp1.CloseDate = System.Today();
            objOpp1.Pricebook2Id = objPriceBook.Id;
            insert objOpp1;
            
         
 
OpportunityLineItem NewRec = new OpportunityLineItem();
NewRec.TotalPrice=99;
NewRec.Quantity=1;
NewRec.OpportunityId=objOpp1.Id;
NewRec.PricebookEntryId=objPriceBookEntry.id;
//NewRec.Product2id =product.id;
insert NewRec;
Eugene PozniakEugene Pozniak

I faced the same problem. You can use the following example.

 

But in my case I used the (SeeAllData=true) annotation to get access to the standard Pricebook.

 

@isTest(SeeAllData=true)
private class LineItemTest {
	static testMethod void myUnitTest() {
		List<Opportunity> testOppList = new List<Opportunity>();
		for (Integer i = 0; i < 100; i++) {
			testOppList.add(new Opportunity(
				Name = 'TestOpp' + i,
				StageName = 'Upside',
				CloseDate = System.Today()));
		}
		insert testOppList;
			
		Product2 prod2 = new Product2(
			Name = 'testName',
			IsActive=true);
		insert prod2;
		
		Pricebook2 standardPB = [
				SELECT id
				FROM Pricebook2
				WHERE isStandard = true];
		
		PricebookEntry price = new PricebookEntry(
			UseStandardPrice = false,
			Pricebook2Id = standardPB.Id,
			UnitPrice = 1.0,
			IsActive = true,
			Product2Id = prod2.Id);
		insert price;
			
		List<OpportunityLineItem> testOppLineItemsList = new List<OpportunityLineItem>();
		for (Opportunity currentOpportunity : testOppList) {
			for (Integer i = 0; i < 2; i++) {
				testOppLineItemsList.add(new OpportunityLineItem(
					OpportunityId = currentOpportunity.Id,
					Quantity = 1.0,
					TotalPrice = 1.0,
					PricebookEntryId = price.Id));
			}
		}
		insert testOppLineItemsList;
	}
}

 

Also I created the List of Opportunity type and OpportunityLineItem type to check the ScriptStatement limit.

 

Hope it will be usefull for you.

 

SFDC coderSFDC coder
hi,
my scenario is also somewhat similar,i have created a product and a pricebookEntry for the same but
i get an error no standard price defined for your product