+ Start a Discussion
SalesRedSalesRed 

Delete from ActivityHistory using Apex?

Hello,   I have a requirement to delete entries to the Activity History after a period of time.  From looking at the following 

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_activityhistory.htm

 

I can select and view Activity history details using SOQL.  

 

SELECT (SELECT ActivityDate, Description FROM ActivityHistories)
FROM Account
WHERE Name Like 'XYZ%'

 

I cannot select from ActivityHistories itself though

 

E.g. "SELECT ActivityDate, Description FROM ActivityHistories"  does not seem to be allowed

 

If I want to retrieve from ActivityHistories (using SELECT ActivityDate, Description FROM ActivityHistories for example.   Then delete these entried based on logic relating to the date on when they were created or activitydate, does anyone know if this can be done?

 

The following article details how to delete activities http://na14.salesforce.com/help/doc/en/activity_del.htm   but I think this means having manual deletions

 

is there a way to delete activities via Apex code, or even a scheduled task or scheduled process of some type via configurations?  We would need to check on the date and delete all activities (of email for example) after a a period of time.

 

Thanks in advance.

InternalServerErrorInternalServerError

This object is read only so to delete the history you would need to delete the activity itself (delete on the Task object). This will delete the activity history.

SalesRedSalesRed

Hi InternalServerError,   

 

Thanks for your help/suggestion. Deleting from Task sounds like it could be what I need.  Do you know if there is a way of determining the task type so that I can determine that I only select Emails to delete?  The following are the task fields I note when I access Task in my salesforce.schema  and build a SOQL statement from it

 

Select t.WhoId, t.WhatId, t.SystemModstamp, t.Subject, t.Status, t.ReminderDateTime, t.RecurrenceType, t.RecurrenceTimeZoneSidKey, t.RecurrenceStartDateOnly, t.RecurrenceMonthOfYear, t.RecurrenceInterval, t.RecurrenceInstance, t.RecurrenceEndDateOnly, t.RecurrenceDayOfWeekMask, t.RecurrenceDayOfMonth, t.RecurrenceActivityId, t.Priority, t.OwnerId, t.LastModifiedDate, t.LastModifiedById, t.IsReminderSet, t.IsRecurrence, t.IsDeleted, t.IsClosed, t.IsArchived, t.Id, t.Description, t.CreatedDate, t.CreatedById, t.CallType, t.CallObject, t.CallDurationInSeconds, t.CallDisposition, t.ActivityDate, t.AccountId From Task t

 

Thanks again for your help!

InternalServerErrorInternalServerError

 

You can either use this:

 

SELECT Id FROM Task WHERE Subject LIKE 'Email%'

SalesRedSalesRed

Hi InternalServerError,

 

I note it does put "Email:" at the start of the subject field.  Do you know if this guarantees it is an email? The reason I ask is, can the subject field be set manually otherwise and therefore could contain "Email:"  when not an actual email.

 

Thanks again.

InternalServerErrorInternalServerError

That should be fine. You can also use:

 

SELECT Id,Type FROM Task WHERE Type = 'Email'

SalesRedSalesRed

Hi InternalServerError,

 

Thanks again.

 

I note when I try this it states "No such column 'Type' on entity.  I guess type does not exist as a field to search on.