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
Patrick J O'SullivanPatrick J O'Sullivan 

System.TypeException: Invalid conversion from runtime type SOBJECT:Parent_object__c to SOBJECT:Child_Object__c

I am trying to create an extension for my Key_Account_Overview__c object pulling specifically filtered lists from my Kam_Plan_Actions__c object which is a master detail child of Key_Account_Overview__c.

my extension class (KAMKAPextension) seems to have no errors and neither does my VF page (KAMbasic) but when I render the VF page I get:

Visualforce Error
Help for this Page

System.TypeException: Invalid conversion from runtime type SOBJECT:Key_Account_Overview__c to SOBJECT:Kam_Plan_Actions__c
Class.KAMKAPextension.<init>: line 14, column 1

I am pretty sure I have made an illegal assignment in line 14 but can't figure it out, I am trying to teach myself APEX currently so it may be a complete newbie mistake but I just don't know enough yet to spot my mistake.

this.KAP = (Key_Account_Overview__c)KAPController.getRecord();

any help would be much appreciated, the code for my class and page are below, please ignore the place holder Items I am focusing on the lists first and the rest were injected for proposal demo purposes for my COO.

Extension class:
public class KAMKAPextension 
{
  private final Kam_Plan_Actions__c KAP;
    List<Kam_Plan_Actions__c> overdue;
    List<Kam_Plan_Actions__c> next30days;
    List<Kam_Plan_Actions__c> from30to60days;
    List<Kam_Plan_Actions__c> from60to90days;
    List<Kam_Plan_Actions__c> longterm;
    List<Kam_Plan_actions__c> Completed;
    List<Kam_Plan_actions__c> Postponed;
    
  public KAMKAPextension(ApexPages.StandardController
								KAPController) {
								this.KAP = (Key_Account_Overview__c)KAPController.getRecord();
								}
  Public list<Kam_Plan_Actions__c>
        getoverdue() {
            If (overdue == null) 
            {
                overdue = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c < TODAY AND Status__c !='Complete' AND Status__c !='Cancelled'];
            }
            return overdue;
        }
    Public list<Kam_Plan_Actions__c>
        getnext30days() {
            If (next30days == null) 
            {
                next30days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c >= TODAY AND due_date__c = NEXT_N_DAYS:29 AND Status__c !='Complete' AND Status__c !='Cancelled'];
            }
            return next30days;
        }
    Public list<Kam_Plan_Actions__c>
        getfrom30to60days() {
            If (from30to60days == null) 
            {
                from30to60days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c !=NEXT_N_DAYS:29  AND due_date__c = NEXT_N_DAYS:59 AND Status__c !='Complete' AND Status__c !='Cancelled'];
            }
            return from30to60days;
        }
    Public list<Kam_Plan_Actions__c>
        getfrom60to90days() {
            If (from60to90days == null) 
            {
                from60to90days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c !=NEXT_N_DAYS:59  AND due_date__c = NEXT_N_DAYS:89 AND Status__c !='Complete' AND Status__c !='Cancelled'];
            }
            return from60to90days;
        }
    Public list<Kam_Plan_Actions__c>
        getlongterm() {
            If (longterm == null) 
            {
                longterm = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c > TODAY AND due_date__c != TODAY AND due_date__c !=NEXT_N_DAYS:89 AND Status__c !='Complete' AND Status__c !='Cancelled'];
            }
            return longterm;
        }
  
}

VF Page:
<apex:page showHeader="true" docType="html-5.0" StandardController="Key_Account_Overview__c" extensions="KAMKAPextension">  
<!-- Style Override -->
     <style>
        .activeTab {background-color: blue; color:white; font-style:italic; font-weight:bold;
        background-image:none}
        .inactiveTab { background-color: lightblue; color:black;font-style:normal; font-weight:normal;
        background-image:none}
    </style>

<!---Tab Panel: -->
        <apex:tabPanel switchType="client" selectedTab="tabdetails"
        id="AccountTabPanel" tabClass="activeTab"
        inactiveTabClass="inactiveTab">
<!-- Individual Tabs -->
            <apex:tab label="Executive Summary" name="ExSum" id="tabExec">
<chatter:follow entityId="{!Key_Account_Overview__c.id}"/>
                <chatter:followers entityId="{!Key_Account_Overview__c.id}"/>
                <apex:pageBlock title="Executive Summary">
                    <Apex:outputField label="Executive Overview" value="{!Key_Account_Overview__c.Executive_Summary__c}"/>
                </apex:pageBlock>
                <apex:pageBlock title="Financial variance">
                  <br>Financial Variance percentages goe here:</br>
                  <br>1. Estimate v Target</br>
                  <br>2. Actual v Target</br>
                  <br>3. Actual v Estimate <Apex:outputField label="Actual v Estimate" value="{!Key_Account_Overview__c.Financials_Variance__c}"/></br>
                </apex:pageBlock>
            </apex:tab>
            <apex:tab label="Chatter">
                <chatter:feed entityId="{!Key_Account_Overview__c.id}"/>
            </apex:tab>
            <apex:tab label="Company Profile" name="CompanyProfile" id="tabComPro">
                <apex:pageBlock title="Company information">
                    <Apex:outputField value="{!Key_Account_Overview__c.Company_Information__c}"/>
                </apex:pageBlock>
                <apex:pageBlock title="Financials">
                  <br>"Target Financials:    "<apex:outputfield label="Target Financials" value="{!Key_Account_Overview__c.Target_Financials__c}"/> </br>
                  <br>"financials last Year:    "<apex:outputfield label="financials last Year" value="{!Key_Account_Overview__c.Financials_last_year__c}"/></br>
                  <br>"financials this Year:    "<apex:outputfield label="financials this Year" value="{!Key_Account_Overview__c.Financials_this_year__c}"/></br>
                </apex:pageBlock>
               <apex:pageBlock title="Port Call Data">
                   <br><apex:outputfield label="Port calls this year" value="{!Key_Account_Overview__c.Port_Calls_this_year_to_date__c}"/> </br>
                   <br><apex:outputfield label="Port calls last year" value="{!Key_Account_Overview__c.Port_Calls_last_year__c}"/> </br>
                   <br><apex:outputfield label="Port Call Variance" value="{!Key_Account_Overview__c.Port_Calls_variance__c}"/> </br>
               </apex:pageBlock>
            </apex:tab>
            <apex:tab label="Key Progress Indicators" name="KPI"
            id="tabKPI">
            <apex:relatedList subject="{!Key_Account_Overview__c}" list="Account_Analyses__r" />
            <apex:relatedList subject="{!Key_Account_Overview__c}" list="QCR_Grids__r" />
            <apex:relatedList subject="{!Key_Account_Overview__c}" list="Objective_Progress_Trackers__r" />
            </apex:tab>
            <apex:tab label="Customer Objectives" name="CustomerObjectives"
            id="tabCusObj">
            <apex:pageBlock title="Customer Objectives">
            <br><b>Primary Objectives</b><apex:outputfield value="{!Key_Account_Overview__c.Primary_Customer_objectives__c}"/> </br>
<br><b>Secondary Objectives </b><apex:outputfield value="{!Key_Account_Overview__c.Secondary_Customer_objectives__c}"/> </br>
</apex:pageBlock>
            </apex:tab>
            <apex:tab label="KAM Objectives"
            name="KAMObjectives" id="tabKAMObj">
            <apex:pageBlock >
<br><b>Kam Objectives </b><apex:outputfield label="Primary cust. objectives" value="{!Key_Account_Overview__c.KAM_Objectives__c}"/> </br>

</apex:pageBlock>
            </apex:tab>
<!-- KAM Plan tab Accordian style -->
            <Apex:tab label="KAM Plan" Name="KAMPlan" id="KAMP">
            <apex:form >
            <apex:pageblock title="KAM Plan">
            <br>KAM Mission overview goes here....</br>
            </apex:pageblock>
            <apex:pageblock >  
        <c:Accordion >
            <c:AccordionSection title="Overdue" >
            	<apex:pageBlockTable value="{!overdue}"  var="kact">
                    <apex:column Headervalue="Kam Action" value="{!kact.name}"/>
                </apex:pageBlockTable>
            </c:AccordionSection>
            <c:AccordionSection title="Next 30 days">
            </c:AccordionSection>
            <c:AccordionSection title="Next 60 days">
            </c:AccordionSection>
            <c:AccordionSection title="Next 90 days">
            </c:AccordionSection>
            <c:AccordionSection title="Long Term View">
            </c:AccordionSection>
    </c:Accordion>
    </apex:pageblock>
            </apex:form>
            </apex:tab>
        </apex:tabPanel>

 </apex:page>

Best Answer chosen by Patrick J O'Sullivan
Pavan Kumar KajaPavan Kumar Kaja
Hi Pratik,

IN page standard controller is 'Key_Account_Overview__c' and you are getting the information using getRecord() method but you declared KAP as 

private final Kam_Plan_Actions__c KAP;

in 2 lin eof your class

So change this line to 

public  Key_Account_Overview__c KAO;

and in your methods use this id to get Kam_Plan_Actions__c records related to Key_Account_Overview__c


SELECT Id, Name, Status__c, Due_Date__c
                    FROM Kam_Plan_Actions__c
                    WHERE due_date__c < TODAY AND Status__c !='Complete' AND Status__c !='Cancelled'  AND  Key_Account_Overview__c =:KAO.id


Here  Key_Account_Overview__c(Change api name according to urs) is MD relation with Kam_Plan_Actions__c

Finally ur class will be looks like below

public class KAMKAPextension{
	Public Key_Account_Overview__c KAO;
    List<Kam_Plan_Actions__c> overdue;
    List<Kam_Plan_Actions__c> next30days;
    List<Kam_Plan_Actions__c> from30to60days;
    List<Kam_Plan_Actions__c> from60to90days;
    List<Kam_Plan_Actions__c> longterm;
    List<Kam_Plan_actions__c> Completed;
    List<Kam_Plan_actions__c> Postponed;
    
	public KAMKAPextension(ApexPages.StandardController KAPController) {
		this.KAO = (Key_Account_Overview__c)KAPController.getRecord();
	}
	
	Public list<Kam_Plan_Actions__c>
        getoverdue() {
            If (overdue == null) 
            {
                overdue = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c < TODAY AND Status__c !='Complete' AND Status__c !='Cancelled'  AND  Key_Account_Overview__c =:KAO.id];
            }
            return overdue;
        }
    Public list<Kam_Plan_Actions__c>
        getnext30days() {
            If (next30days == null) 
            {
                next30days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c >= TODAY AND due_date__c = NEXT_N_DAYS:29 AND Status__c !='Complete' AND Status__c !='Cancelled' AND  Key_Account_Overview__c =:KAO.id];
            }
            return next30days;
        }
    Public list<Kam_Plan_Actions__c>
        getfrom30to60days() {
            If (from30to60days == null) 
            {
                from30to60days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c !=NEXT_N_DAYS:29  AND due_date__c = NEXT_N_DAYS:59 AND Status__c !='Complete' AND Status__c !='Cancelled' AND  Key_Account_Overview__c =:KAO.id];
            }
            return from30to60days;
        }
    Public list<Kam_Plan_Actions__c>
        getfrom60to90days() {
            If (from60to90days == null) 
            {
                from60to90days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c !=NEXT_N_DAYS:59  AND due_date__c = NEXT_N_DAYS:89 AND Status__c !='Complete' AND Status__c !='Cancelled' AND  Key_Account_Overview__c =:KAO.id];
            }
            return from60to90days;
        }
    Public list<Kam_Plan_Actions__c>
        getlongterm() {
            If (longterm == null) 
            {
                longterm = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c > TODAY AND due_date__c != TODAY AND due_date__c !=NEXT_N_DAYS:89 AND Status__c !='Complete' AND Status__c !='Cancelled' AND  Key_Account_Overview__c =:KAO.id];
            }
            return longterm;
        }
  
}




All Answers

kiranmutturukiranmutturu
private final Kam_Plan_Actions__c KAP;--> this should be private final Key_Account_Overview__c KAP;
Pavan Kumar KajaPavan Kumar Kaja
Hi Pratik,

IN page standard controller is 'Key_Account_Overview__c' and you are getting the information using getRecord() method but you declared KAP as 

private final Kam_Plan_Actions__c KAP;

in 2 lin eof your class

So change this line to 

public  Key_Account_Overview__c KAO;

and in your methods use this id to get Kam_Plan_Actions__c records related to Key_Account_Overview__c


SELECT Id, Name, Status__c, Due_Date__c
                    FROM Kam_Plan_Actions__c
                    WHERE due_date__c < TODAY AND Status__c !='Complete' AND Status__c !='Cancelled'  AND  Key_Account_Overview__c =:KAO.id


Here  Key_Account_Overview__c(Change api name according to urs) is MD relation with Kam_Plan_Actions__c

Finally ur class will be looks like below

public class KAMKAPextension{
	Public Key_Account_Overview__c KAO;
    List<Kam_Plan_Actions__c> overdue;
    List<Kam_Plan_Actions__c> next30days;
    List<Kam_Plan_Actions__c> from30to60days;
    List<Kam_Plan_Actions__c> from60to90days;
    List<Kam_Plan_Actions__c> longterm;
    List<Kam_Plan_actions__c> Completed;
    List<Kam_Plan_actions__c> Postponed;
    
	public KAMKAPextension(ApexPages.StandardController KAPController) {
		this.KAO = (Key_Account_Overview__c)KAPController.getRecord();
	}
	
	Public list<Kam_Plan_Actions__c>
        getoverdue() {
            If (overdue == null) 
            {
                overdue = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c < TODAY AND Status__c !='Complete' AND Status__c !='Cancelled'  AND  Key_Account_Overview__c =:KAO.id];
            }
            return overdue;
        }
    Public list<Kam_Plan_Actions__c>
        getnext30days() {
            If (next30days == null) 
            {
                next30days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c >= TODAY AND due_date__c = NEXT_N_DAYS:29 AND Status__c !='Complete' AND Status__c !='Cancelled' AND  Key_Account_Overview__c =:KAO.id];
            }
            return next30days;
        }
    Public list<Kam_Plan_Actions__c>
        getfrom30to60days() {
            If (from30to60days == null) 
            {
                from30to60days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c !=NEXT_N_DAYS:29  AND due_date__c = NEXT_N_DAYS:59 AND Status__c !='Complete' AND Status__c !='Cancelled' AND  Key_Account_Overview__c =:KAO.id];
            }
            return from30to60days;
        }
    Public list<Kam_Plan_Actions__c>
        getfrom60to90days() {
            If (from60to90days == null) 
            {
                from60to90days = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c !=NEXT_N_DAYS:59  AND due_date__c = NEXT_N_DAYS:89 AND Status__c !='Complete' AND Status__c !='Cancelled' AND  Key_Account_Overview__c =:KAO.id];
            }
            return from60to90days;
        }
    Public list<Kam_Plan_Actions__c>
        getlongterm() {
            If (longterm == null) 
            {
                longterm = [
                    SELECT Id, Name, Status__c, Due_Date__c 
                    FROM Kam_Plan_Actions__c 
                    WHERE due_date__c > TODAY AND due_date__c != TODAY AND due_date__c !=NEXT_N_DAYS:89 AND Status__c !='Complete' AND Status__c !='Cancelled' AND  Key_Account_Overview__c =:KAO.id];
            }
            return longterm;
        }
  
}




This was selected as the best answer
Patrick J O'SullivanPatrick J O'Sullivan
Thanks Guys you both were right, am selecting  ashi's as best answer because the examples might help comeone else in the future.