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
dcgb2332dcgb2332 

Auto-Populate Created By field in Task object with Assigned to User (name)

When a task is created, I have a custom pick list on a custom field (Assigned To). I would like that information to populate the Created By field, with an Apex Trigger but am not sure how to start the code
Best Answer chosen by dcgb2332
Raj VakatiRaj Vakati
Try this
 
trigger taskOwnerTrigger on Task (before insert) {
    for (Task taskRe :Trigger.New) {
	taskRe.CreatedById =taskRe.OwnerId;
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
trigger taskOwnerTrigger on Task (before insert) {
    for (Task taskRe :Trigger.New) {
	taskRe.CreatedById =taskRe.OwnerId;
    }
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
User-added image
dcgb2332dcgb2332
Raj,

Thanks so much! Unfortunately it isn't working:

The "Task Assigned to" is meant to match the "Assigned to User" field, nothing happened with the trigger you shared.

User-added image
User-added image

Is it the verbiage that is potentially needing to change?

Thank you!!
dcgb2332dcgb2332
Ok I fgured it out, thank you so much for your help!
trigger Created_By on Task (before insert) {
    for (Task taskRe :Trigger.New) {
    taskRe.CreatedById = taskRe.OwnerId;
    }
}

I needed the owner ID to match up with the created by ID; my API names were off.

Thanks so much!!