3

I have a controller extension and I get the unknown property error on page load.

public with sharing class ExtensionController{  
       public ApexPages.StandardController standardCon;    
       public ExtensionController(ApexPages.StandardController con){  
          standardCon = con;  
       }    
       public void populateRelAccFields(){  
          Opportunity opp = (Opportunity) standardCon.getRecord();  
          opp.Account = [select Phone, Type from Account where id =: opp.AccountId];  
       } 
    } 
1
  • 3
    Welcome to SFSE. I updated the Title, as things like 'whats wrong with my code', or 'help me fix this' might deter people from even looking at the question Oct 7, 2013 at 16:15

1 Answer 1

2

By default, when using the getRecord() method, only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression. So without having your VF, lets just assume you need to do separate SOQL statements to ensure you have all the fields that you need.

You need to create a property in the extension

Public Opportunity opp      {get;set;}
Public Account acc          {get;set;}

public ExtensionController(ApexPages.StandardController con){
     standardCon = con;
     this.opp = (Opportunity) standardCon.getRecord();
     opp = [Select Id, Name, AccountId, Whatever Fields you want 
            From Opportunity 
            Where Id =: opp.id];
     acc = [Select Id, Name, Whatever fields you want 
            From Account
            Where Id =: opp.AccountId];

}

http://www.salesforce.com/us/developer/docs/pages/Content/apex_ApexPages_StandardController_getRecord.htm

5
  • NOW AM GETTING ERROR LIKE System.QueryException: List has no rows for assignment to SObject Error is in expression '{!populateRelAccFields}' in page opptrial
    – ranadheer
    Oct 7, 2013 at 15:38
  • 1
    Can you update the question with the exact code you are using Oct 7, 2013 at 15:40
  • Without knowing the use case, I assume you want to have access to both the opportunity and the Account within the class. I have updated the code above to reflect Oct 7, 2013 at 15:56
  • You could get all of those account fields from the single opp query--Account.Name,Account.Website, etc.
    – greenstork
    Oct 7, 2013 at 15:59
  • Very True. You are correct. I just didnt know if he needed access to the Account object or not. If he doesnt need access to the object, then yes, that would work Oct 7, 2013 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .