You need to sign in to do that
Don't have an account?
Mont Mont
Can't insert OpportunityLineItem in Test method
Hello. I have problems with this fragment in my Test method:
Account acc = new Account(Name ='testing acc'); insert acc; Product2 prod = new Product2(Name = 'AutoBot', IsActive = true); insert prod; PriceBook2 pb2 = new PriceBook2(Name = 'test', IsActive = true); insert pb2; Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting'); insert opport; PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true); OpportunityLineItem item = new OpportunityLineItem(); item.OpportunityId = opport.Id; item.Product2Id = prod.Id; item.PricebookEntryId = pbe.Id; item.Quantity = 2; item.TotalPrice = item.Quantity * pbe.UnitPrice; insert item; //crush hereCrush in 'insert item'. Where is a problem? I don't see it.
All Answers
There is 2 problem:
1. You need standard pricebook which is not available.
2. You did not insert PricebookEntry (Line # 13). You must insert Pricebook Entry (insert pbe).
try the following code:
Account acc = new Account(Name ='testing acc');
insert acc;
Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
insert prod;
//PriceBook2 pb2 = new PriceBook2(Name = 'test', IsActive = true);
//insert pb2;
Id pricebookId = Test.getStandardPricebookId();
Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pricebookId, StageName = 'Prospecting');
insert opport;
PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
insert pbe;
OpportunityLineItem item = new OpportunityLineItem();
item.OpportunityId = opport.Id;
item.Product2Id = prod.Id;
item.PricebookEntryId = pbe.Id;
item.Quantity = 2;
item.TotalPrice = item.Quantity * pbe.UnitPrice;
insert item; //Supposed to work
Thanks
Abdul Vahid
iBirds Software Services Pvt. Ltd.
Please check one below code u have to use standard price book in the test class.
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com