function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ryno LourensRyno Lourens 

Can't call a method from a custom controller.

Hi everyone,

I'm creating a Visualforce page that needs to display a specific value from a custom object called Rate Cards. I've created a custom controller with a new method called GetRate that should do the trick, however, when I try to call the GetRate method from the Visualforce page, it says that no such method exists. Please take a look at my two pages and let me know what you think!

Rate Card Test Visualforce Page:
<apex:page standardController="Opportunity" extensions="RateCardCalculations">
    <apex:form >
        <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="oli">
            <apex:pageBlock title="Rate Card - {!Opportunity.Pricebook2.Name}">
                <apex:pageBlockTable value="{!oli}" var="item">
                    <apex:column headerValue="Product Name">
                        <apex:outputLink value="/{!item.Id}">{!item.PricebookEntry.Name}</apex:outputLink>
                    </apex:column>
                    <apex:column headerValue="Product Family">
                        <apex:outputField value="{!item.Product_Family__c}" />
                    </apex:column>
                    <apex:column headerValue="Billings/Type/Market">
                        <apex:inputField value="{!item.Price_Book__c}" style="display:none;"/>
                        <apex:outputField value="{!item.Billing_Type_Market__c}" />
                    </apex:column>
                    <apex:column headerValue="Base Price">
                          <apex:outputText value="{0, number, currency}">
                                  <apex:param value="{!getRate(item)}"/>
                          </apex:outputText>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:repeat>
    </apex:form>
</apex:page>
RateCardCalculations Custom Controller
public class RateCardCalculations {
   	public Opportunity opp;
    
    public RateCardCalculations(ApexPages.StandardController controller) {
        opp = (Opportunity)controller.getRecord();
    }
    
    public Decimal getRate(OpportunityLineItem item) {
    Decimal rate = 0;
    String fieldName = item.Name;
    List<Rate_Card__c> rateCards = [SELECT Name, Product_Family__c, Billings_Type_Market__c, Price_Book__c
                                    FROM Rate_Card__c                                    
                                    WHERE Price_Book__c = :item.Price_Book__c 
                                    AND   Billings_Type_Market__c = :item.Billing_Type_Market__c 
                                    AND   Product_Family__c = :item.Product_Family__c];
    if(rateCards != null && rateCards.size() > 0) {
        for(Rate_Card__c rc : rateCards) {
            SObjectField f = SObjectType.Rate_Card__c.fields.getMap().get(item.PricebookEntry.Product2.Name + '__c');
            if(f != null) {
                rate = (Decimal)rc.get(f);
                break;
            }
        }
    }
    return rate;
	}
}

tl:dr - Please let me know why the Visualforce page won't call the GetRate method from the custom controller.
SubratSubrat (Salesforce Developers) 
Hello ,

It looks like the issue is that the getRate() method is defined in the custom controller extension, but the Visualforce page is using the Opportunity standard controller instead of the custom controller.

To fix this, you can modify your Visualforce page to use the custom controller extension instead of the Opportunity standard controller. You can do this by replacing standardController="Opportunity" with controller="RateCardCalculations" in your apex:page tag.
 
<apex:page controller="RateCardCalculations">

If the above information helps , please mark this as best answer.
Thank you.
Ryno LourensRyno Lourens
Hi Subrat,

I added the RateCardCalculations as a Controller Extension at the start of the Visualforce page:
 
<apex:page standardController="Opportunity" extensions="RateCardCalculations">

Did I add the extension incorrectly? I need the viuslaforce page to interact with the Opportunity object, as well as pulling information from the RateCard custom object.
SubratSubrat (Salesforce Developers) 
Hi Ryno ,

I noticed one more thing that In your Visualforce page, you're trying to call the getRate method from your custom controller like this:
 
<apex:outputText value="{0, number, currency}">
    <apex:param value="{!getRate(item)}"/>
</apex:outputText>


However, in your custom controller, the method is actually named GetRate with a capital "G".
<apex:outputText value="{0, number, currency}">
    <apex:param value="{!GetRate(item)}"/>
</apex:outputText>


Can you try this and let me know further .
Thank you.
Ryno LourensRyno Lourens
Hi Subrat,

Unfortunately that did not make a difference. When I try to save the Visualforce page with any combination of the spelling of GetRate, I get the same error:

'Error: Unknown function GetRate. Check spelling'

Thanks again!