[SalesForce] Error: Unknown property using Visualforce component

The below codes work fine in a brand new VF page as shown below:

<apex:page standardController="Employee__c" >
    <c:GenericHistoryComponent my_Object="{!Employee__c}" record_Limit="50"></c:GenericHistoryComponent>
</apex:page>

But my question is, how would you make it work to an existing VF page?

Getting this error:

Error: Unknown property

<apex:page standardController="Visitor__c">
 .....
 <c:GenericHistoryComponent my_Object="{!Employee__c}" record_Limit="50"> </c:GenericHistoryComponent>
</apex:page>

My component looks like this:

<apex:component controller="GenericHistoryComponentController">

    <!-- Attribute Definition -->
    <apex:attribute name="myObject" description="Object we wish to view the history of" type="SObject" required="true" assignTo="{!myObject}" />
    <apex:attribute name="recordLimit" description="Number of lines of history to display" type="Integer" required="false" assignTo="{!recordLimit}" />

    <apex:form >
        <!-- Object History Related List -->
        <apex:pageBlock id="historyTable" title="{!objectLabel} History">
            <apex:pageBlockTable value="{!ObjectHistory}" var="History" >
                <apex:column headerValue="Date"  value="{!History.thedate}"/>
                <apex:column headerValue="User">
                    <apex:outputLink value="/{!History.userId}"> {!History.who} </apex:outputLink> 
                </apex:column>
                <apex:column headerValue="Action"><apex:outputText escape="false" value="{!History.action}"/></apex:column>
            </apex:pageBlockTable>

            <apex:outputPanel rendered="{!tableSize > rowCount}">
               <div class="pShowMore">
                    <apex:commandLink value="Show more »" rerender="historyTable" action="{!refreshObjectHistory}">
                    </apex:commandLink>
                    &nbsp;|&nbsp;
                    <a href="/_ui/common/history/ui/EntityHistoryFilterPage?id={!myObjectId}">Go to list&nbsp;»</a>
               </div>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:component>

Best Answer

Assuming the component and its GenericHistoryComponentController will work with any type of object, you just need to supply an SObject reference. For a standard controller. That reference is given the same name as the type that is specified by the standardController attribute.

So for this to work with your existing page it's:

<apex:page standardController="Visitor__c">
     ...
     <c:GenericHistoryComponent my_Object="{!Visitor__c}" record_Limit="50">
     </c:GenericHistoryComponent>
</apex:page>

PS

On the subject of displaying the history of a different object, if there is a field reference to one instance (i.e. a parent) you could do this:

<apex:page standardController="Visitor__c">
     ...
     <c:GenericHistoryComponent my_Object="{!Visitor__c.Employee__r}" record_Limit="50">
     </c:GenericHistoryComponent>
</apex:page>

but if not you will have to write a controller extension that provides a property to the single instance.

Related Topic