• Michael Clarke 36
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies
Hi, I have built my first vf page and controller extension and it works fine in Sandbox.
How do I create a Test class for this?

Visualforce Page:
<apex:page standardController="Case" extensions="Case_ListOppSplits_Controller" lightningStylesheets="true">
    <apex:pageBlock>
    	<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>

Controller Extension Class:
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;
    }
    
}

​​​​​​​
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.


 
Hi, I am new to Apex

I have copied an example trigger code, however I get the error message 'Variable does not exist: addError'.
All it does is prevent deletion of an orphan contact with a custom error msg.
Can you tell me what I am doing wrong?

trigger ContactBeforeDelete on Contact (before delete)
{
    for(Contact c:trigger.old)
    {
        if(c.accountId==null)
        {
            c.addError=('You are not authorised to delete this contact.');
        }
    }
}
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.


 
Hi, I am new to Apex

I have copied an example trigger code, however I get the error message 'Variable does not exist: addError'.
All it does is prevent deletion of an orphan contact with a custom error msg.
Can you tell me what I am doing wrong?

trigger ContactBeforeDelete on Contact (before delete)
{
    for(Contact c:trigger.old)
    {
        if(c.accountId==null)
        {
            c.addError=('You are not authorised to delete this contact.');
        }
    }
}