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
AlecSAlecS 

Create a task under my custom object

I have a custom object Invoice. I want that when a certain action is performed, it must keep track of that task under that object.

 

I have this code in one of my class to create the task.

 

Task inv_task = new Task();
	    inv_task.WhatId = theInvoice.id;
            inv_task.WhoId = theInvoice.Contract__r.Name;
            inv_task.Subject = 'Invoice Emailed';
            inv_task.status = 'Completed';
            inv_task.description = 'Invoice emailed';
 insert inv_task;
        

 But I get this error message:

 

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Contact/Lead ID: id value of incorrect type: 00530000000cIRWAA2: [WhoId]

Error is in expression '{!doMyApexLogic}' in component <apex:page> in page apexbuttonpage
 
How can I create a task. Please help!
 
Thanks!
Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can't assign a 005 ("User") to WhoID (must be 003 ("Contact") or 00Q ("Lead")). Instead, to assign the task to a user, use OwnerId instead. Try looking at the SOAP Developer's Guide for details on standard object fields, such as Task.

All Answers

sfdcfoxsfdcfox

You can't assign a 005 ("User") to WhoID (must be 003 ("Contact") or 00Q ("Lead")). Instead, to assign the task to a user, use OwnerId instead. Try looking at the SOAP Developer's Guide for details on standard object fields, such as Task.

This was selected as the best answer
netspidernetspider

you need to

 

inv_task.WhoId = theInvoice.Contract__r.Id;
AlecSAlecS

Thanks, sfdcfox!