Find your content:

Search form

You are here

FIELD_INTEGRITY_EXCEPTION, You can't relate a lead to an account or other Object

 
Share
 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]


Attribution to: Bharat

Possible Suggestion/Solution #1

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.


Attribution to: macslo

Possible Suggestion/Solution #2

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 : )


Attribution to: Palmira Angelova
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31309

My Block Status

My Block Content