2

I have two custom objects, service_case__c and part__c. I am trying to display parts on the page layout of service_case__c through a visual force page. Though I have no errors in developer console, when hitting preview, I receive the error:

Invalid conversion from runtime type Service_Case__c to Part__c 
An unexpected error has occurred. Your development organization has been notified.

Also, another way this question could be asked is: "How do I display another object's data onto a different object?" Here is the code for my controller, PartListCon:

public class PartListCon {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
      private final part__c part;

      public PartListCon(ApexPages.StandardSetController controller) {
        this.part = (part__c)controller.getRecord(); 
    }    

    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(
                    Database.getQueryLocator(
                    [SELECT Name, id from Part__c limit 1]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<part__c> getParts() {
        return (List<part__c>) setCon.getRecords();
    }
}

And here is my simple VF page:

<apex:page Standardcontroller="Service_Case__c" recordSetVar="Service_case__c" extensions="PartListCon">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!parts}" var="p">
            <apex:column value="{!p.Name}"/>
            <apex:column value="{!p.id}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

1 Answer 1

2

If you just want to display a page on the Service_Case__c layout, you don't need a StandardSetController (or recordSetVar) at all.

<apex:page standardController="Service_Case__c" extensions="MyExtension">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!parts}" var="part">
            <!-- content -->
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
public with sharing class MyExtension
{
    public List<Part__c> parts { get; private set; }
    public MyExtension(ApexPages.StandardController controller)
    {
        parts = [/*some query*/];
    }
}

As for the compile error, you have a standardController mismatch. Both types need to be the same. Also, don't name your recordSetVar the same thing as your standardController.

Either:

<apex:page standardController="Service_Case__c" recordSetVar="records" extensions="PartListCon">
    <!-- other markup -->
</apex:page>
public with sharing class PartListCon
{
    public PartListCon(ApexPages.StandardSetController controller)
    {
        Service_Case__c record = (Service_Case__c)controller.getRecord();
    }
}

Or:

<apex:page standardController="Part__c" recordSetVar="records" extensions="PartListCon">
    <!-- other markup -->
</apex:page>
public with sharing class PartListCon
{
    public PartListCon(ApexPages.StandardSetController controller)
    {
        Part__c record = (Part__c)controller.getRecord();
    }
}
1
  • 1
    Thanks man! You are very helpful. I truly appreciate it! Aug 26, 2016 at 0:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .