By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
horizontal-light horizontal-dark
  • Home
  • Tutorials
    • Salesforce AI
    • Salesforce DevOps
    • Career
    • Errors
    • Interview Questions
    • Salesforce Integration
    • Salesforce Flow
  • Salesforce Tools
  • Apex Development
  • Lightning Web Components
  • Salesforce Admin
  • About
    • Privacy Policy
    • Disclaimer
    • Terms & Conditions
    • Contact
SalesforceCornerSalesforceCorner
Search
  • Home
  • Tutorials
    • Salesforce AI
    • Salesforce DevOps
    • Career
    • Errors
    • Interview Questions
    • Salesforce Integration
    • Salesforce Flow
  • Salesforce Tools
  • Apex Development
  • Lightning Web Components
  • Salesforce Admin
  • About
    • Privacy Policy
    • Disclaimer
    • Terms & Conditions
    • Contact
Follow US
Salesforce Corner » Apex Development » WhoId vs WhatId in Salesforce: What’s the Difference and When Should You Use Each?
Apex Development

WhoId vs WhatId in Salesforce: What’s the Difference and When Should You Use Each?

Understand Salesforce Activity Relationships with Confidence

Neha Panwar
By
Neha Panwar
ByNeha Panwar
Salesforce Developer and Technical Writer
Neha Panwar is a Salesforce developer and technical writer who shares practical tutorials, Apex guides, and real-world solutions for developers. She focuses on simplifying Salesforce concepts,...
Follow:
- Salesforce Developer and Technical Writer
Last updated: 2026/06/29
Share
WhoId vs WhatId in Salesforce
SHARE

If you’ve ever created a Task or Event in Salesforce, you’ve probably seen the Name and Related To fields. Behind these two fields are the API names WhoId and WhatId. Understanding the difference between them is important for Salesforce Admins, Developers, and anyone working with automation or Apex.

Although both fields are used on Activities, they serve completely different purposes. Choosing the wrong field can lead to incorrect relationships, reporting issues, or unexpected behavior in Flows and Apex.

In this guide, you’ll learn what WhoId vs WhatId in Salesforce means, how these fields work, supported objects, practical examples, Apex code, and common interview questions.

Contents
What Are Activities in Salesforce?What Is WhoId in Salesforce?Supported Objects for WhoIdWhat Is WhatId in Salesforce?Supported Objects for WhatIdWhoId vs WhatId: Key DifferencesWhy Are WhoId and WhatId Called Polymorphic Fields?Real Business ExampleQuerying WhoId and WhatId Using SOQLImage Prompt (Place Here)Automatic Account PopulationWhat Happens If WhoId Points to a Lead?Common Mistakes Developers MakeUsing WhatId for ContactsUsing WhoId for OpportunitiesAssuming WhatId Always References an OpportunityForgetting Null ChecksBest PracticesFrequently Asked QuestionsFinal Thoughts

What Are Activities in Salesforce?

Before understanding WhoId and WhatId, it’s important to know how Activities work.

Activities are records used to track interactions with customers. Salesforce stores Activities using two standard objects:

  • Task
  • Event

Examples include:

  • Phone calls
  • Meetings
  • Follow-up tasks
  • Customer visits
  • Emails
  • Demos

Every Activity can be connected to a person, a business record, or both.

That’s where WhoId and WhatId come in.

What Is WhoId in Salesforce?

WhoId represents the person associated with an Activity.

Think of it as answering this question:

“Who is this activity for?”

WhoId can reference only:

  • Lead
  • Contact

For example:

A sales representative schedules a meeting with John Smith, who is stored as a Contact.

The meeting should be linked to John.

Here:

  • WhoId → John Smith (Contact)

Another example:

A sales rep calls a Lead that hasn’t converted yet.

Here:

  • WhoId → Lead Record

This field appears in Salesforce as the Name field.

Salesforce New Task form in browser
Salesforce New Task form in browser

Supported Objects for WhoId

Unlike lookup fields, WhoId doesn’t support every object.

It supports only:

ObjectSupported
ContactYes
LeadYes
AccountNo
OpportunityNo
CaseNo
CampaignNo
Custom ObjectNo

This is easy to remember because WhoId always points to a person.

What Is WhatId in Salesforce?

WhatId represents the business record related to the Activity.

Instead of asking:

“Who is involved?”

It answers:

“What is this activity related to?”

Unlike WhoId, WhatId supports many different Salesforce objects.

Common examples include:

  • Account
  • Opportunity
  • Case
  • Campaign
  • Contract
  • Asset
  • Order
  • Product
  • Custom Objects

Example:

A sales rep schedules a meeting with a customer regarding an Opportunity.

The Activity becomes:

WhoId → Contact

WhatId → Opportunity

Now Salesforce knows:

  • Which person attended
  • Which business record the meeting was about

Salesforce task record on Chrome browser
Salesforce task record on Chrome browser

Supported Objects for WhatId

WhatId supports many standard and custom objects.

Some common ones include:

  • Account
  • Opportunity
  • Case
  • Campaign
  • Contract
  • Asset
  • Order
  • Custom Objects

This flexibility makes WhatId much more versatile than WhoId.

WhoId vs WhatId: Key Differences

Understanding the distinction becomes much easier when you compare both fields side by side.

FeatureWhoIdWhatId
PurposePersonBusiness Record
UI LabelNameRelated To
Supports ContactYesNo
Supports LeadYesNo
Supports OpportunityNoYes
Supports AccountNoYes
Supports Custom ObjectsNoYes
Used onTask & EventTask & Event

Most interview questions are based on this comparison alone.

WhoId vs WhatId in Salesforce (1)
WhoId vs WhatId in Salesforce (1)

Why Are WhoId and WhatId Called Polymorphic Fields?

Both fields are known as polymorphic lookup fields.

Unlike a normal Lookup Relationship, these fields can reference multiple object types.

For example:

WhoId can reference:

  • Contact
  • Lead

WhatId can reference:

  • Opportunity
  • Account
  • Case
  • Campaign
  • Custom Object
  • Many other supported objects

Salesforce automatically determines which object the ID belongs to.

This flexibility makes Activities much more powerful.

Developers often encounter polymorphic fields while writing SOQL queries and Apex code. If you’re new to Apex programming, Apex Switch Statements vs If-Else: When and How to Use Each explains how to write cleaner business logic when working with different object types.

Real Business Example

Imagine you’re a sales representative.

You have:

Customer:

Rahul Sharma

Opportunity:

Salesforce Implementation Project

You schedule a meeting.

Salesforce stores:

WhoId → Rahul Sharma (Contact)

WhatId → Salesforce Implementation Project (Opportunity)

Later, anyone viewing the Opportunity can immediately see every meeting, email, and phone call related to that deal.

Similarly, anyone opening Rahul’s Contact record can also see the same Activity history.

This is one reason Salesforce Activity Management is so powerful.

Working with WhoId and WhatId in Apex

Developers frequently use WhoId and WhatId while creating Tasks, Events, Flows, integrations, and custom automation. Since both fields are polymorphic, your code should always handle them carefully.

Here’s a simple Apex example that creates a follow-up task for a Contact and links it to an Opportunity.

Task followUp = new Task(
Subject = 'Follow-up Call',
Status = 'Not Started',
Priority = 'Normal',
WhoId = '003XXXXXXXXXXXXXXX',
WhatId = '006XXXXXXXXXXXXXXX'
);

insert followUp;

In this example:

  • WhoId references the Contact.
  • WhatId references the Opportunity.

When users open either record, they’ll see the same activity in the Activity Timeline.

If you’re working with automation that creates Tasks from Apex, understanding trigger behavior is equally important. How to Prevent Recursive Triggers in Salesforce Apex explains how to avoid common recursion problems while creating or updating activity records.

Querying WhoId and WhatId Using SOQL

Fetching activity records is straightforward.

List<Task> tasks = [
SELECT Subject,
WhoId,
WhatId,
ActivityDate,
Status
FROM Task
WHERE Status = 'Not Started'
];

You can also retrieve relationship fields.

List<Task> tasks = [
SELECT Subject,
Who.Name,
What.Name
FROM Task
];

Salesforce automatically resolves the related object when possible.

However, remember that What could represent different object types, so avoid assuming it’s always an Opportunity or Account.

Image Prompt (Place Here)

WhoId vs WhatId in Salesforce (
WhoId vs WhatId in Salesforce (

Automatic Account Population

One Salesforce feature that surprises many beginners is automatic Account assignment.

Suppose:

  • WhoId → Contact
  • Contact belongs to ABC Corporation

Salesforce automatically fills the Activity’s Account field.

Another scenario:

  • WhatId → Opportunity

Since every Opportunity belongs to an Account, Salesforce automatically identifies the correct Account from the Opportunity.

Because of this automatic relationship handling, reports become much easier to build.

What Happens If WhoId Points to a Lead?

This is another common interview question.

If WhoId references a Lead, the Related To (WhatId) field is unavailable.

Why?

Because Leads are not associated with Accounts.

Without an Account relationship, Salesforce cannot determine the business record associated with the Activity.

After Lead conversion:

  • WhoId becomes the Contact.
  • WhatId can reference Opportunity, Account, or another supported object.

ChatGPT Image Jun 29, 2026, 05_34_59 PM
ChatGPT Image Jun 29, 2026, 05_34_59 PM

Common Mistakes Developers Make

Although the concept is simple, these mistakes appear frequently in real projects.

Using WhatId for Contacts

Incorrect:

WhatId = Contact.Id

Correct:

WhoId = Contact.Id

Using WhoId for Opportunities

Incorrect:

WhoId = Opportunity.Id

Correct:

WhatId = Opportunity.Id

Assuming WhatId Always References an Opportunity

WhatId can point to many object types.

Always check the object type before processing records.

Forgetting Null Checks

Some Tasks may contain:

  • Only WhoId
  • Only WhatId
  • Both fields
  • Neither field

Always verify values before accessing relationships.

Best Practices

Follow these recommendations while designing automation or writing Apex.

  • Use WhoId only for Leads and Contacts.
  • Use WhatId for business records like Opportunities, Cases, Accounts, Campaigns, and Custom Objects.
  • Avoid hardcoding record IDs.
  • Handle null values safely.
  • Test activities with different object combinations.
  • Document your automation so admins understand the relationship logic.

These practices improve maintainability and reduce production issues.

Frequently Asked Questions

What is WhoId in Salesforce?

WhoId is an Activity field that references a Contact or Lead. It identifies the person associated with a Task or Event.

What is WhatId in Salesforce?

WhatId references business records such as Opportunities, Accounts, Cases, Campaigns, and Custom Objects.

Can a Task have both WhoId and WhatId?

Yes. A Task or Event can contain both fields at the same time, allowing Salesforce to track both the person involved and the related business record.

Why are WhoId and WhatId called polymorphic fields?

Because they can reference multiple object types instead of only one object.

Final Thoughts

Although WhoId and WhatId look similar, they solve two different business problems.

WhoId identifies who the Activity is for, while WhatId identifies what the Activity is about. Together, these fields allow Salesforce to build a complete history of customer interactions across Leads, Contacts, Opportunities, Cases, Accounts, Campaigns, and many other records.

Whether you’re building Flows, writing Apex code, creating reports, or preparing for Salesforce interviews, understanding these two fields will help you design cleaner automation and maintain accurate Activity relationships.

If you’re exploring more Apex concepts, 20 Apex String Methods Every Salesforce Developer Should Know, Apex Switch Statements vs If-Else: When and How to Use Each, and How to Prevent Recursive Triggers in Salesforce Apexare excellent next reads.

TAGGED:Activity ManagementApexEventsPolymorphic FieldsSalesforcesalesforce adminsalesforce apexsalesforce developerSalesforce InterviewTasks
Share This Article
Facebook Email Print
ByNeha Panwar
Salesforce Developer and Technical Writer
Follow:
Neha Panwar is a Salesforce developer and technical writer who shares practical tutorials, Apex guides, and real-world solutions for developers. She focuses on simplifying Salesforce concepts, integrations, and backend development to help beginners and professionals learn faster.
Leave a Comment Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest Post

Salesforce dynamic forms interface illustration
Salesforce Dynamic Forms: A Better Way to Show and Hide Fields
Salesforce Flow
Salesforce flow debugging guide
How to Debug and Fix Salesforce Flow Errors ?
Salesforce Flow
Salesforce Flow Loops tutorial diagram
Salesforce Flow Loops: Collections, Iteration, and Best Practices
Salesforce Flow
Salesforce Apex string methods tutorial
20 Apex String Methods Every Salesforce Developer Should Know
Apex Development
How to prevent recursive triggers
How to Prevent Recursive Triggers in Salesforce Apex
Apex Development

Stay Updated with Salesforce Tutorials

Get the latest Salesforce guides, tutorials, and developer tips delivered to your inbox.
slaesforce corner mascot

Explore More Topics

  • salesforce admin
  • salesforce developer
  • Salesforce Admin
  • salesforce apex
  • Salesforce Development
  • salesforce tutorial
  • salesforce security
  • salesforce automation
  • Apex Development
  • lightning web components
  • Lightning Web Components
  • salesforce lwc
  • Salesforce Tutorials
  • Salesforce Tools
  • lwc tutorial
horizontal-dark-transparent

Learn Salesforce development with practical tutorials, Apex guides, integration examples, and real-world solutions for developers.

  • Quick Links:
  • About
  • Contact
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
Facebook Twitter Youtube Linkedin-in

Salesforce Corner © 2026

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?