+ Start a Discussion

Product Name from Opportunity Product List

I have an opportunity with a number of product line items.

I want to find the name of a product in a given line item (oldProdList[i] is an OpportunityLineItem).


I use the following query:


String ProdName = [SELECT Name FROM Product2 WHERE (id = :[SELECT Product2Id FROM PriceBookEntry WHERE ID = :oldProdList[i].PricebookEntryId].Id)];

 I get the error: Illegal assignment from LIST<Product2> to String .


I have the following questions:


1. Name is defined as a string field in Product2. Why am I getting this error?

2. Is it the right query structure for obtaining the product name?






You are getting that error because you are retrieving an Object and trying to assign the object to a string.


You can try:


String ProdName;

List<Product2> Products = [SELECT Name FROM Product2 WHERE (id = :[SELECT Product2Id FROM PriceBookEntry[i] WHERE ID = :oldProdList.PricebookEntryId].Id)];

for(Product2 pr:Products)


ProdName = pr.Name;



This should work for you.




Hi Ernie82,


Your suggestion didn't work, although I realize now that the query returns multiple records.


To clarify what I am trying to do:


I am looking at the OpportunityLineItem of a given opportunity.

It is contained in oldProdList[i].


The product ID of the product associated with this line item is given by:

oldProdList[i].PricebookEntryId. This has been tested and verified.


I want to find the product name associated with this line item.

Since I have the PricebookEntryId, I want to find the Product2Id (field within a PricebookEntry)

and from it the name of the product (field within a Product2 table).


I thought that my query would provide the data.

Why would I get multiple entries?






I found the answer.


The product name is not in the Product2 object, but rather in the PricebookEntry table.


The product name is retrieved by the query:


String ProdName = [SELECT Name FROM PriceBookEntry WHERE id = :oldProdList[i].PricebookEntryId].Name;


     Thank you for your help,