Understanding WhatId and WhoId in Salesforce
Salesforce is a powerful CRM platform that offers flexibility in tracking relationships between various standard and custom objects. One key feature of its data model is the Activity object (used for Tasks and Events), which lets users log interactions such as calls, meetings, and follow-ups. While working with these objects, you may have come across two special fields: WhatId and WhoId.
In this blog post, we’ll dive deep into understanding what these fields represent, how they differ, and best practices when using them.
What are WhatId and WhoId?
In Salesforce, Tasks and Events are special standard objects used to represent activities. These objects support polymorphic relationships—meaning a single lookup field can refer to records of multiple object types. This is where WhatId and WhoId come into play.
🔹 WhoId
– Refers to a person-type record.
– Can be either a Lead or a Contact.
– Used to associate the activity with an individual.
Example: If you made a call to a lead or sent an email to a contact, the WhoId would be set to that person’s record.
Task t = new Task(
Subject = ‘Call Follow-up’,
Status = ‘Completed’,
Priority = ‘Normal’,
WhoId = ‘0031x00000XXXXXX’ // Contact ID
);
insert t;
🔹 WhatId
– Refers to a non-person-type record.
– Can be any object except Lead or Contact, such as Account, Opportunity, Case, Custom Object, etc.
– Used to associate the activity with a business object or process.
Example: If you log a task related to a sales opportunity or a support case, the WhatId should be used.
Task t = new Task(
Subject = ‘Demo Scheduled’,
Status = ‘Not Started’,
Priority = ‘High’,
WhatId = ‘0061x00000YYYYYY’ // Opportunity ID
);
insert t;
Key Differences
Feature | WhoId | WhatId | Example |
Refers To | Lead or Contact | Any object except Lead or Contact | Person vs Business Object |
Field Type | Polymorphic Lookup | Polymorphic Lookup | |
Field on Task/Event | Yes | Yes |
Things to Keep in Mind
- You can use both WhoId and WhatId together in a single Task or Event.
2. Polymorphic fields like WhatId and WhoId do not support cross-object relationships in formulas.
3. When querying activities, you’ll often need to use TYPEOF in SOQL (in tooling or Apex) to get fields from WhatId/WhoId:
SELECT Id, Subject, WhoId, WhatId,
TYPEOF WhatId WHEN Opportunity THEN Name, StageName
END
FROM Task
Use Case Example
Let’s say a sales rep has a phone call with a potential customer (Lead) regarding a new product launch (Opportunity). Here’s how you might create that activity:
Task callTask = new Task(
Subject = ‘Initial Call About New Product’,
WhoId = ’00Q1x00000ABCDE’, // Lead ID
WhatId = ‘0061x00000XYZ123’, // Opportunity ID
Status = ‘Completed’,
Priority = ‘Normal’
);
insert callTask;
Best Practices
– Always check that the Id you assign to WhatId or WhoId is valid and corresponds to the right object type.
– Use custom logic in Apex if you need to resolve what object a polymorphic ID refers to.
– Use descriptive Subjects and Status fields to ensure meaningful activity tracking.
Conclusion
Understanding the distinction between WhatId and WhoId is crucial for accurately modeling business processes in Salesforce. They enable precise tracking of interactions and help you build reports and automation that reflect real-world relationships. By leveraging these fields properly, your users and admins can maintain a clear view of their engagements with customers and opportunities.