0

I am looking if we can write a trigger on user object to update the task object fields, need is to update the task owner field whenever the user name is changed.

Looking for help,

Thanks,

Savyasachi

3
  • 1
    This is doable. What you have tried so far to achieve this?
    – Ashwani
    Commented Jan 26, 2017 at 14:28
  • I have never heard any such requirement, probably because you might have a text field. If you use a lookup field, its name would be updated automatically.
    – Raul
    Commented Jan 26, 2017 at 14:31
  • @ Rahul Sharma, it is text field, we do not have lookup
    – user41219
    Commented Jan 26, 2017 at 14:39

1 Answer 1

0

what you have to do in your trigger is to check whether the user name changed, and if so, you have to query the tasks that have your text field as the Id of the user who changed the username.

this is a sample solution:

trigger <your_trigger_name> on User (after update,...) {
List<Task> tasksToUpdate=  new List<tasksToUpdate>();

for(User newUser : Trigge.new){
    User oldUser = trigger.oldMap.get(newUser.Id);

    if(newUser.username != oldUser.username) {
        for(Task tsk :[select Id, <your_text_field>,... from Task where <your_text_field> = newUser.Id]){
            // Here you modify the filds you want
            ...

            // Adn then add tehe Task to the list of updates
            tasksToUpdate.add(tsk);
        }
    }
}

if(!tasksToUpdate.isEmpty()){
    update tasksToUpdate;
}

}

I hope this helps you

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .