function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Todd KadasTodd Kadas 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type: 00Qo000000UcO86EAF: [WhatId]

Hoping someone can help me out.  I'm a bit new here.  Error message and Apex class provided below.  I have a trigger that fires after a user creates a new note in Notes & Attachment resulting in a task being created.  Below script then populates fields in the task to complete it.  Works fine when notes are created in Accounts and Opportunities but throws below error when notes are created in Leads or Contacts.  

If you're scratching your head around why this would have been created, it's that several of our users complained that they weren't getting credit for activities they perform throughout the week, such as updating account data with notes.  By using this, when management reviews their teams activities, each note is counted as 1 activity.

Apex script unhandled trigger exception by user/organization: 005o0000001aU9R/00D0m000000CmUV Source organization: 00Do0000000ZWI0 (null)
NotesTrigger: execution of AfterInsert
 
caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type: 00Qo000000UcO86EAF: [WhatId]
 
Class.NotesHandler.onAfterInsert: line 54, column 1
Trigger.NotesTrigger: line 14, column 1
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
*\arg ClassName    :NotesHandler
*\arg CreatedOn    :
*\arg LastModifiedOn    :
*\arg CreatedBy    :
*\arg ModifiedBy    :
*\arg Description    :Handler class to create task after notes are inserted
*/
public with sharing class NotesHandler
{
    /*Start - Constructor*/
    public NotesHandler()
    {
        //do nothing
    }
    /* End - Constructor*/
    
    /**
    @MethodName    :onAfterUpdate
    @Param    :
    @Description:
    **/
    public void onAfterInsert(List<Note>lstNote)
    {
    String userID=UserInfo.getUserId();
    List<Task>lstTask=new List<Task>();
    for(Note objNote :lstNote)
    {
        Schema.SObjectType objType=objNote.parentId.getSobjectType();
        String strObjType = objType+' ';
        if( strObjType != 'Task')
        {
        Task objTask = new Task();
        objTask.Subject = 'Note created -'+objNote.Title;
        objTask.Priority = 'Normal';
        objTask.Type = 'Note created';
        objTask.Status = 'Completed';
        objTask.ActivityDate = objNote.CreatedDate.Date();
        objTask.Task_Completion_Date__c = objNote.CreatedDate;
        if(strObjType=='Contact'||strObjType=='Lead')
        ObjTask.WhoId=objNote.parentId;
        else
        objTask.WhatId = objNote.parentId;
        objTask.OwnerId = userId;
        
        lstTask.add(objTask);
        
        }
   
        
    }
     
     if(!lstTask.isEmpty())
         insert lstTask;
    }
    
    
}
HARSHIL U PARIKHHARSHIL U PARIKH
On line 44, is it suppose to be something DOT userId (e.g., objNote.userId) ?
Todd KadasTodd Kadas
Hi Govind-another user helped me find the error.  It was on line 30. This: String strObjType = objType+' ' should have been this: String strObjType = objType+'';.  Oversight on my part.  Thanks for the reply either way...