PranavNagrecha/AwesomeSalesforceSkills / skills/admin/activity-and-task-patterns
activity-and-task-patterns
VerifiedTask and Event objects: polymorphic WhatId/WhoId, Activity object model, ActivityHistory vs OpenActivity, activity timeline customization, bulk task creation, Einstein Activity Capture boundaries. NOT for turning on EAC or calendar sync — use admin/einstein-activity-capture-setup. NOT for Email-to-Case — use admin/case-management-setup. Covers ActivitiesSettings metadata, enableActivities, shared Task/Event custom fields and FLS, TaskStatus value sets, Shared Activities and TaskRelation/EventRelation, recurring and child activities, and partial-success bulk DML on Task.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 14.9 KB |
| references/examples.md | markdown | 7.4 KB |
| references/gotchas.md | markdown | 13.9 KB |
| references/llm-anti-patterns.md | markdown | 5.8 KB |
| references/metadata-examples.md | markdown | 21.6 KB |
| references/well-architected.md | markdown | 8.3 KB |
| scripts/check_activity_and_task_patterns.pyexecutable | script | 17.5 KB |
| templates/activity-and-task-patterns-template.md | markdown | 3.8 KB |
references/examples.md
View on GitHub# Examples — Activity and Task Patterns
Two worked scenarios and one anti-pattern showing the shape this skill
produces. Each example assumes the practitioner has already decided to use
the standard Task/Event model (see `Decision Guidance` in SKILL.md for the
volume threshold that pushes to a custom `Interaction__c` instead).
---
## Example 1: Polymorphic timeline query for a custom related-list LWC
**Context:** A custom Account record page LWC needs to render the next
3 open tasks alongside the most recent closed event, with the related
record's `Name` displayed when the activity is on an Opportunity or Case
and the contact's full name when it's on a Contact.
**Problem:** A naive `SELECT What.Name FROM Task` compiles but returns
NULL for any `WhatId` that points at a polymorphic relationship the
practitioner didn't explicitly enumerate. Worse, querying `Activity`
directly fails compilation entirely — Activity is abstract.
**Solution:**
```apex
List<Task> openTasks = [
SELECT Id, Subject, ActivityDate, WhoId, WhatId,
TYPEOF What
WHEN Account THEN Name
WHEN Opportunity THEN Name, StageName, Amount
WHEN Case THEN CaseNumber, Subject
ELSE Id, Name
END,
TYPEOF Who
WHEN Contact THEN FirstName, LastName, Email
WHEN Lead THEN FirstName, LastName, Company
END
FROM Task
WHERE AccountId = :recordId
AND IsClosed = false
ORDER BY ActivityDate ASC
LIMIT 3
];
List<Event> recentEvents = [
SELECT Id, Subject, ActivityDateTime, DurationInMinutes,
TYPEOF What
WHEN Account THEN Name
WHEN Opportunity THEN Name, StageName
END
FROM Event
WHERE AccountId = :recordId
AND ActivityDateTime < :Datetime.now()
ORDER BY ActivityDateTime DESC
LIMIT 1
];
```
**Why it works:** `TYPEOF` is the only way to project polymorphic
parent fields beyond `Id` and `Type`. The `ELSE Id, Name` branch
catches every other object type that may be activity-enabled (e.g.,
custom objects with `Enable Activities = true`) so the LWC doesn't
explode when a user logs a task against an object the developer
forgot about. The `AccountId` filter on `Event` works because both
Task and Event maintain a denormalized `AccountId` that Salesforce
populates from the `WhatId` parent's account at insert.
---
## Example 2: Bulk task generation from a record-triggered context
**Context:** When an Opportunity moves to `Stage = 'Proposal/Price Quote'`,
a follow-up task should be created for the opportunity owner with a due
date 5 business days out. The trigger fires on bulk loads of up to
200 records.
**Problem:** Practitioners write loop-DML (`insert task` inside the
`for` loop), which fails the platform's synchronous limit of 150
DML statements per transaction (salesforce_app_limits_cheatsheet.txt
L64) on the 151st record and bricks the entire transaction.
The fix is bulk DML — but the practitioner must also handle the
case where `Stage` is being updated AND the OldMap shows it was
already at `Proposal/Price Quote` (a re-save), so duplicates aren't
inserted.
**Solution:**
```apex
public with sharing class OpportunityStageFollowupHandler {
public static void createProposalFollowupTasks(
List<Opportunity> newOpps,
Map<Id, Opportunity> oldMap
) {
List<Task> tasksToInsert = new List<Task>();
for (Opportunity opp : newOpps) {
Opportunity prior = oldMap?.get(opp.Id);
Boolean isFreshlyInProposal =
opp.StageName == 'Proposal/Price Quote'
&& (prior == null || prior.StageName != 'Proposal/Price Quote');
if (!isFreshlyInProposal) continue;
tasksToInsert.add(new Task(
WhatId = opp.Id,
OwnerId = opp.OwnerId,
Subject = 'Follow up on proposal',
ActivityDate = BusinessHours.add(
BusinessHours.getDefaultBusinessHoursId(),
Datetime.now(),
5 * 24L * 60 * 60 * 1000
).date(),
Priority = 'High',
Status = 'Not Started'
));
}
if (tasksToInsert.isEmpty()) {
return;
}
Database.DMLOptions dml = new Database.DMLOptions();
dml.optAllOrNone = false; // one bad row must not roll back 200 opps
dml.EmailHeader.triggerUserEmail = false; // no assignment mail per task
for (Database.SaveResult sr :
Database.insert(tasksToInsert, dml, AccessLevel.SYSTEM_MODE)) {
if (!sr.isSuccess()) {
for (Database.Error err : sr.getErrors()) {
System.debug(LoggingLevel.ERROR,
err.getStatusCode() + ' ' + err.getMessage()
+ ' fields=' + err.getFields());
}
}
}
}
}
```
**Why it works:** One DML statement regardless of batch size, against
a synchronous ceiling of 150. The `isFreshlyInProposal` gate prevents
duplicates when a user re-saves an opportunity that was already in the
target stage. `BusinessHours.add` respects the org's working calendar
instead of a naive `Date.today().addDays(5)`, which would land on a
Saturday roughly 2/7 of the time.
The two `Database.DMLOptions` lines are the part practitioners omit.
`optAllOrNone = false` lets the remainder of the operation succeed when
one record fails, and you inspect `Database.SaveResult` instead of
catching an exception (apexrefguide.txt L148100–L148104).
`EmailHeader.triggerUserEmail = false` suppresses the notification the
platform sends because *creating or modifying a task* is one of the
events that header governs (apexdev.txt L8612–L8617) — see
`references/gotchas.md` Gotcha 6. Neither option exists on the bare
`insert` DML verb, which is the real reason to reach for the
`Database` method here.
---
## Anti-Pattern: Querying ActivityHistory or OpenActivity outside a subquery
**What practitioners do:** Run a top-level SOQL like
`SELECT Id, Subject FROM ActivityHistory WHERE AccountId = :acctId`
expecting it to behave like Task.
**What goes wrong:** Compilation fails. `ActivityHistory` and
`OpenActivity` are *projection* objects — they exist only as
subqueries from an activity-enabled parent. They have no
queryable surface of their own, no DML, and no API endpoints.
Practitioners who hit this often pivot to "well, I'll query
Task and Event separately and union them" — which works but
forces them to deduplicate against the timeline the platform
already projects for free.
**Correct approach:**
```apex
List<Account> accts = [
SELECT Id, Name,
(SELECT Id, Subject, ActivityDate, Status
FROM OpenActivities
ORDER BY ActivityDate ASC LIMIT 5),
(SELECT Id, Subject, ActivityDate, Status
FROM ActivityHistories
ORDER BY ActivityDate DESC LIMIT 5)
FROM Account
WHERE Id = :acctId
];
```
The relationship names are `OpenActivities` and `ActivityHistories`
(plural, capital `A`), and the platform handles Task/Event union
plus the open-vs-closed split internally. If you need to *modify*
one of these records, look up the underlying Task or Event by
`Id` and DML it directly — `ActivityHistory` and `OpenActivity`
are read-only.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |