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
Zach Gavin DelacroixZach Gavin Delacroix 

FIELD_INTEGRITY_EXCEPTION Error

Hello Guys,

I'd like to ask for help on how to resolve this particular issue. I don't get syntax Error but when I execute the trigger, it returns me this Error.

Error:Apex trigger SkedCreateTasksTrigger caused an unexpected exception, contact your administrator: SkedCreateTasksTrigger: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Name ID: id value of incorrect type: 00528000002rYgOAAU: [WhoId]: Trigger.SkedCreateTasksTrigger: line 30, column 1


Story:
        I have VA Task and a Standard Salesforce Task (Activity) Related List under My JOB object.

        Basically, when I update the Job Status to Complete, I'd like to have a copy of VA Task as a Standard Salesforce Task for each VA Task.

CODE:
trigger SkedCreateTasksTrigger on sked__Job__c (before Update) {
    
    set<id> jobId = new set<id>();
    
    for(sked__Job__c job: trigger.new){
        if(job.sked__Job_Status__c == 'Complete'){
            jobId.add(job.id);
        }
    }
    
    list<Task> newTasks = new list<Task>();
    
    for(VA_Task__c vaTask : [select Job__c, Comments__c,Priority__c,Status__c,Subject__c,Assigned_To__r.id from VA_Task__c where job__c In: jobId]){
        
        Task t = new Task();
        t.WhoId = vaTask.Assigned_To__r.id;
        t.WhatId = vaTask.Job__c;
        t.priority = vaTask.Priority__c;
        t.Status = 'Not Started';
        t.Subject = vaTask.Subject__c;
        t.Description = vaTask.Comments__c;
        
        newTasks.add(t);
    }
    
    for(integer i=0;i<newTasks.size();i++){
        system.debug(newTasks[i].whoId);
    }
    
    insert newTasks;
}

How Can I fix the Error?

Thanks,

Zach
Best Answer chosen by Zach Gavin Delacroix
William Christ NYWilliam Christ NY
Zach,

The issue is on line 16.  "t.WhoId" can only accept the record Id for a Lead or Contact record.  The code is attempting to assign the Id of User record to it, which is not compatible.  I think the user assigned to the Task should be assigned to "t.OwnerId" instead.

William

All Answers

William Christ NYWilliam Christ NY
Zach,

The issue is on line 16.  "t.WhoId" can only accept the record Id for a Lead or Contact record.  The code is attempting to assign the Id of User record to it, which is not compatible.  I think the user assigned to the Task should be assigned to "t.OwnerId" instead.

William
This was selected as the best answer
Zach Gavin DelacroixZach Gavin Delacroix
Ah, that's perfect. the OwnerId worked :)

Thanks a lot William for the Info.

You're awesome!

Zach