0

Trying to implement history related list on a custom visual force page, the source code is here github. The problem now i'm facing is that I'm getting an error which I'm not sure what does it means. I have the custom object name called Employee__c and my VF controller name called EmployeeController

Error:

Unknown property 'EmployeeController.Employee__c'

Page:

<c:GenericHistoryComponent recordLimit1="50" myObject1="{!Employee__c}"/>

Component:

<apex:component controller="GenericHistoryComponentController">

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

    <apex:form >
        ....
    </apex:form>
</apex:component>

Class:

public class GenericHistoryComponentController {

    // External variables
    public SObject myObject {get; set;}
    public Integer recordLimit {get; set;}
    public Integer updatedRecordLimit {get; set;}
    public Integer tableSize {get; set;}
    public static String objectLabel {get;}
    public Id myObjectId {get;set;}
    public Integer rowCount {get;set;}
    // Internal Variables
    public objectHistoryLine[] objectHistory; 

    public static final Map<String, Schema.SObjectType> mySObjectTypeMap = Schema.getGlobalDescribe();
    public static Map<String, Schema.SObjectField> myObjectFieldMap;
    public static List<Schema.PicklistEntry> historyFieldPicklistValues;

    public List<objectHistoryLine> getObjectHistory(){
        if(updatedRecordLimit != null){
            recordLimit = updatedRecordLimit;
        }else{
            updatedRecordLimit = recordLimit;
        }

    // if no object passed in, return empty list
        if (myObject == null) {
            return new List<objectHistoryLine>();
        }

        myObjectId = String.valueOf(myObject.get('Id'));
        Schema.DescribeSObjectResult objectDescription = myObject.getSObjectType().getDescribe();

        myObjectFieldMap = objectDescription.fields.getMap();
        objectLabel = String.valueOf(objectDescription.getLabel());

        //Get the name of the history table
        String objectHistoryTableName = objectDescription.getName();

        //ID field name         
        string ObjectIdName; 

        list<sObject> historyList = Database.query( 'SELECT CreatedDate,'+
                                                    'CreatedById,'+
                                                    'Field,'+
                                                    'NewValue,'+
                                                    'OldValue ' +
                                                    'FROM ' + objectHistoryTableName + ' ' +
                                                    'WHERE ' + ObjectIdName + ' =\'' + myObjectId + '\' ' +
                                                    'ORDER BY CreatedDate DESC');




         return objectHistory;
    }    

    public PageReference refreshObjectHistory(){
        updatedRecordLimit = updatedRecordLimit + recordLimit;
        getObjectHistory();
        return null;
    }


}
  • try this {!$ObjectType['Employee__c'] instead of `Employee__c – Ashwani Jan 25 '17 at 2:56
  • Error: Wrong type for attribute <c:generichistorycomponent myObject1="{!$ObjectType['Employee__c ']">. Expected SObject, found String – Nick Kahn Jan 25 '17 at 3:11
  • Value to the myObject1 attribute should be instance of sObject. In your EmployeeController, you need to create an instance of Employee__c object and then pass it to component attribute. – LakhanP Jan 25 '17 at 3:15
  • Surely not all of this code is relevant...please at least make an effort to remember the M in MVR. Minimum Viable Reproduction. – Adrian Larson Jan 25 '17 at 3:52
1

Try this -

VF Controller:

 public class EmployeeController {
      public Employee__c emp {get;set;}
      public EmployeeController() { 
        String idStr = ApexPages.currentPage.getParameters().get('id');
        emp = new Employee__c(Id= idStr);//Change this initialization as per your need. }
    }

VF Page:

<c:GenericHistoryComponent recordLimit1="50" myObject1="{!emp}"/>
  • Thanks that pass the error but i'm getting the null value for the myObjectId? it throws error now System.NullPointerException: Attempt to de-reference a null object i'm looking at the line 30 which I have myObjectId = String.valueOf(myObject.get('Id')); – Nick Kahn Jan 25 '17 at 3:26
  • please try the code that I have posted on the github – Nick Kahn Jan 25 '17 at 3:27
  • Hi Nick, let me try that code, meanwhile can you check that instance passed to the component attribute has Id populated correctly, you can print the instance before assigned to the component using system.debug()? – LakhanP Jan 25 '17 at 3:31
  • i'm not sure i understand attribute has id populated, but i can see my object name DEBUG|/myObject /Employee__c:{} and DEBUG|/myObjectId /null and DEBUG|/objectHistoryTableName /Employee__c – Nick Kahn Jan 25 '17 at 3:35
  • Nick, You must have an existing employee__c record for which you want to display history. You need to assign the id of an existing or current employee record. I have edited my answer. – LakhanP Jan 25 '17 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.