Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Diagram illustrating the key differences between Salesforce WhoId and WhatId relationship fields
Apex

WhatId vs WhoId in Salesforce: What They Mean & When to Use Each

WhatId references non-Person records (Account, Opportunity, Custom Objects). WhoId references people (Lead or Contact). On Tasks and Events the two fields behave differently — here's exactly how.

What is the "difference between WhoId and WhatId?" is the most obvious question when you start working on activities for first time. It sounds confusing what exactly it refers to.

If you try to create an event or task from UI, you can see two fields.

Difference between WhoId and WhatId

Task Layout

  • Name : Here you can either choose Lead or Contact. It refers to people.
  • Related to : All Standard/Custom objects except Lead and Contact. It refers to objectives.

Lets take an example, Sales Rep "Rob" wants to Schedule a follow up with Contact "Mark" regarding his Opportunity which is yet to be closed. In this scenario, this information should be mapped as follows:

  • Assigned To : Sales Rep "Rob" who is scheduling meeting.
  • Name : Contact "Mark".
  • Related To : Opportunity related to contact "Mark".

What are the difference between WhoId and WhatID?

WhoId is API name of Name fields and WhatId is API name of Related To field. If you want to fetch task information, you need to use following SOQL:

SELECT Id, WhoId, WhatId, Subject from Task

These field are polymorphic in nature.

What is a polymorphic field?

A polymorphic field is one where the record can linked with multiple other types of objects. E.g: WhoID can only be linked with Contact or Lead records. WhatID can linked to all other objects excluding Contact and Leads. You cannot create such kind of fields in any if the Standard/Custom object.

Frequently asked questions

What is WhatId in Salesforce?

WhatId is a polymorphic relationship field on the Task and Event objects that points to a non-person record — Account, Opportunity, Campaign, Contract, or any Custom Object that has activities enabled. It's the 'Related To' field in the Task/Event UI. WhatId is required when the task isn't linked to a person.

What is the difference between WhoId and WhatId?

WhoId references people — specifically a Lead or a Contact. WhatId references things — Account, Opportunity, Campaign, custom objects, etc. Both are polymorphic lookup fields that exist only on Task and Event records. A given activity can have one of each, but only one Who and one What at a time (unless Shared Activities is enabled, which allows up to 50 Contacts on a single activity).

Can WhoId point to an Account?

No. WhoId only accepts Lead or Contact IDs. If you assign an Account ID to WhoId in Apex, the DML throws an INVALID_FIELD_FOR_INSERT_UPDATE error. To attach a Task to an Account, use WhatId instead. The Task's UI 'Name' field maps to WhoId, while 'Related To' maps to WhatId.

Are WhatId and WhoId polymorphic?

Yes — both are polymorphic lookup fields. You query the related object via TYPEOF in SOQL: SELECT Id, WhatId, TYPEOF What WHEN Account THEN Name END FROM Task. In Apex, cast the result based on Schema.SObjectType: String objType = task.WhatId.getSObjectType().getDescribe().getName();

Which standard objects can be referenced by WhatId?

Out of the box: Account, Opportunity, Campaign, Contract, Order, Solution, Asset, Quote, and Product2. Any Custom Object becomes a valid WhatId target once you enable 'Allow Activities' on the object definition. Cases and Leads are intentionally excluded — they have their own activity relationships.

How do I query a Task's related Account using WhatId?

Use the relationship name 'What' (without Id) and TYPEOF in SOQL: SELECT Id, Subject, TYPEOF What WHEN Account THEN Name, Industry WHEN Opportunity THEN StageName END FROM Task WHERE Id = :taskId. The TYPEOF clause is required because What is polymorphic — Salesforce cannot guess the field set otherwise.

Can I update WhoId or WhatId in Apex?

Yes, but with two caveats: (1) If Shared Activities is enabled, you should use the TaskWhoRelation/EventWhoRelation junction objects to add multiple Contacts instead of overwriting WhoId. (2) Updating either field re-fires assignment rules and triggers — be careful in bulk operations to avoid governor-limit issues.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment