+ Start a Discussion
danlatdanlat 

Example test method for testing opportunity line items

Hi All,

 

Would anyone be so kind enough as to provide a simple example for testing an opportunity with an associated product.

 

My problem is setting up the object to test, so far I have this:

 

 

// ... creating an Opportunity omitted Product2 p2 = new Product2(Name='Test Product'); OpportunityLineItem oli = new OpportunityLineItem(ProductId=p2.Id, OpportunityId=o.Id);

 

 But this is giving me errors, Do I need to instantiate a PricebookEntry and do it that way?  Please help.

 

In the end what I want is an Opportunity with an attached product. I tried to keep my tests independent but its proving very hard in this instance.

 

The Opportunity Product is added via Salesforce.

 

Many Thanks For Any Help.

danlat

 

 

Best Answer chosen by Admin (Salesforce Developers) 
danlatdanlat

OK, this is the last reply to myself!

 

I just thought I'd share my code to test Opportunities with Line Items, the only assumption it makes is that you have a standard price book.

 

 

// ---------- BEGIN SET UP ---------- //

// Set up some local variables
String opportunityName = 'My Opportunity';
String standardPriceBookId = '';

PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
standardPriceBookId = pb2Standard.Id;

// set up opp and Verify that the results are as expected.
Opportunity o = new Opportunity(AccountId=null, Name=opportunityName,
StageName='Prospecting', CloseDate=Date.today());
insert o;
Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :o.Id];
System.assertEquals(opportunityName, opp.Name);

// set up product2 and Verify that the results are as expected.
Product2 p2 = new Product2(Name='Test Product',isActive=true);
insert p2;
Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :p2.Id];
System.assertEquals('Test Product', p2ex.Name);

// set up PricebookEntry and Verify that the results are as expected.
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true);
insert pbe;
PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
System.assertEquals(standardPriceBookId, pbeex.Pricebook2Id);

// set up OpportunityLineItem and Verify that the results are as expected.
OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id, Quantity=1, TotalPrice=99);
insert oli;
OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :oli.Id];
System.assertEquals(pbe.Id, oliex.PriceBookEntryId);

// ---------- END SET UP (phew!) ---------- //

 

 At the end of this code if all goes well you will have an opportunity (o) with an attached opportunity line item.

 

Hope this helps someone,

danlat

 

Message Edited by danlat on 01-19-2010 05:41 AM

All Answers

danlatdanlat

Just following up on my post.

 

I think I have cracked it, this is what I have now and I dont get any errors on Save, can anyone see any problems with this?

 

 

// create a product Product2 p2 = new Product2(Name='Test Product'); insert p2; // Add product to standard pricebook // Id taken from Salesforce.com String standardPriceBookId = '00B20000004gzIk'; PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id); insert pbe; // attach to opportunity OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id);

 

 Many Thanks

danlat

 

danlatdanlat

OK, this is the last reply to myself!

 

I just thought I'd share my code to test Opportunities with Line Items, the only assumption it makes is that you have a standard price book.

 

 

// ---------- BEGIN SET UP ---------- //

// Set up some local variables
String opportunityName = 'My Opportunity';
String standardPriceBookId = '';

PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
standardPriceBookId = pb2Standard.Id;

// set up opp and Verify that the results are as expected.
Opportunity o = new Opportunity(AccountId=null, Name=opportunityName,
StageName='Prospecting', CloseDate=Date.today());
insert o;
Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :o.Id];
System.assertEquals(opportunityName, opp.Name);

// set up product2 and Verify that the results are as expected.
Product2 p2 = new Product2(Name='Test Product',isActive=true);
insert p2;
Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :p2.Id];
System.assertEquals('Test Product', p2ex.Name);

// set up PricebookEntry and Verify that the results are as expected.
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true);
insert pbe;
PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
System.assertEquals(standardPriceBookId, pbeex.Pricebook2Id);

// set up OpportunityLineItem and Verify that the results are as expected.
OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id, Quantity=1, TotalPrice=99);
insert oli;
OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :oli.Id];
System.assertEquals(pbe.Id, oliex.PriceBookEntryId);

// ---------- END SET UP (phew!) ---------- //

 

 At the end of this code if all goes well you will have an opportunity (o) with an attached opportunity line item.

 

Hope this helps someone,

danlat

 

Message Edited by danlat on 01-19-2010 05:41 AM
This was selected as the best answer
AntonyasLenAntonyasLen

i kept got error with this code =/

l 11