4
 public Event returnEventOppObj(Opportunity opp1, Sobject sobj){
        Event e2 = New Event(Subject='Call1',Type = 'Call1', StartDateTime = datetime.now(), EndDateTime = datetime.now(), Ownerid =userinfo.getUserId(), WhatId=opp1.id, WhoId=sobj.Id);
        return e2;
    }

if I call this method where sObject is Lead, it will throwing exception as:

FIELD_INTEGRITY_EXCEPTION, You can't relate a lead to an account or other object.: [WhatId]

2 Answers 2

13

from documentation

WhoId: Description: ID of a related Contact or Lead. If the WhoId refers to a lead, then the WhatId field must be empty. Label is Contact/Lead ID. If Shared Activities is enabled, this is the ID of a related Lead or primary Contact.

The same behavior you can observe when you want to add an event on the page manually. When you pick 'Lead' then 'Related To' is grayed.

1
  • 1
    I can't find the cited text in the linked SF documentation. Obsolete link?
    – jsalvata
    Commented Sep 1, 2020 at 11:49
0

THANK YOU for posting this!

FYI in case anyone else ever runs into this -- this error will fire if you're running a test class. Similarly, the WhatId field will also be greyed out if you're on a task related to a lead in the UI. However, the error will NOT fire if you're just using the API. So if you ever write a class to update WhatId on a task related to a lead, keep in mind that you can't have your test classes actually assert the WhatId action update (even though it will still work if you call the class with a normal UI action rather than via the test class).

It ate up so much of my time to get to the bottom of this. Hope this info saves someone else some trouble in the future : )

1
  • this answer is a bit confusing and could do with some editing and specific code examples to illustrate your point
    – cropredy
    Commented Jun 14, 2018 at 1:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .