WhoId is the polymorphic lookup on Task and Event that points to a person: one Lead or one Contact (labelled Name in the UI, relationship name Who in SOQL). WhatId points to the non-person record the activity is about, such as an Account, Opportunity, Case or custom object (labelled Related To, relationship name What). If WhoId holds a Lead, WhatId must stay empty.
The definitions fit in a paragraph. The trouble starts when automation sets both fields on a lead task and the insert fails, or when a query asks for fields on Who and gets nothing type-specific back. Below are the rules Salesforce enforces, the error you hit when you break one, and SOQL and Apex that handle both fields correctly.
The breakdown of WhoId and WhatId
Think of these fields as two buckets. One is for people, the other is for everything else. If you are routing records with a Flow Decision element before creating follow-up tasks, the Decision has to set the right ID in each field. Both fields are polymorphic lookups, a relationship type that sits alongside lookups and master-detail in the Salesforce object relationships guide.
| WhoId | WhatId | |
|---|---|---|
| UI label | Name | Related To |
| SOQL relationship name | Who | What |
| Objects allowed | Lead or Contact | Account, Opportunity, Case, Campaign, Contract, Asset, custom objects and other non-person objects |
| How many per activity | One, or with Shared Activities one lead or up to 50 contacts | One |
| Can it be set when WhoId is a Lead | n/a | No, must be empty |
WhoId is for people
WhoId points to a person. In standard Salesforce that means a Lead or a Contact, and nothing else. Logging a call with a prospect puts that Lead record in WhoId. Meeting an existing client puts the Contact record there. Saving an Account ID in that field fails with an error.
WhatId is for business objects
WhatId is the context, the business object the activity belongs to. This is where you link the task to an Account, an Opportunity, a Case, or a custom object you built yourself. You cannot put a Lead or a Contact in WhatId. Salesforce reserves that field strictly for non-person entities.

A Salesforce Task and its related records, with person entities shown separately from business objects.
Rules Salesforce enforces between WhoId and WhatId
Salesforce checks the two fields against each other on save, and together they feed a third field you never set yourself. The Task object reference documents both behaviours.
A Lead in WhoId means WhatId must be empty
You can pair a Contact with an Opportunity on the same task. A Lead can't be paired with anything. Setting both fails with field integrity exception: unknown (cannot specify whatID with lead whoID). The message sometimes reads unknown, unknown (...); the part in parentheses stays the same, so that is the string to search your logs for.
Lead prospect = [SELECT Id FROM Lead WHERE IsConverted = false LIMIT 1];
Account targetAccount = [SELECT Id FROM Account LIMIT 1];
Task callPrep = new Task(
Subject = 'Prepare discovery call',
WhoId = prospect.Id,
WhatId = targetAccount.Id
);
insert callPrep; // DmlException: cannot specify whatID with lead whoID
There are two ways out. If the activity really belongs to a deal or an account, convert the lead first and relate the task to the resulting Contact, with the business record in WhatId. If it is early prospecting, leave WhatId null. In any trigger or Flow that stamps WhatId from context, check the WhoId type before you assign it. Otherwise one lead in an insert of 200 tasks fails the whole statement.
Task.AccountId is derived
You do not set AccountId on a Task. Salesforce works it out in this order:
- If WhatId is an Account, Opportunity, Contract or a custom object that is a child of Account, AccountId is that record's account.
- Otherwise, if WhoId is a Contact, AccountId is the contact's account (the primary contact's, under Shared Activities).
- Otherwise AccountId is null.
So a task with an Acme opportunity in WhatId and a contact from a different account in WhoId rolls up to Acme, because WhatId wins. A task with a Lead in WhoId has no WhatId by rule, so its AccountId is null and it has nothing to roll up to at account level. A task on a Case with no WhoId also ends up with a null AccountId, because Case is not one of the WhatId objects that sets it.
Neither field accepts a User
The Task reference lists only Contact and Lead for WhoId, and User is absent from the WhatId list too. The rep a task is assigned to goes in OwnerId. A follow-up for a sales rep about a call with a customer has the rep's User Id in OwnerId, the Contact in WhoId and the Opportunity in WhatId.
Shared Activities: more than one WhoId
With Shared Activities enabled, a task or event can relate to one lead or up to 50 contacts. Those extra people are stored as rows in TaskRelation and EventRelation, junction objects with a polymorphic RelationId field (relationship name Relation) and an IsWhat flag that is false for contacts and leads. TaskWhoRelation and EventWhoRelation are the person-only views over the same data, which is usually what you want. WhoId on the task then holds the primary contact, so code that reads only WhoId sees one person out of possibly dozens.
To read everyone attached to a task:
SELECT RelationId, Relation.Name FROM TaskWhoRelation WHERE TaskId = :taskId
Per the TaskRelation reference, with Shared Activities enabled TaskRelation does not support triggers, workflow or validation rules. Because TaskRelation has no triggers, put the logic on Task or Event, and remember it runs only when that record itself is saved. Adding a non-primary contact may not touch the Task, so a scheduled job that queries TaskWhoRelation is the reliable way to catch those changes. Very old integrations are worth checking too: the Task reference notes that API 23.0 and earlier cannot query, update or delete tasks related to more than one contact.
Why WhoId and WhatId matter for developers
In SOQL these fields are awkward, precisely because they are polymorphic. Plain dot notation only gives you fields every target shares, such as Who.Name; it will not pull Email from a Contact or Company from a Lead. TYPEOF is generally available from API version 46.0 (Summer '19); earlier versions had it only as a Developer Preview. It takes the relationship name, so you write TYPEOF Who. TYPEOF WhoId is a syntax error.
Here is how I write a query when I need details from the person record:
SELECT Subject,
TYPEOF Who
WHEN Contact THEN Name, Email
WHEN Lead THEN Name, Company
END
FROM Task
WHERE Status = 'Open'
ELSE is optional and covers any type your WHEN branches do not list. On Task.Who, Contact and Lead are the only possible types, so these two branches cover every record. When you only need to filter by type, skip TYPEOF and use the Type qualifier in the WHERE clause:
SELECT Id, Subject, Who.Name FROM Task WHERE Who.Type = 'Contact'
TYPEOF is not allowed in COUNT() queries, Bulk API SOQL, GROUP BY or HAVING queries, the SELECT of a semi-join subquery, or nested inside another TYPEOF. My default is Who.Type and What.Type for filtering, with TYPEOF reserved for fields that exist on one type only. The trade-off shows up in data extracts: a Bulk API job cannot use TYPEOF, so it needs Who.Type filters and one query per object type. The SOQL complete guide covers the rest of the relationship query syntax.
In Apex, getSObjectType() on the Id tells you what you are dealing with. This pattern collects IDs per type from open tasks so you can route follow-ups with one query per object afterwards:
List<Task> openTasks = [
SELECT Id, WhoId, WhatId, Subject
FROM Task
WHERE IsClosed = false
LIMIT 200
];
Set<Id> contactIds = new Set<Id>();
Set<Id> leadIds = new Set<Id>();
Set<Id> opportunityIds = new Set<Id>();
for (Task tsk : openTasks) {
Schema.SObjectType t = tsk.WhoId?.getSObjectType();
if (t == Contact.SObjectType) {
contactIds.add(tsk.WhoId);
} else if (t == Lead.SObjectType) {
leadIds.add(tsk.WhoId);
}
t = tsk.WhatId?.getSObjectType();
if (t == Opportunity.SObjectType) {
opportunityIds.add(tsk.WhatId);
}
}
The safe navigation operator returns null for a blank WhoId or WhatId, so neither branch fires and nothing throws. I have seen teams hardcode ID prefixes instead, checking whether an ID starts with "003" for Contacts or "00Q" for Leads, and please do not do that. It breaks the first time someone adds a custom object to the routing logic and guesses its prefix wrong. Compare against SObjectType tokens and the compiler checks your object names for you.
The biggest headache with these two fields is reporting. To show tasks related to both a Contact and an Opportunity, both fields have to be populated. Leave the WhatId blank and that activity might vanish from your Opportunity reports, even when the Contact is related to that Opportunity.
Common pitfalls and limitations of WhoId and WhatId
A few things are simply off the table. You cannot use these fields in standard cross-object formula fields, so showing the Account's region on a Task record is not happening with a simple formula. You might need to choose between Apex or Flow to sync that value into a custom field.
Validation rules are the other sore spot. Because these fields can point at so many things, a rule that fires only when the WhatId is one specific custom object is harder to write than it looks. You usually end up checking the ID prefix, which is fine as a quick fix and not much more than that. Under Shared Activities, validation rules also never see TaskRelation records, so a rule on WhoId only ever checks the primary contact.
Watch bulk loads that set WhatId on activities. A data migration that assigns an Account to every historical task will reject each row whose WhoId is a Lead, so split those rows out or clear WhatId for them before the load.
Key takeaways
- WhoId always refers to a person, so a Lead or a Contact.
- WhatId refers to the business object: Account, Opportunity, Case, and so on.
- An activity can have both fields filled only when WhoId is a Contact. A Lead in WhoId means WhatId must be empty.
- You can't use these fields in standard cross-object formulas.
- Use
TYPEOF Whoin SOQL to get type-specific fields from the related records, andWho.Typeto filter. - In Apex, compare
getSObjectType()results withSObjectTypetokens and leave ID prefixes alone.
If you are preparing for a senior developer interview, expect a question on this. It is one of those fundamental pieces of the Salesforce data model that shows whether you know how the platform handles relationships under the hood, and the usual follow-up is why a lead task cannot carry a WhatId.
Leave a Comment