+ Start a Discussion
Michaela PrambsMichaela Prambs 

Creating a controller extension

Hello everybody,

does anyone have the code of the "SpeakerControllerExtension"-tab?
I think the order of my code is just wrong...

Thanks a lot
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help u

Step 2: Extend the Data Model

In this step, you add two fields to the Speaker object: Picture_Path to store the location of the picture on the server, and Picture, a Formula field used to display the image in the Visualforce page.

In Setup, select Build > Create > Objects, and click the Speaker link.

In the Custom Fields & Relationships section, click New, and create a Picture_Path field defined as follows:

Data Type: Text
Field Label: Picture Path
Length: 255
Field Name: Picture_Path

Click Next, Next, Save & New.

Create a Picture field defined as follows:

Data Type: Formula
Field Label: Picture
Field Name: Picture
Formula Return Type: Text
Formula: IMAGE(Picture_Path__c, '')

Click Next, Next, Save.
Michaela PrambsMichaela Prambs
Thank you for your answer but I already know this page.
I need the right code for the whole "SpeakerControllerExtension"-tab.
Amit Chaudhary 8Amit Chaudhary 8
You can Copy VF page from above link. Then you need to create one custom tab.
Setup- > tab - > then VF page tab.

Kannan N 10Kannan N 10
Use this link & read the comments below... 


Let know if this works...
Michaela PrambsMichaela Prambs
Now I have the following error:
Kannan N 10Kannan N 10
@Michaela :  Did you try the comments posted in the link page... The code has some typo and subsequently a few revised links are provided, such as this one: https://gist.github.com/ccoenraets/0ce6978905802731e2b1    Is it not helping?
Michaela PrambsMichaela Prambs
@Kannan: yes I tried all the comments posted in the link page. So now I tried the code you were providing me on your last link but still the same error message :(
Marina FinotelliMarina Finotelli
My code wasn't working either. And I was also following the instructions according to trailhead link https://developer.salesforce.com/trailhead/project/salesforce_developer_workshop/creating_controller_extension

What made it work was fixing {!picture} to {!Speaker__c.Picture__c} on SpeakerForm.vfp

I also applied the SpeakerForm Visualforce Page to New and Edit links:
To do so, go to Stepup > Create > Objects > Speaker > Section: Buttons, Links, and Actions > Edit: Edit (do the same for New) > Override with: Visualforce Page: SpeakerForm[SpeakerForm]

Here is my final code for SpeakerForm.vfp
<apex:page standardController="Speaker__c" extensions="SpeakerControllerExtension">

<apex:form >
    <apex:pageBlock title="Edit Speaker">
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!Speaker__c.First_Name__c}"/>
            <apex:inputField value="{!Speaker__c.Last_Name__c}"/>
            <apex:inputField value="{!Speaker__c.Email__c}"/>
            <apex:inputField value="{!Speaker__c.Bio__c}"/>
            <apex:inputFile value="{!Speaker__c.Picture__c}" accept="image/*" />
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>

Here is my final code for SpeakerControllerExtension.apxc (which looks the same as the one shared by Kannan)
public class SpeakerControllerExtension {

    public blob picture { get; set; }
    public String errorMessage { get; set; }

    private final Speaker__c speaker;
    private ApexPages.StandardController stdController;

    public SpeakerControllerExtension(ApexPages.StandardController stdController) {
        this.speaker = (Speaker__c)stdController.getRecord();
        this.stdController = stdController;

    public PageReference save() {
        errorMessage = '';
        try {
            upsert speaker;
            if (picture != null) {
                Attachment attachment = new Attachment();
                attachment.body = picture;
                attachment.name = 'speaker_' + speaker.id + '.jpg';
                attachment.parentid = speaker.id;
                attachment.ContentType = 'application/jpg';
                insert attachment;
                speaker.Picture_Path__c = '/servlet/servlet.FileDownload?file='
                                          + attachment.id;
                update speaker;
            return new ApexPages.StandardController(speaker).view();
        } catch(System.Exception ex) {
            errorMessage = ex.getMessage();
            return null;