Oct 29, 2012

Assigning Workflow Task Owner to a Related User

I recently had to come up with a way to assign a workflow task to a related user on the triggering object. That functionality is not available Since a workflow task can only be assigned to the record owner, a specific user or a role. The object that causes the workflow to fire has several user lookup fields, and based on specific events, one of the users specified by these fields needs to be set as the assigned user for the task that is created by the workflow.

I decided to use a trigger on the task object to get the job done. I also decided to write the code in a pretty generic way because I think that there may be other uses for this piece of code. So what I did is create a class called WorkflowInstructions that parses the task description, looking for a familiar string format (a "workflow instruction") and based on what it finds, do something (in this case, assign the task owner to a related user on the originally fired object). Not sure I am doing a good job explaining this in words, so lets get to the actual code...


So now that we have a nice generic class,here is the code for the trigger:
And now, all you have to do to make the workflow work is to set up the workflow instruction in the description of the workflow task. The trigger will fire, change the owner of the task to the related user, and then remove the instruction from the description. here is an example: