I have this custom component built on Contact that creates a Task record. The goal is to modify these functionalities to also work on Lead and Account objects. Had no issues making it work fully on Lead, but am running into errors when trying to save it with Account.

Error while SavingErrorInsert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Name ID: id value of incorrect type: 0012C00000MOiyeQAD: [WhoId]

I've found a few places in the code that could be causing this error on the save but not sure which is the ultimate culprit. Shown below:

Apex Controller:

    if(WhoId.getSobjectType() == Case.SobjectType){
        Case c = [Select ContactId from Case where Id=:WhoId limit 1];    
        cntrl.Contact = c.ContactId;             
    else if(WhoId.getSobjectType() == Opportunity.SobjectType){
        Opportunity opp = [Select Contact__c from Opportunity where Id=:WhoId limit 1];    
        cntrl.Contact = opp.Contact__c;             
    else if(WhoId.getSobjectType() == Lead.SobjectType){
        Lead ld = [Select Id from Lead where Id=:WhoId limit 1];    
        cntrl.Contact = ld.Id;  
    else if(WhoId.getSobjectType() == Account.SobjectType){
        Account acc = [Select Id from Account where Id=:WhoId limit 1];    
        cntrl.Contact = acc.Id;         
        Contact c = [Select Id from Contact where Id = :WhoId]; 
        cntrl.Contact = c.Id; 
// Create Task with Primary Contact
newTsk.WhoId = selectedwhoIdsList[0].id;
insert newTsk;

handleSaveTask of Controller

            var SelectdWhoids = component.get("v.newTask.WhoId");  
            console.log('selected who id****'+SelectdWhoids);
            // Server call 1: To create one completed task (Inbound/Outbound Hit) 
            // 2: To update status of Selected Tasks to Completed 
            // 3. To create discussion on call activites for selected opportunities
            var action = component.get("c.UpsertTaskRecs");        
            action.setParams({"recordId":component.get('v.recordId'),objTsk: component.get("v.newTask"),whoids: SelectdWhoids,"updateTask":selectedTaskLst,"Opps":selectedOppLst,"Branch":branchRelation,
            action.setCallback(this, function(response) {
                var state = response.getState();
                if (state === "SUCCESS") {                
                    var result = response.getReturnValue();
                    if(result=='Success') {     
                            "variant": "success",
                            "title": "Success",
                            "message": "Task was created"
                        // Refresh component
                        if($A.get("$Browser.formFactor") === "DESKTOP"){
                            var e = component.getEvent("refreshCallScriptCmp");
                            e.setParams({ "componentName": "LogCall"});
                            var dismissActionPanel = $A.get("e.force:closeQuickAction"); 
                    else {
                            "variant": "error",
                            "title": "ERROR",
                            "message": "Error while Saving"+result
                    // Enable the button
                    btn.set('v.disabled', false);

Any ideas how to remedy this?


You seem to be missing some code to provide complete context, but Accounts cannot go in the WhoId of a task. Only Contacts and Leads can. Accounts go on the WhatId, as do Opportunities and other objects.

The only exception to that is if you're using Person Accounts. In that case, you can use the PersonContactId of the Account object and put that in the whoId, and you could also put the Id in the WhatId if you so desired.

From the docs:

The WhoId represents a human such as a lead or a contact. WhoIds are polymorphic. Polymorphic means a WhoId is equivalent to a contact’s ID or a lead’s ID. The label is Name ID.

The WhatId represents nonhuman objects such as accounts, opportunities, campaigns, cases, or custom objects. WhatIds are polymorphic. Polymorphic means a WhatId is equivalent to the ID of a related object. The label is Related To ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.