0

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

/**
*\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;
    }


}

Trigger

/**
*\arg ClassName  :NotesTrigger
*\arg CreatedOn  :
*\arg LastModifiedOM  :
*\arg CreatedBy    :
*\arg ModifiedBy  :
*\arg Description  :Trigger used to create task after notes are created
*/  
trigger NotesTrigger on Note (after insert) 
{
//Creating instance of handler class.
  NotesHandler objNote = new NotesHandler();

    objNote.onAfterInsert(Trigger.new);

}
1

I made a few edits in the way you check the object type. I think that may fix it, but haven't tried it.

for(Note objNote :lstNote)
{
    Schema.SObjectType objType = objNote.parentId.getSobjectType();

    if( objType != Schema.Task.SObjectType)
    {
    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(objType==Schema.Contact.SObjectType||objType==Schema.Lead.SObjectType)
    ObjTask.WhoId=objNote.parentId;
    else
    objTask.WhatId = objNote.parentId;
    objTask.OwnerId = userId;

    lstTask.add(objTask);

    }
  • An explanatory comment would be worthwhile, i.e. Contacts and Leads are never referenced in Task.WhatId and always referenced via Task.WhoId. All other parent objects for Tasks use Task.Whatid – cropredy Jun 9 '17 at 22:34
  • Actually, he already checked for that. I think his problem was in this line: String strObjType = objType +' '; The space shouldn't have been there. I didn't know that you could actually turn an sOjbectType into a string that way so i just replaced it with something that i knew does work. – Kasper Jun 10 '17 at 0:14

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.