# Automatic Action: change the assignee if Category change

**URL:** <https://kanboard.discourse.group/t/automatic-action-change-the-assignee-if-category-change/2705>\
**Category:** Questions\
**Created:** [March 30, 2023, 10:42am UTC](https://kanboard.discourse.group/t/automatic-action-change-the-assignee-if-category-change/2705 "2023-03-30T10:42:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rischfre](https://avatars.discourse-cdn.com/v4/letter/r/53a042/32.png) [@rischfre](https://kanboard.discourse.group/u/rischfre)\
**Post date:** [March 30, 2023, 10:42am UTC](https://kanboard.discourse.group/t/automatic-action-change-the-assignee-if-category-change/2705/1 "2023-03-30T10:42:31Z")

</div>

Hi,

i noticed that there is no existing rule, letting create a new rule: _if the Task category changes, then changes as well the assignee._

is it possible to “program it”? i have few php knowledge, but i may try, knowing where i have to start.

Could you please give me some hints?

Thank you

---

<div class="post-metadata">

**Author:** ![alfredb](https://yyz2.discourse-cdn.com/free1/user_avatar/kanboard.discourse.group/alfredb/32/1051_2.png) [@alfredb](https://kanboard.discourse.group/u/alfredb)\
**Post date:** [March 30, 2023, 11:45am UTC](https://kanboard.discourse.group/t/automatic-action-change-the-assignee-if-category-change/2705/2 "2023-03-30T11:45:31Z")

</div>

> [@rischfre](#):
>
> some hints?

Out of the box:

- Create an event for category change.
- Find out where the category\_id of a task changes.
- Fire that event whenever a change occurs.
- Create an action that is listening to your event.
- Within that action, do whatever you want.

Sidenote: This topic might be too heavy for starting. Just my opinion.

---

<div class="post-metadata">

**Author:** ![creecros](https://yyz2.discourse-cdn.com/free1/user_avatar/kanboard.discourse.group/creecros/32/156_2.png) [@creecros](https://kanboard.discourse.group/u/creecros)\
**Post date:** [March 30, 2023, 12:27pm UTC](https://kanboard.discourse.group/t/automatic-action-change-the-assignee-if-category-change/2705/3 "2023-03-30T12:27:54Z")

</div>

You don’t need a new event, since there is already `TaskModel::EVENT_CREATE_UPDATE`

but you would need a new action, one where the `hasRequiredCondition` is looking for the catergory

```auto
public function hasRequiredCondition(array $data)
    {
        return $data['task']['category_id'] == $this->getParam('category_id');
    }

```

and then the `doAction` sets the new assignee.

edit:  
This is the cloest action available, _if the task category changes, then color changes_

> <https://github.com/kanboard/kanboard/blob/main/app/Action/TaskAssignColorCategory.php>

this would be very simple to convert into an action that does exactly what you want, in fact, here I’ll do it for you.

```auto
<?php

namespace Kanboard\Action;

use Kanboard\Model\TaskModel;

/**
 * Assign a user to a specific category
 *
 * @package Kanboard\Action
 * @author Craig Crosby
 */
class TaskAssigneeByCategory extends Base
{
    /**
     * Get automatic action description
     *
     * @access public
     * @return string
     */
    public function getDescription()
    {
        return t('Assign automatically a tasks assignee based on a category');
    }

    /**
     * Get the list of compatible events
     *
     * @access public
     * @return array
     */
    public function getCompatibleEvents()
    {
        return array(
            TaskModel::EVENT_CREATE_UPDATE,
        );
    }

    /**
     * Get the required parameter for the action (defined by the user)
     *
     * @access public
     * @return array
     */
    public function getActionRequiredParameters()
    {
        return array(
            'user_id' => t('Assignee'),
            'category_id' => t('Category'),
        );
    }

    /**
     * Get the required parameter for the event
     *
     * @access public
     * @return string[]
     */
    public function getEventRequiredParameters()
    {
        return array(
            'task_id',
            'task' => array(
                'project_id',
                'category_id',
            ),
        );
    }

    /**
     * Execute the action (change the task color)
     *
     * @access public
     * @param array $data Event data dictionary
     * @return bool True if the action was executed or false when not executed
     */
    public function doAction(array $data)
    {
        $values = array(
            'id' => $data['task_id'],
            'owner_id' => $this->getParam('user_id'),
        );

        return $this->taskModificationModel->update($values, false);
    }

    /**
     * Check if the event data meet the action condition
     *
     * @access public
     * @param array $data Event data dictionary
     * @return bool
     */
    public function hasRequiredCondition(array $data)
    {
        return $data['task']['category_id'] == $this->getParam('category_id');
    }
}

```

now, how you add this action is up to you, if via plugin, you will need to change the namespace.
