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
jcaldjcald 

How do I reference a custom number field from an Account in Apex?

How do I reference a custom field from an Account in Apex?
I am trying to reference a custom field (Account) from an apex code in a VF Opportunity page. I've tried using a lookup relationship both way (Account to Opportunity) (Opportunity to Account) and it is saying "Error: Incorrect pararmeter......Expected Number, recieved Text"

my apex code:

<Apex:outputtext value="{!1.04*Opportunity.Amount__c*Opportunity.Factor_Rate__c} " />

Opportunity.Factor_Rate__c is related to a custom field in Account. Please help . Thanks
bob_buzzardbob_buzzard
Have you checked your field definitions to make sure that the Amount__c and Factor_Rate__c fields are both defined as Number - the error message suggests that one of them is defined as Text.
jcaldjcald
Opportunity.Opp_Amount__c is Number and Opportunity.Opp_Factor_Rate__c is a  Lookup (Account).

I've used Account.Factor_Rate__c (number) in replacement of Opportunity.Opp_Factor_Rate__c and included <apex:page standardController="Opportunity" extensions="Account">. This is giving me an error: Unknown property 'OpportunityStandardController.Account'

Thanks for the help.



bob_buzzardbob_buzzard
You can do this using a standard controller (and that isn't how you use extension controllers, but that's another topic).  Get rid of the extensions from the apex:page and change your text output to:

<Apex:outputtext value="{!1.04*Opportunity.Amount__c*Opportunity.Opp_Factor_Rate__r.Factor_Rate__c} " />

What I'm doing here is following the reference to the associated account (via the __r notation) and then accessing a field from that account.

jcaldjcald
I believe that will work. It didn't give me any errors. Now, i have too relate that formula to a particular account. Any help with that?

I appreicate the help. Thanks!
bob_buzzardbob_buzzard
That formula will follow the relationship for the account associated with the opportunity - do you want to pull the information from a different account (i.e. one that isn't related to the opportunity)
jcaldjcald
My main goal is to reference a column for a particular account with their account information.

<thead>
                <tr>
                    <th style="font-size: 12px" bgcolor="#202d79">24 Months</th>
                    <th style="font-size: 12px"><font color="#000">   
                    <apex:outputtext value="{!1.04*Opportunity.Finance_Amount__c*Opportunity.Acct_Factor_Rate__r.Factor_Rate__c}"/>
                    </font></th>
                    <th>empty</th>
                </tr>
</thead>
bob_buzzardbob_buzzard
I don't really understand this - your page is using an opportunity standard controller which will be related to an account.  How does the column relate to an account?
bob_buzzardbob_buzzard
How do you know what that different account is?  All you have is the opportunity at the moment.
jcaldjcald
I guess that's my second question - How do I reference a custom number field from an Account in Apex?

Is there a way to reference an account through apex attributes?
bob_buzzardbob_buzzard
You'd need to write an extension controller that retrieves the account and makes it available to the page.  You'd some way to know which account you want to retrieve though.
jcaldjcald
ok. so i've been reading up on extension controller. Please let me know if this is somewhat correct.


public class MyAcctController {
   public Account__c accountid{ get; private set;

      public MyAcctController(ApexPages.StandardController) {    
   accountid = (Account__c).getRecord();    
  }   
public PageReference save()      {      
    accountid.Account__c = '001i000000UP1gF;                                
}                                           
}


<apex:page standardController="Opportunity" extensions="MyAcctController">
<apex:pageblock>
<apex:outputtext value="{!1.04**Opportunity.Amount__c*Account__c.Factor_Rate__c}"/>
</apex:pageblock>
</apex:page>