How to use Account Object in custom page controller ?
I did the following steps :
- I create a New Tab for Visualforce Page
setup–>app setup–> create–>tabs
clicked "New" under visualforce tab and entered details and choose the visualforce pages which i have already created. And clicked next and choose the application in which i want that tab to be included and clicked save.
-
Extend the Account – StandardController like that:
http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardcontroller.htm -
Setup the tabstyle – Attribute like the example-link like that:
https://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_std_styling.htm
My VF-Page will be init like:
<apex:page standardController="Account" extensions="OrderController" tabStyle="Order__tab">
My OrderController will be init like:
public with sharing class OrderController {
private final Account account;
public OrderController(ApexPages.StandardController controller) {
this.account = (Account)controller.getRecord();
//1.) does not work because there is no id url-parameter in url
//account = [SELECT Id, Name, Owner.FirstName FROM Account WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
if(this.account == null){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'account = null'));
}else{
//2.) all properties of the account Object are still NULL
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'account.id: ' + this.account.Id));
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'account.name: ' + this.account.name));
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'account.Owner: ' + this.account.Owner.FirstName));
//
}
}
The result of it is that the properties of this.account
like id, name, Owner are null
Why ? I need the properties of this.account
to create a valid order.
Best Answer
Create a new custom button(preferable a detail page button) on an Account object, add your custom VF page in source and add on page layout.
Because to have an Account via getRecord() method in constructor, you must give the account Id either directly in the URL or use a button as I mentioned above.