0

Below summarizes the issue I am encountering around Tasks with contacts associated.

Scenario 1: When a contact is removed from an existing task, the removed contact should look up all associated tasks and evaluate MAX ActivityDate value.

Scenario 2: When a task has been deleted with x number of contacts, all x contacts should independently evaluate MAX ActivityDate based on associated tasks to each contact.

Problem: using an AFTER update / delete trigger on Task object, the removed contacts can no longer be seen in the TaskRelation soql query below. TaskRelation cannot have triggers or be referenced in Trigger.old and Trigger.new context as far as I am aware. Also, a Task query only ever returns one record with the WhoId populated with the main contact regardless of how many contacts are associated, the TaskRelation is where the contact associated can be fetched based on below soql.

Query used - returns MAX ActivityDate by contact:

TaskRelation[] contactTasks = [SELECT Task.ActivityDate 
    FROM TaskRelation 
    WHERE IsDeleted = false and RelationId = :contactId AND 
          Task.ActivityDate != NULL 
    ORDER BY Task.ActivityDate DESC];

Any help / ideas are much appreciated!

3
  • things to think about: 1) when task is deleted, the TaskRelations are cascade deleted; 2) the after delete trigger on Task knows the TaskId, 3) hence to detect deleted TaskRelation , use SOQL that includes the keyword ALL RECORDS and filter of TaskId = :theDeletedTaskIds
    – cropredy
    Nov 26, 2020 at 1:00
  • @cropredy Looks like using ALL ROWS in soql along with querying IsDeleted=true can help solve Scenario 2. How about Scenario 1? Even with all rows specified, when a contact is removed from a task, the task relation record is no longer present.
    – JamsJams
    Nov 26, 2020 at 18:08
  • short of a scheduled apex or scheduled flow, I have no answer for use case 1; vote up Idea
    – cropredy
    Nov 26, 2020 at 18:15

1 Answer 1

0

The Salesforce Object Ref documentation says to use getDeleted() for TaskRelation: IsDeleted - Indicates whether a task has been deleted; label is Deleted. When a TaskRelation record is deleted, it isn’t moved to the Recycle Bin and can’t be undeleted, unless the record was cascade-deleted when the parent object was deleted. Don’t use the IsDeleted field to detect deleted records in SOQL queries or queryAll() calls on directly deleted relation records. Instead, use the call getDeleted().

You must log in to answer this question.

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