0

I have no experience with Apex coding or programming in general, so please be very basic in your responses. I have a button on my ctOppty__c object creating a VF pdf page from my ctOppty__c object data which references ctOppty__c and child_ctOppty__c fields. The pdf output from those M/D objects is fine. My next step is to pull in contract data (ctContracts__c.Name) from a custom object, ctContracts__c on the same VF pdf page. The ctContracts__c object has a lookup relationship to/from ctOppty__c. My extension (SalesQuoteExtension) is listed below: public with sharing class SalesQuoteExtensionNEW {

    public id ctOpptyID {get; set;}
    public List<ctContracts__c> LstContracts {get; set;}
    public SalesQuoteExtensionNEW(ApexPages.StandardController controller)
    {
    ctOpptyID=ApexPages.currentPage().getParameters().get('id');
    LstContracts=new List <ctContracts__c>([
        SELECT ID, Name FROM ctContracts__c WHERE ctOppty_Name__c=:ctOpptyID
        ]);
    }
    }

VF Page:

    <apex:page StandardController="CtOppty__c" extensions="SalesQuoteExtension" applyHtmlTag="false"  showHeader="false" sidebar="false" renderAs="pdf" >
    <table>
    <tr>
        <td> <strong>Quote Name:</strong></td>
        <td> <apex:outputField value="{!CtOppty__c.Name}"/></td>
    </tr>
    <tr>
        <td> <strong>Contract Name:</strong></td>
        <td> <apex:outputField value="{!CtOppty__c.Name}"/></td>
        //the above field reference will be ctContracts.Name when I can get the extension to work properly//
    </tr>
    </table>
    </apex:page>

This is the resulting error:

Invalid conversion from runtime type CtOppty__c to ctContracts__c

As I said, I have no experience with coding and if I can create a very basic extension that can draw cross object data into a VF page, that will give me a basis to create more complex VF pages. Any help with this would be greatly appreciated.

Below is my latest explanation of what I am trying to accomplish. I am making a very stripped-down redesign of my question so I can understand exactly what is not happening. I have two custom objects (ctOppty__c & ctContracts__c) that are not related to each other except by a lookup field on the ctOppty object called Contract_Name__c. This is a lookup field to the Contract Name (Name) on the ctContracts__c object. I have a button on the ctOppty object which creates a Sales Quote from ctOppty information. I want to include ctContract information (mainly the Contract Name for now). I am having trouble creating an Apex Class extension (SalesQuoteExtensionNEW) which will pull in the ctContract information for that particular ctOppty. When I reference the ctContracts__c Name field on my Visualforce page, it gives me the following error: " Error: Unknown property 'CtOppty__cStandardController.CtContracts__c' ". Obviously, my extension is not correctly accessing my ctContracts object.

I have included stripped-down markups of what I currently have:

  public with sharing class SalesQuoteExtensionNEW {
    public id ctOpptyID {get; set;}
    public List<ctContracts__c> LstContracts {get; set;}
    public SalesQuoteExtensionNEW(ApexPages.StandardController controller)
    {
    ctOpptyID=ApexPages.currentPage().getParameters().get('id');
    LstContracts=new List <ctContracts__c>([
        SELECT ID, Name FROM ctContracts__c WHERE ctOppty_Name__c=:ctOpptyID
        ]);
    }
    }

VF Page:

    <apex:page StandardController="CtOppty__c" extensions="SalesQuoteExtensionNEW" applyHtmlTag="false" showHeader="false" sidebar="false"renderAs="pdf">
<table>
    <tr>
        <td>Quote Name:</td>
        <td><apex:outputField value="{!CtOppty__c.Name}"/></td>
    </tr>
<apex:repeat value="{!lstContracts}">
    <tr>
        <td>Contract Name:</td>
        <td><apex:outputField value="{!CtContracts__c.Name}"/></td>        
    </tr>
</apex:repeat>
</table>
</apex:page>

The following error occurs when saving the VF page: " Error: Unknown property 'CtOppty__cStandardController.CtContracts__c' "

2
  • 1
    Why do you reference different Standard Controllers in apex ctContracts__c' and Visualforce page CtOppty__c`?
    – o-lexi
    Feb 16, 2017 at 17:46
  • Please edit your question with any additional content. That's not what comments are for.
    – Adrian Larson
    Feb 17, 2017 at 21:59

1 Answer 1

0

The error here is pretty descriptive of what's going wrong. As mentioned in the comments, you are trying to retrieve the wrong type of object from the provided controller.

When your markup provides a standard controller, that determines the type of SObject returned by the StandardController.getRecord() method.

<!-- markup -->
<apex:page standardController="MyObject__c" extensions="MyExtension">

/* extension */
public MyExtension(ApexPages.StandardController controller)
{
    MyObject__c record = (MyObject__c)controller.getRecord();
    // note the type here matches the controller specified in  your markup
    // i.e. standardController="MyObject__c"
}

You have provided a different type, hence the error:

/* your version */
public MyExtension(ApexPages.StandardController controller)
{
    OtherObject__c record = (OtherObject__c)controller.getRecord();
    // note the type here does not match the controller specified in your markup
}

If you want to query for related data, you can query for it:

public Parent__c parent { get; private set; }
public List<Child__c> children { get; private set; }
public MyExtension(ApexPages.StandardController controller)
{
    parent = [SELECT Name, OtherFields__c FROM Parent__c WHERE Id IN (
        SELECT Parent__c FROM MyObject__c WHERE Id = :controller.getId()
    )];
    children = [SELECT Name FROM Child__c WHERE MyObject__c = :controller.getId()];
}

If there isn't a direct relationship, you'll have to determine how to find which records are relevant. SOQL is quite flexible.

4
  • ok...I am thoroughly confused.
    – Russ L
    Feb 17, 2017 at 20:50
  • The key is that the standard controller can only return the specified type of object. Anything else must be queried for.
    – Adrian Larson
    Feb 17, 2017 at 20:52
  • I recreated the extension to closely resemble what you showed: public class SalesQuoteExtension { public ctContracts__c parent { get;private set; } public SalesQuoteExtension(ApexPages.StandardController controller) { parent = [SELECT Name FROM ctContracts__c WHERE Id IN ( SELECT Contract__c FROM ctOppty__c WHERE Id = :controller.getId() )]; } } It saved correctly but I still get a "List has no rows for assignment to SObject " error. -Sorry...I'm not sure how to enter the markup in the Comments section.
    – Russ L
    Feb 17, 2017 at 22:04
  • @Russ don't. Edit your question instead.
    – Adrian Larson
    Feb 17, 2017 at 22:10

You must log in to answer this question.

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