1

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();
            }
            else{
                theBook = activepbs[0];
            }
        }
        else{
            theBook = theOpp.Pricebook2;
        }

        if(!forcePricebookSelection)
            updateAvailableList();
    }

    public void addToShoppingCart(){

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

        updateAvailableList();  
    }

    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('id',theOpp.Id);
        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:outputPanel><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:commandLink>
                </apex:column>
                <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>
                <apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.Rate__c.Label}">
                    <apex:inputField value="{!s.Rate__c}" style="width:70px" onkeyup="refreshTotals();"/>
                </apex:column>
                <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>
                <apex:column headerValue="{!$ObjectType.OpportunityLineItem.Fields.TotalPrice.Label}">
                <apex:outputText id="tot" value="${0, number, ###,###,###,##0.00}">
                <apex:param value="{!(s.Quantity*s.UnitPrice)}"/>
                </apex:outputText> 
                </apex:column> 
        </apex:pageblockTable>
    </apex:pageBlock>

    </apex:outputPanel>
</apex:form>

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

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">
        <script>                           
        document.getElementById('{!$Component.rate}').value = 
     Number(document.getElementById('{!$Component.rate}').value.replace(/,/g, '')).toFixed(2);
    </script>
    </apex:inputField>
</apex:column>

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. – John Neilan 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? – John Neilan May 31 '17 at 15:25
  • 1
    rate is id of inputField – Santanu Boral 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!! – John Neilan 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.