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
Bryan Jimenez 5Bryan Jimenez 5 

How can I add a controller extension to apex class

Hi Everyone,

I am having trouble adding a standardcontroller "Mclabs2__Sale" to my Custom controller below.

Can someone guide me in the right direction?
 
global with sharing class ModalFlowAttachment{

    global Flow.Interview.attachmentFlow2 myflow { get; set; }
    global string varAttachmentParentId = 'init';

    global ModalFlowAttachment(){
        myflow  = new Flow.Interview.attachmentFlow2(new map<string, object>{'varAttachmentParentId'=> ''});
    }
    global string getVarAttachmentParentId() {
        if(varAttachmentParentId == 'init'){
            varAttachmentParentId = '';
            return '';
        }
        return myflow.varAttachmentParentId;
    }
    global Attachment attachment {
        get {
            if (attachment == null)
                attachment = new Attachment();
            return attachment;
        }
        set;
    }
    global PageReference upload() {

        attachment.OwnerId = UserInfo.getUserId();
        attachment.IsPrivate = true;
        attachment.parentId = getVarAttachmentParentId();

        try {
            insert attachment;
        } 
        catch (DMLException e) {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
            return null;
        } 
        finally {
            attachment = new Attachment();
        }

        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
        varAttachmentParentId = null;
        return null;
    }

 
Lokesh KumarLokesh Kumar
Hi Bryan,

Use your custom controller as an extension and standardcontroller like

 <apex:page standardController = "Account" extensions="myCustomController">
</apex:page>

OR

You should have a controller variable in your custom controller class and you should set that member variable to the inbound controller.  like so:
 
public class MyCustomController{
 
 private final ApexPages.StandardController controller;
public MyCustomController(ApexPages.StandardController controller){
  this.controller = controller;
}
}
 
Anything you wish to initialize you'll need to add to the constructor that the vf page uses (it always uses the one with a controller, whether a standardcontroller or standardsetcontroller)

Thanks!