Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 39 ยท Projects and Practice

Project: A Helpdesk Workflow

Design an Odoo helpdesk module: ticket model with stages, a validated workflow, SLA deadlines, automatic assignment, kanban board and security, with runnable Python prototypes.

The second project is a helpdesk: customers report problems, agents work on them, managers watch deadlines. It is a good exercise because it uses what a real Odoo business flow needs: a state machine, stages for a kanban board, an SLA clock, automatic assignment and different views for different people.

Odoo is not available on this site. Odoo code is read-only, and each business rule is first prototyped in a runnable Python stand-in.

Plan

Piece Odoo tool
Ticket and stage models models.Model, fields.Many2one for the stage
Board of columns Kanban view with default_group_by="stage_id" and drag and drop
Allowed moves Python method that validates the transition
Deadlines Computed sla_deadline field
Assignment create() override or an automation rule
History mail.thread with tracking=True
Customer replies Mail alias, so emails create tickets
Access Agents see all; customers see only their tickets

Models

Python
class HelpdeskStage(models.Model):
    _name = "helpdesk.stage"
    _description = "Ticket stage"
    _order = "sequence, id"

    name = fields.Char(required=True, translate=True)
    sequence = fields.Integer(default=10)
    is_closed = fields.Boolean()
    fold = fields.Boolean(help="Fold the column in kanban when empty")


class HelpdeskTicket(models.Model):
    _name = "helpdesk.ticket"
    _description = "Support ticket"
    _inherit = ["mail.thread", "mail.activity.mixin"]
    _mail_post_access = "read"

    name = fields.Char("Subject", required=True)
    description = fields.Html()
    partner_id = fields.Many2one("res.partner", string="Customer")
    user_id = fields.Many2one("res.users", string="Agent", tracking=True)
    stage_id = fields.Many2one("helpdesk.stage", tracking=True,
                               group_expand="_read_group_stage_ids",
                               default=lambda self: self.env["helpdesk.stage"].search([], limit=1))
    priority = fields.Selection([("0", "Low"), ("1", "Normal"), ("2", "High"), ("3", "Urgent")],
                                default="1")
    sla_deadline = fields.Datetime(compute="_compute_sla", store=True)
    closed_at = fields.Datetime(readonly=True)

    @api.model
    def _read_group_stage_ids(self, stages, domain):
        return stages.search([], order="sequence")   # show empty stages as columns

group_expand makes the kanban show every stage as a column, even empty ones, so tickets can be dragged into them. In the kanban view dragging a card changes stage_id, which calls write(), which is where you place the validation.

stateDiagram-v2
    [*] --> New
    New --> InProgress: assign
    InProgress --> Waiting: ask customer
    Waiting --> InProgress: customer replies
    InProgress --> Solved: resolve
    Solved --> InProgress: reopen
    Solved --> [*]

Not every stage change is allowed: you cannot jump from New to Solved without work.

Validating transitions

Python
ALLOWED = {
    "New": {"In Progress"},
    "In Progress": {"Waiting", "Solved"},
    "Waiting": {"In Progress"},
    "Solved": {"In Progress"},
}

def write(self, vals):
    if "stage_id" in vals:
        new = self.env["helpdesk.stage"].browse(vals["stage_id"])
        for ticket in self:
            if new.name not in ALLOWED.get(ticket.stage_id.name, set()):
                raise UserError(f"You cannot move a ticket from {ticket.stage_id.name} to {new.name}.")
        if new.is_closed:
            vals["closed_at"] = fields.Datetime.now()
    return super().write(vals)

Relying on stage names is fragile (they are translatable); a production module would put a technical code field on the stage, or use a Selection field for the state next to a stage many2one.

Prototype the workflow

Python Validate stage transitions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

SLA deadlines

A service-level agreement sets a target response time by priority. The deadline is a stored computed field based on the creation date and a priority table. Real deployments count only working hours with the company's resource.calendar; this stand-in uses a plain calendar to keep the idea clear.

Python
@api.depends("priority", "create_date")
def _compute_sla(self):
    hours = {"0": 72, "1": 24, "2": 8, "3": 2}
    for ticket in self:
        base = ticket.create_date or fields.Datetime.now()
        ticket.sla_deadline = base + timedelta(hours=hours[ticket.priority])
Python Compute SLA deadlines and breaches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Assignment strategies

New tickets must find an owner. Common rules are round robin (take turns), least loaded (fewest open tickets) and skill based. In Odoo you put this in create() (or in the automation rule on creation), looking up agents from a group:

Python
@api.model_create_multi
def create(self, vals_list):
    tickets = super().create(vals_list)
    for ticket in tickets.filtered(lambda t: not t.user_id):
        ticket.user_id = self._pick_agent()
    return tickets
Python Least-loaded assignment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Views and security

  • Kanban grouped by stage_id, card colour by priority, an avatar for the agent.
  • Form with a header statusbar field and buttons whose invisible expressions follow the allowed moves (Form Views, Groups and Widgets).
  • Search with filters "My tickets" ([('user_id', '=', uid)]), "Unassigned" and "Overdue".
  • Groups: Agent (sees all tickets) and Manager (configuration and reports). Customers on the portal get a record rule that shows only partner_id = user.partner_id (Record Rules).
  • Email in: a mail alias such as support@yourcompany creates a ticket from each message and threads replies through chatter (Automated Actions, Cron Jobs and Email).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does group_expand on the stage field achieve in a kanban view?

    group_expand supplies the full list of groups so empty columns appear and cards can be dropped into them.
  2. 2. Where is the best place to validate a stage change made by dragging a card?

    Dragging calls write on the model, so the rule must live there, not only in the view.
  3. 3. Why is the SLA deadline a stored computed field?

    Stored fields are database columns, which makes filters like 'overdue' fast.
  4. 4. Which record rule limits portal customers to their own tickets?

    The domain matches tickets whose customer is the logged-in portal user's partner.

Summary

  • A helpdesk combines stages, a validated workflow, SLA fields and assignment rules.
  • Validate moves in write(), since kanban drag and drop, forms and API calls all go through it.
  • Compute deadlines in stored fields so you can filter on breaches.
  • Assignment can be a small method in create(); mail aliases turn emails into tickets.
  • Different groups and record rules give agents and customers different views of the same model.

Next: Practice Questions and Interview Prep