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
Kimberly Wargo 1Kimberly Wargo 1 

Write a trigger to reassign the task owner

I hope this is an easy one.. I have not yet started writing triggers so it's all foreign to me.
I need to write a trigger to reassign a task based on a subject content to a lookup user in the record.
We have mutliple people that handle an account and I created a workflow/rule to automatically assign a task based on a specific criteria but I cannot assign it to another user besides the account owner.
Any help is appreciated.
Best Answer chosen by Kimberly Wargo 1
Siddharth ManiSiddharth Mani
If I understand right, your requirement is to write a trigger to assign a task to some user (other than the account owner) on creation of a Account (can be later changed for update as well i guess). See if this works out for you:
 
trigger assignTask on Account (after insert) {
  Task newTask = new Task();
  List<Account> accList = new List<Account>();
  for(Account acc : Trigger.new) {
     accList.add(acc);
  }
    for(Account ac : accList) {
    newTask.WhatId= ac.Id;
    newTask.Subject = 'Test Subject';
    newTask.OwnerId = '00528000001IUKN';
}
insert newTask;
}

This is the simplest i could think of and you can substitute the ownerId to be any User's Id (You can add other fields as necessary inside the for loop).
Of course this will need more work if you have some conditions to check before assigning Users to a task.
Let me know if this helps!

All Answers

Siddharth ManiSiddharth Mani
If I understand right, your requirement is to write a trigger to assign a task to some user (other than the account owner) on creation of a Account (can be later changed for update as well i guess). See if this works out for you:
 
trigger assignTask on Account (after insert) {
  Task newTask = new Task();
  List<Account> accList = new List<Account>();
  for(Account acc : Trigger.new) {
     accList.add(acc);
  }
    for(Account ac : accList) {
    newTask.WhatId= ac.Id;
    newTask.Subject = 'Test Subject';
    newTask.OwnerId = '00528000001IUKN';
}
insert newTask;
}

This is the simplest i could think of and you can substitute the ownerId to be any User's Id (You can add other fields as necessary inside the for loop).
Of course this will need more work if you have some conditions to check before assigning Users to a task.
Let me know if this helps!
This was selected as the best answer
Mahesh DMahesh D
Hi Kimberly,

If you can explain more about tour requirement along with an example then it will be easy for us to help you.

Regards,
Mahesh