How to create a custom Activity and Activity History related list in a VF Page, as you can’t directly query these objects.

Yukti:

The code below creates an Activity and Activity History related list for an Account.

  • Capture the instance of Account for which you want to generate relative list.
  • Use the following Query to get fetch Activity and Activity History related to selected Account.
  • Display both related list on VF page.

 

Query that can be used:

For Activity:

Select (Select o.What.Name , o.WhatId, o.IsTask, o.IsEvent__c, o.ActivityDate, o.Account.Name From OpenActivities o) from account  where ID=:account.id];

For Activity History:

Select (Select o.What.Name , o.WhatId, o.IsTask, o.IsEvent__c, o.ActivityDate, o.Account.Name From ActivityHistories o) from account  where ID=:account.id];

 

Reusable Code:

Activity: 

List<OpenActivity> actRList = new List<OpenActivity>();

for(Account acc:[ Select (Select o.What.Name , o.WhatId, o.IsTask, o.IsEvent__c, o.ActivityDate, o.Account.Name From OpenActivities o) from account where ID=:acc.id])

{

 if(!acc.OpenActivities.isempty())

{

 for(OpenActivity opnAct: acc.OpenActivities)  // If there are multiple Tasks and Events this loop will take care of that.

{

actRList.add(opnAct);

}

}

}

Activity History:

List<ActivityHistory> actHisRList = new List<ActivityHistory>();

for(Account accHis:[ Select (Select o.What.Name , o.WhatId, o.IsTask, o.IsEvent__c, o.ActivityDate, o.Account.Name From ActivityHistories o) from account   where ID=:acc.id])

{

if(!accHis.ActivityHistories.isempty())

{

for(ActivityHistory opnActH: accHis.ActivityHistories )  // If there are multiple Tasks and Events this loop will take care of that.

{

actHisRList.add(opnActH);

}

}

}

This post was written by , posted on June 25, 2013 Tuesday at 12:55 pm
  • sam says:

    Hiii everyone

    Please write some post to list activity log of a parent child relationship custom object on VF page

  • jayanti says:

    i have used this code,but it is showing one error for(ActivityHistory opnActH: accHis.ActivityHistories ) ,accHis.ActivityHistories is not exist.So please suggest soemthing here.also i have one requirement taht all activity should show on visualforce page so it is fine if i will do with this code?
    Thanks in advance

    • admin says:

      Hello Jayanti,
      Sorry for my late response, Can you please post the part of code here..
      BTW i hope you are using select inside select :
      [ Select (Select o.What.Name , o.WhatId, o.IsTask, o.IsEvent__c, o.ActivityDate, o.Account.Name From ActivityHistories o) from account where ID=:acc.id]

      Regards,
      Admin

  • admin says:

    Just a quick update on the previously suggested solution:

    I think now in Salesforce you can use Task and Event table in sub queries just like OpenActivities.

    Thus now even the below mention query will work fine:

    Select (Select Id, WhoId,WhatId, IsClosed,recordTypeId From Tasks where WhoId=null and Isclosed=False and WhatId != null), ID From Opportunity o where o.id in :ABC;

    Limitations to both is inner query Governor limits :(

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Visit Us On FacebookVisit Us On Twitter