+ Start a Discussion
wedaftwedaft 

Problem with Apex trigger to create a new task with populated fields

Hi everyone,

 

I am trying to create a Apex trigger that automatically creates a new task record for me once a checkbox field named "School Assigned" on my custom object, "Buffalo", is checked. I want a lot of the fields in the new task record to be automatically populated in the process. The code creates the new tasks fine, but unfortunately I have been having a lot of trouble setting up the code to populate the following default fields on the Task object: 1. Owner (AKA Assigned to) and 2. What (AKA Related to). The code is copied below, with the problem lines of code in blue and the corresponding error message I get in red. I know very little about Apex, any help is very much appreciated!

 

--------------------------------------------------------------------------------------------------------------------------------------------------------

 

trigger createTaskOnBuffaloX on Buffalo__c (after insert, after update) {

List <task> taskToInsert = new List <task> ();

for (Buffalo__c b : Trigger.new) {

if (b.School_assigned__c == true) {

task task = new task ();

task.ActivityDate = b.First_CO__c;
task.Subject = 'First CO';
task.owner = b.owner;   Error: Compile Error: Field is not writeable: Owner

task.what = b.name; Error: Compile Error: Illegal assignment from String to SOBJECT:Name



taskToInsert.add(task);


}//end if

}//end for o

try {
insert taskToInsert;
} catch (system.Dmlexception e) {
system.debug (e);
}

}

Best Answer chosen by Admin (Salesforce Developers) 
nagalakshminagalakshmi

Hi,

 

Whatid is the id of record. But you are trying to insert the name insted of id(string in to id). So its getting error. Try to insert id in the place of name i.e

 task.WhatId = b.id;

 

Thanks

All Answers

TheresaAndersonTheresaAnderson

Try Task.OwnerId = x.OwnerId;

       Task.WhatId = x.WhatId;

wedaftwedaft

Okay, so Owner line is working now, so thanks a lot for that!

 

I can also get the code to save without an error message using the following: task.WhatId = b.Name;

 

But, when I actually try to run the trigger by going to the "Buffalo" record and checking the "school assigned" box, I get the following message which prevents me from saving the record:

 

Error:Apex trigger createTaskOnBuffaloX caused an unexpected exception, contact your administrator: createTaskOnBuffaloX: execution of AfterUpdate caused by: System.StringException: Invalid id: meat: Trigger.createTaskOnBuffaloX: line 14, column 1

nagalakshminagalakshmi

Hi,

 

Whatid is the id of record. But you are trying to insert the name insted of id(string in to id). So its getting error. Try to insert id in the place of name i.e

 task.WhatId = b.id;

 

Thanks

This was selected as the best answer
wedaftwedaft

Thanks! That's working now.

 

One last question: for some reason, the trigger is generating two tasks that look almost identical instead of just one. Why is that?

 

Again, here's the code:

 

trigger createTaskOnBuffaloX on Buffalo__c (after update) {

List <task> taskToInsert = new List <task> ();

for (Buffalo__c b : Trigger.new) {

if (b.School_assigned__c == true)
if (b.task_created__c == false) {

task task = new task ();

task.ActivityDate = b.First_CO__c; // and so on so forth untill you map all the fields.
task.Subject = 'First CO';
task.OwnerId = b.OwnerId;
task.WhatId = b.id;

taskToInsert.add(task);


}//end if

}//end for o

//once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.
try {
insert taskToInsert;
} catch (system.Dmlexception e) {
system.debug (e);
}

}

wedaftwedaft

Ok I figured out the answer to my last question. It's because I have a workflow in place that is causing the record to update twice, thus creating two tasks. I think I need to add an extra IF statement so that only after the workflow update has already occured does the trigger fire.

 

What I need specifically, is an if statement that says that the "school begin" field must not be blank. So basically something opposite of this: if (b.school_begin__c == null)