+ Start a Discussion
ChitraChitra 

Question on OpportunityLineItem

I am trying to create a new OpportunityLineItem.. I am not sure how to map the productId with the listings in the Pricebook??...

I understand from earier post that the productId gets added automatically when creating the OpportunityLineItem.. But how does it know the correct productId ..?

If not.. how to get the productId( from the pricebook????)

And what are the required fields for OpportunityLineItem..

Thanks a lot..

Chitra

DevAngelDevAngel

Hi Chitra,

The opportunityLineItem contains a field called ProductId.  This is how you related the line item to the product.  Products are contained in pricebooks.  You can have multiple pricebooks.  When you query the product object, you can obtain the pricebook id for each product.

So, determine the pricebook that you want to use.  Query the products using the PricebookId as a filter.  Select the appropriate product and create a new line item using the product id from the chosen product.

ChitraChitra

Hey Dave,

Thanks a lot.. Let me make sure I completely understand this..

1. I get the list of pricebooks and find out their  id.

2. With the pricebook id .. I can get the list of products and their corresponding id..

3. Then I can create the OpportunityLineItem using the productID and thats the association..!!!

 

I tried to do this.. But my company has 2 pricebooks.. I tired to find the active pricebook using

pricebook.getIsActive() method.. but it throws up null..!!

So I checked the web version [customized salesforce.com - Product - Pricebook] .. it says one of the pricebook is active.. What am I doing wrong???? Is there  any other way to find out the active PriceBook ???

Thank you...

Chitra

 

DevAngelDevAngel

Hi chitra,

Try this (I did this in notepad, so there may be syntax errors).

//get active price book

QueryResult qr = binding.query("Select Id, name from PriceBook where IsActive = true");

//you can have more than one active price book, you will have to figure out which one you want to use

//retrieve the price book from the query resutls

PriceBook pb = (PriceBook) qr.getResults()[0];

//Select the products in this pricebook

qr = binding.query("Select Id, name, productcode, defaultprice from product where pricebookid = '" + pb.getId().getValue(); + "'");

//this gives you a list of products from the pricebook

//now we well add a line item using the first product from the pricebook

//create the opportunity line item

OpportunityLineItem oli = new OpportunityLineItem();

oli.setDescription("this is the new line item.");

oli.setOpportunityId(oppID);

oli.setProductId(((Product)qr.getResults()[0]).getId());

oli.setQuantity(2);

//call the create function

SaveResult[] sr = binding.create(new SObject[] {oli});

ChitraChitra

Hey Dave..

Thanks. It helped..!!!

Chitra