+ Start a Discussion

Set PriceBookEntry.Name of Opportunity Line Item for display in VF page.

Set PriceBookEntry.Name of Opportunity Line Item for display in VF page.

I am using a datatable to show a list of Opportunities. I initially query for and display directly from sfdc.

public List<OpportunityLineItem> getLineItemsTechnology() {
if(lit == null){
lit = [select PriceBookEntry.Name, Mix__c, Quantity, UnitPrice, PriceBookEntry.ProductCode
from OpportunityLineItem where OpportunityId = :oid and PriceBookEntry.ProductCode = 'Technology'];

return lit;


I have another method that adds to lit and re-renders that section.
I want to set PriceBookEntry.Name for display in the VF page

Something like

OpportunityLineItem tmpOLI = [select Mix__c,PriceBookEntry.Name from OpportunityLineItem limit 1];
OpportunityLineItem newOLI = tmpOLI.clone(false);

newOLI.PriceBookEntry.Name = 'Set Name For Display in VF Page Datatable';

I can execute/save the code that sets the PriceBookEntry.Name through eclipse using v10 of the Api.
However through the UI [saving apex code or saving from the vf controller editor] I get the message.

Error: Compile Error: Field is not writeable: PricebookEntry.Name at line 54 column 17.

You may be able to save the code successfully with API 10 but you should get a runtime exception since the field is truly not updateable.

PricebookEntry is master-detailed to Product2 where it gets it's name, i.e. PricebookEntry.Name is a derived field from Product2.Name so this error is correct and not to mention it's better to get this at compile time than runtime.

For this specific issue, if you want to display some descriptive text along side each line item in your visualforce page then you could create a wrapper class that has a String and PricebookEntry member (and associated accessors) and then just display the collection of these wrapper objects. Alternatively if you want to pull a custom field from the product2 or opportunityLineItem object you can do that as well.

I am going to move this to the VF board because the solution seems to be specific to that area.
Thanks for your response. I have other Apex code that is in production and running without error that has been in since Apex was GA that fails to compile with the latest version of the API.

Error: Compile Error: Field is not writeable: ProductCode at line 67 column 115

The line being

defaultServicePBE = new PricebookEntry(isActive=true,UnitPrice=5,product2Id = newProduct.Id,ProductCode='Service',Pricebook2Id=defaultPricebookId,CurrencyIsoCode=currentUser.defaultCurrencyIsoCode);

This code is part of a test method.

I am not sure how your code is running without errors unless we happen to be ignoring them for convenience.

PricebookEntry.ProductCode is also not editable as it is similarly derived from the related product2 object.

I will open some inquiries internally but rest assured the current behavior is correct.