Contents[Hide]

Additional objects in your controller.

If your standard controller is related to Accounts and you want additionally use another object in your custom controller just add it as public with standard get; set; methods.

Controller:

// Variables
public End_User__c endUserObj { get; set; }

// Assign record details to variable
endUserObj = [SELECT Id, Name, phone__c FROM End_User__c WHERE Id = :variableWithId ];

// Value input by user can be read by
String phone = endUserObj.phone__c;

then on VF Page you can access object fields as simple as:

<apex:inputField label="Phone" value="{!endUserObj.phone__c}"/> 

Empty Object for fields types only

Let suppose that we want to display "Bussines Unit" Picklist field from User object and read value chosen by user.

Apex code:

// Variables
public User userObj {get; set;}

// Constructor
public myPage_controller() {
    // Assign new empty user object to userObj varieble. Otherwise we will get nullpointer exception when try to read the value in next step
    userObj = new User(); step.
}

public PageReference doShowBUUsers() {
    String bu = userObj.Business_Unit__c;
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, bu)); // Show as error mesage
    return null;
}

Visualforce:

<apex:page Controller="ptPictureBook_controller">
    <apex:form >
    <apex:pageBlock >
        <apex:pageMessages id="errorMsg" />
        <apex:pageBlockSection >      
            <apex:inputField value="{!UserObj.Business_Unit__c}"/>
            <apex:commandButton value="Show" action="{!doShowBUUsers}"/>            
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Thats easiest way to get standard picklist and chosen value without to much work.

   
© Paweł Woźniak