3

I have a visualforce page that is supposed to display the activity history for the Contact record for ONLY the current user. However, it seems to be leaving out some of the activity history, with no apparent consistency to the records it leaves out.

On this Contact for example, it displays a dozen activity history records, but leaves out four seemingly at random.

Here are six that are showing. This is an incomplete list: Is Showing 1 Is Showing 2 Is Showing 3 Is Showing 4 Is Showing 5 Is Showing 6

Here is the complete list of the four not showing: Not Showing 1 Not Showing 2 Not Showing 3 Not Showing 4

As you can see, there does not seem to be a consistent pattern. For example, a record created on 3/18/2016 is showing. But, a record created afterwards, on 3/21 is NOT showing. And then a record created later at 5/14 is showing again.

Here is what this looks like on the vf page. If I search for "Left VM again", there are 0 results. (There are other activities from the same day 3/21, that are showing up, this is why I searched by the comment field). VF page view

Here's the code for the vf page, and the apex controller that runs the query.

VF:

    <apex:page standardController="Contact" extensions="ActivityHistoryOnContact" showHeader="true" sidebar="false" >
    <style>
    .activeTab {background-color: #081f3f; color:white; background-image:none}
    .inactiveTab { background-color: lightgrey; color:black; background-image:none}
    </style>


         <apex:form > 
         <apex:pageblock >
   <apex:pageBlockSection >
   <apex:pageBlockSectionItem >
<apex:commandLink value="Log A Call" action="{!URLFOR($Action.Contact.Log_a_Call,Contact.id)}" target="_blank" styleClass="btn" style="text-decoration:none;padding:4px;"></apex:commandlink>
      </apex:pageBlockSectionItem>
   </apex:pageBlockSection>
          </apex:pageblock>



            <apex:repeat value="{!ListTasks}" var="tsk" id="repeat">
<apex:pageblock >
   <apex:pageBlockSection >
<apex:pageBlockSectionItem >
             <apex:outputLabel value="Subject" for="tsk.Subject" style="font-weight:bold"/>
                    <apex:outputLink value="/{!tsk.id}" target="_blank">{!tsk.Subject}</apex:outputLink>
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
             <apex:outputLabel value="Comments" for="!tsk.Description" style="font-weight:bold"/>
                    <apex:outputField value="{!tsk.Description}" id="Contact"/>  
        </apex:pageBlockSectionItem>  

                <apex:pageBlockSectionItem >
             <apex:outputLabel value="Call Result" for="tsk.Call_Result__c" style="font-weight:bold"/>
                    <apex:outputField value="{!tsk.Call_Result__c}" id="Contact"/>  
        </apex:pageBlockSectionItem>   

                        <apex:pageBlockSectionItem >
             <apex:outputLabel value="Due Date" for="tsk.ActivityDate" style="font-weight:bold"/>
                    <apex:outputField value="{!tsk.ActivityDate}" id="Contact"/>  
        </apex:pageBlockSectionItem>   

        <apex:pageBlockSectionItem > 
        </apex:pageBlockSectionItem>       
       </apex:pageBlockSection>
      </apex:pageblock>
                    </apex:repeat>

          </apex:form> 


                </apex:page>

Apex Controller:

    public class ActivityHistoryOnContact{


public String currentUser = UserInfo.getUserId();
public String oldUser;
//this oldUser variable is for legacy Users that were imported from a separate instance of SF and had a different id in this old instance.

public String currentContact = ApexPages.currentPage().getParameters().get('id');
    public String taskList {get;set;}



    public Task[] getListTasks() {

     oldUser=[select old_ID__c from User where id=:currentUser limit 1]
 [0].old_ID__c;

 taskList = 'select whoid,subject,status, ActivityDate, LastModifiedDate, WhatID,Description, Primary_Contact_Phone_Number__c, Primary_Contact_Email_Address__c, Call_Result__c, Last_Call_Date__c, Last_Call_Agent__c from task WHERE (CreatedbyId=\''+currentUser+'\' OR OLDOWNERID__C=\''+oldUser+'\') AND WhoID =\''+currentContact+'\' order by ActivityDate desc limit 20000'; 
 System.debug('~~~~~~'+tasklist);

        List<Task> tskList = Database.query(taskList);

        return tskList;     

}

   public ActivityHistoryOnContact(ApexPages.StandardController controller){}





}

Any thoughts as to why seemingly random records are not appearing? I am at a loss, since the records don't seem to have anything in common.

4
  • Any possibility there are multiple contacts with that name. If so it may look like the same contact but the Ids would be different. Same possibility with the owner. I just had the happen with a client where they had a full user portal user deactivated and a portal user active. The records were mixed ownership yet looked like the same person owned them.
    – Eric
    May 26, 2017 at 3:24
  • I had thought of that, and I checked the ids. I can see all activity history if I look at the contact as an administrator and click "Go To List." Just not on the vf page.
    – MRG
    May 30, 2017 at 16:31
  • 1
    Do you see all the tasks in your apex debug? Could it be possible that this user might be an admin who inserted tasks in bulk and ended up owning more than 20000 tasks? What happens when you remove the limit of 20000 ? Finally can you run a report on task object and check how many tasks are created by the specific user you are logged in as?
    – Rao
    May 31, 2017 at 21:55
  • Debug at oldUser=[select old_ID__c from User where id=:currentUser limit 1] [0].old_ID__c; and see if old_id__c is populated for the current user ? After this pull in the oldownerid__c field in the task object to the task page layout and see if the value is populated for the task record
    – Rao
    May 31, 2017 at 21:59

1 Answer 1

3
+50

My intuition is there could be issue in SOQL query if I consider current user has visibility of all the task records and you are not using with sharing in the controller.

Also, you haven't share how OLDOWNERID__C of Task is getting saved.

Can you change this query like this where you will consider OLDOWNERID__C if that is not null:

public Task[] getListTasks() 
    {
        List<Task> tskList = new List<Task>();
        oldUser=[select old_ID__c from User where id=:currentUser limit 1][0].old_ID__c;
        if(oldUser !=null)
        {
         tskList= [Select whoid,subject,status, ActivityDate, 
                             LastModifiedDate, 
                             WhatID,Description, Primary_Contact_Phone_Number__c, 
                             Primary_Contact_Email_Address__c, 
                             Call_Result__c, Last_Call_Date__c, Last_Call_Agent__c, OLDOWNERID__C 
                             FROM Task 
                             WHERE (CreatedbyId=:currentUser OR OLDOWNERID__C = :oldUser) 
                             AND WhoID =:currentContact 
                             Order by ActivityDate desc,CreatedbyId,OLDOWNERID__C  limit 20000];
        }
        else
        {
         tskList= [Select whoid,subject,status, ActivityDate, 
                             LastModifiedDate, 
                             WhatID,Description, Primary_Contact_Phone_Number__c, 
                             Primary_Contact_Email_Address__c, 
                             Call_Result__c, Last_Call_Date__c, Last_Call_Agent__c, OLDOWNERID__C 
                             FROM Task 
                             WHERE CreatedbyId=:currentUser
                             AND WhoID =:currentContact 
                             Order by ActivityDate desc,CreatedbyId,OLDOWNERID__C limit 20000];
        }
        return tskList; 
    }
1
  • I was unable to test this suggestion, unfortunately. I will give you the bounty anyways for responding
    – MRG
    Jun 8, 2017 at 16:21

You must log in to answer this question.

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