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
GailGail 

ActivityHistory not displaying Related To on VF page

the Apex class:

public with sharing class AcctActivityList{
  
    public Account acct {get; set;}
    public List<ActivityHistory> ClosedTasks {get; set;}
    string mail = 'mail';
  
    public AcctActivityList(ApexPages.StandardController stdcontroller) {
        //get account
        acct = (Account)stdController.getRecord();
        //get activity history - can't query activity history directly, just in a subquery against another object

        SObject[] ActivityHistory = [Select id, (select Id, AccountId, Account.Name, ActivityDate, ActivityType, Description, OwnerId, Subject,
                                        LastModifiedDate, IsTask, WhatId, WhoId from ActivityHistories where
                                        (ActivityType !='Email') ORDER by ActivityDate DESC)
                           from Account where Id =: acct.id];
      
        ClosedTasks = (List<ActivityHistory>)ActivityHistory.get(0).getSObjects('ActivityHistories');
      
    }
}

The vf page:

<apex:page standardcontroller="Account" extensions="AcctActivityList">  
     <apex:pageBlock title="Activity History">
    <apex:pageBlockTable value="{!ClosedTasks}" var="ah" >
        <apex:column headerValue="Subject">
        <apex:outputLink value="/{!ah.id}">{!ah.Subject}</apex:outputlink>
        </apex:column>
        <apex:column headervalue="Name" value="{!ah.AccountId}"/>
        <apex:column headervalue="Related To" value="{!ah.WhatId}"/>
        <apex:column value="{!ah.IsTask}"/>
        <apex:column headerValue="Due Date" value="{!ah.ActivityDate}"/>
        <apex:column value="{!ah.OwnerId}"/>
        <apex:column value="{!ah.LastModifiedDate}"/>
      
    </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
  

How do I get Related To to appear on the Visualforce page?
See where I refer to WhatId on the vf page? This results in the error "Content cannot be displayed: U#324.4ff (What): InvalidForeignKeyValue, 001f000000DQ9AyAAL" appearing on the page. Now, that "foreign key" is actually the account id of the account on which this is being displayed. That shouldn't be an issue tho - AccountId above it displays fine if I take out the reference to WhatId. I need to show Related To in the list - anyone know how I can do that?                          
bob_buzzardbob_buzzard
I suspect what is happening here is that the column knows the value parameter specifies an sobject field, so will try to render a link to the underlying object that includes the name, but as the whatid is a polymorphic relationship that causes it a problem.

If you can live without the hover popup on the related item, you can query back the whatid and related name use those to build a link in the column body:


SObject[] ActivityHistory = [Select id, (select Id, AccountId, Account.Name, ActivityDate, ActivityType, Description, OwnerId, Subject,
                                        LastModifiedDate, IsTask, WhatId, What.Name, WhoId from ActivityHistories where
                                        (ActivityType !='Email') ORDER by ActivityDate DESC)
                           from Account where Id =: acct.id];

...

<apex:column headervalue="Related To">
  <apex:outputLink value="/{!ah.WhatId}">{!ah.What.Name}</apex:outputLink>
</apex:column>