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
Michael Clarke 36Michael Clarke 36 

My first controller extension

Hi, I am learning skills as a developer and I have a use case for what I have learnt.
The Case object has parent Opportunity, and the Opportunity object has children Opportunity_Split__c.

I would like to show Opportunity Splits on the Case page using the Lightning App Builder.

My understanding is that for a visualforce page to appear on a Case Page it needs to utilise the Case standard controller. Therefore I need to extend the standard controller with an extension.

Visualforce code is:
<apex:page standardController="Case" extensions="Case_ListOppSplits_Controller" lightningStylesheets="true">
    <apex:pageBlock title="Opportunity Splits">
    	<apex:pageBlockTable value="{!Opportunity_Splits}" var="oppSplit">
            <apex:column value="{!oppSplit.Name}"/>
            <apex:column value="{!oppSplit.Split_Loan_Amount__c}"/>
            <apex:column value="{!oppSplit.Rate_Type__c}"/>
            <apex:column value="{!oppSplit.Repayment_Type__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Case Controller extension is:
public class Case_ListOppSplits_Controller {

    public ApexPages.StandardSetController setController{
        get{
            if(setController==NULL){
		        Case currentCase = [SELECT Id, Subject, Opportunity__c FROM Case WHERE Id =: ApexPages.currentPage().getParameters().get('id')];
                setController = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Id, Name, Opportunity__c, Loan_Purpose__c, Loan_Type__c, Loan_Usage__c, Rate_Type__c, Repayment_Type__c, Split_Loan_Amount__c
                     FROM Opportunity_Split__c
                     WHERE Opportunity__c =: currentCase.Opportunity__c]));
            }
            return setController;
        }
        set;
    }

//initialise setController  and return  a list of records
    public list<Opportunity_Split__c> getOpportunity_Splits(){
        return (List<Opportunity_Split__c>) setController.getRecords();
    }
}
The visualforce page cannot be saved as it throws this error: Unknown constructor 'Case_ListOppSplits_Controller.Case_ListOppSplits_Controller(ApexPages.StandardController controller)'

Am I anywhere near a solution or is my understanding way off the mark?

Your help in solving this, and expanding my knowledge, would be very much appreciated.


 
AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

It appears that this isn't actually a custom controller, but rather a controller extension. You should be using the below If you don't need standard controller functionality. 

<apex:page standardController="Case" controller ="Case_ListOppSplits_Controller" lightningStylesheets="true">

If you're really trying to write a controller extension, then your extension needs to have a constructor that takes an argument of ApexPages.StandardSetController.
 
public ApexPages.StandardSetController stdctrl {get; set;}  
public setController(ApexPages.StandardSetController controller) {         stdctrl = controller;     }

If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep
Michael Clarke 36Michael Clarke 36
Hi Anudeep,
Based on your answer I think I need a standard controller and controller extension.
I have to have standardController = 'Case' because I want to place the vf page on the Case record page. (That is my understanding)
I believe I need a standard controller extension to bring through the Opp Splits data.

So how would I incorporate your code into mine?
Michael Clarke 36Michael Clarke 36
This seems to work, now just to work out how to get the vf page onto the Case Lightning page. The vf page is not appearing as an option on the vf component on the Lightning app builder.
public class Case_ListOppSplits_Controller {

    public Case myCase;
    
    public Case_ListOppSplits_Controller(ApexPages.StandardController stdController){
        this.myCase = (Case)stdController.getRecord();

    }

//initialise setController  and return  a list of records
    public list<Opportunity_Split__c> getOpportunity_Splits(){
        Case currentCase = [SELECT Id, Subject, Opportunity__c FROM Case WHERE Id =: ApexPages.currentPage().getParameters().get('id')];
        List<Opportunity_Split__c> OppSplit = [SELECT Id, Name, Opportunity__c, Loan_Purpose__c, Loan_Type__c, Loan_Usage__c, Rate_Type__c, Repayment_Type__c, Split_Loan_Amount__c
                     FROM Opportunity_Split__c
                     WHERE Opportunity__c =: currentCase.Opportunity__c];
        return OppSplit;
    }
    
}