I am trying to get the default value from a customer Opportunity Product field to show up on a Visualforce page. My VF page has a custom controller. When I add an Opportunity Product to the page, I want the default value for my Rate__c custom field (which is 0.00) to appear on the VF page. Is there something I need to do in my controller or VF page to have this happen? It already happens for the UnitPrice field that is on the same VF page.

VF Controller:

public with sharing class opportunityProductEntryExtension {

public Opportunity theOpp {get;set;}
public String searchString {get;set;}
public opportunityLineItem[] shoppingCart {get;set;}
public priceBookEntry[] AvailableProducts {get;set;}
public Pricebook2 theBook {get;set;}   

public String toSelect {get; set;}
public String toUnselect {get; set;}
public Decimal Total {get;set;}

public Boolean overLimit {get;set;}
public Boolean multipleCurrencies {get; set;}

    private Boolean forcePricebookSelection = false;

    private opportunityLineItem[] forDeletion = new opportunityLineItem[]{};

    public opportunityProductEntryExtension(ApexPages.StandardController controller) {

        multipleCurrencies = UserInfo.isMultiCurrencyOrganization();
            theOpp = [select Id, Pricebook2Id, PriceBook2.Name from Opportunity where Id = :controller.getRecord().Id limit 1];

        shoppingCart = [select Id, Rate__c, Quantity, TotalPrice, UnitPrice, Description, PriceBookEntryId, PriceBookEntry.Name, PriceBookEntry.IsActive, PriceBookEntry.Product2Id, PriceBookEntry.Product2.Name, PriceBookEntry.PriceBook2Id from opportunityLineItem where OpportunityId=:theOpp.Id];

        if(theOpp.Pricebook2Id == null){
            Pricebook2[] activepbs = [select Id, Name from Pricebook2 where isActive = true limit 2];
            if(activepbs.size() == 2){
                forcePricebookSelection = true;
                theBook = new Pricebook2();
                theBook = activepbs[0];
            theBook = theOpp.Pricebook2;


    public void addToShoppingCart(){

        for(PricebookEntry d : AvailableProducts){
                shoppingCart.add(new opportunityLineItem(OpportunityId=theOpp.Id, PriceBookEntry=d, PriceBookEntryId=d.Id, UnitPrice=d.UnitPrice));


    public PageReference onSave(){
        return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
    public PageReference onCancel(){
        return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
    public PageReference changePricebook(){
        PageReference ref = new PageReference('/oppitm/choosepricebook.jsp');
        ref.getParameters().put('retURL','/apex/opportunityProductEntry?id=' + theOpp.Id);
        return ref;

VF Page:

<apex:sectionHeader Title="Add {!$ObjectType.Product2.LabelPlural}" subtitle="{!opportunity.Name}"/>
<apex:messages style="color:red"/>

<apex:form >

    <apex:outputPanel id="mainBody">

        <apex:outputLabel styleClass="label">PriceBook: </apex:outputLabel>
        <apex:outputText value="{!theBook.Name}"/>&nbsp;
        <apex:commandLink action="{!changePricebook}" value="change" immediate="true"/><br/>
        <apex:outputPanel rendered="{!multipleCurrencies}">
            <apex:outputLabel styleClass="label">Currency: </apex:outputLabel>
            <apex:outputText value="{!chosenCurrency}"/><br/>
        <apex:pageBlock title="Selected {!$ObjectType.Product2.LabelPlural}" id="selected">
            <apex:pageblockTable value="{!shoppingCart}" var="s">
                <apex:column >
                    <apex:commandLink value="Remove" action="{!removeFromShoppingCart}" reRender="selected,searchResults" immediate="true">
                        <apex:param value="{!s.PriceBookEntryId}" assignTo="{!toUnselect}" name="toUnselect"/>
                <apex:column headerValue="{!$ObjectType.Product2.LabelPlural}" value="{!s.PriceBookEntry.Product2.Name}"/>
                <apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Quantity.Label}">
                    <apex:inputField value="{!s.Quantity}" style="width:70px" required="true" onkeyup="refreshTotals();"/>
                <apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Rate__c.Label}">
                    <apex:inputField value="{!s.Rate__c}" style="width:70px" onkeyup="refreshTotals();"/>
                <apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.UnitPrice.Label}">
                    <apex:inputField value="{!s.UnitPrice}" required="{!IF(s.Port__c == null,TRUE,FALSE)}" style="width:70px" onkeyup="refreshTotals();"/>
                <apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.TotalPrice.Label}">
                <apex:outputText id="tot" value="${0, number, ###,###,###,##0.00}">
                <apex:param value="{!(s.Quantity*s.UnitPrice)}"/>


  • can you add your visualforce and controller code May 30 '17 at 20:43
  • OK, I added. It was rather long which is why I held off adding them. Thanks. May 30 '17 at 21:02
  • remove unnecessary code and put only relevant one May 30 '17 at 21:05
  • What's the type of the Rate__c field? May 31 '17 at 7:09
  • Rate__c is a Number field with 2 decimals. May 31 '17 at 15:26

For apex:inputField it is bit tricky.

you need to wrap javascript function inside apex:inputField then on loading on the page if value is blank it will display 0.00

<apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Rate__c.Label}">
    <apex:inputField value="{!s.Rate__c}" style="width:70px" onkeyup="refreshTotals();" id="rate">
        document.getElementById('{!$Component.rate}').value = 
     Number(document.getElementById('{!$Component.rate}').value.replace(/,/g, '')).toFixed(2);

Also, you can call javascript onblur function and use this script in case you need decimal places to be shown always.

  • It is a currency field, but I also tried the same thing with a number decimal field and it still does not show. May 30 '17 at 21:20
  • Thanks. I tried that but it didn't work. When you have $Component.rate, does rate need to be the variable API name? May 31 '17 at 15:25
  • 1
    rate is id of inputField May 31 '17 at 15:29
  • Got it, thanks. I had a typo in one of the lines. I fixed that and it now works! Thanks so much!! May 31 '17 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.