Odoo workflow
In simple words workflow is "Perform some action in a organized way". To organize tasks or activities we use workflow. In the term of Computer Science workflow is same as the directed graph, where the nodes are called activities and arcs are called transitions. Here activities are some action that could be done on odoo server such as changing the state of some records based on some condition. We can say that the behaviour of the workflow depends on user action (for example clicking on a button).Follwong screen shot shows that what we are going to do that.
In above image we have four states and each single state represent a group of user. The purpose of creating or using these states is to force the users to perform some action in an organized way. For example before going to the ORIC state we need to perform some checks. The another benefit of using workflows is to display related records to the right user. Now its time to jump into the code.
models.py code
class workflow_example(models.Model):
_name = 'workflow.example'
state= fields.Selection([('PGRC','PGRC'),
('ORIC','ORIC'),
('VC','VC'),
('External Evaluator','External Evaluator')],'State')
Code Description
In above code we have created a model named "workflow_example" and in that model we have created one selection field named "state". This field contains four key value pairs (pgrc,oric,vc and external evaluator). The value of above fields will be shown as the label of workflow.
views.xml code
<page string="External Evaluation">
<header>
<button name="btn_submit_to_oric" string="Submit" type="object" class="oe_highlight" groups="pgrc_user"/>
<newline />
<field name="external_evaluation_sub_state" widget="statusbar" statusbar_visible="PGRC,ORIC,VC,External Evaluator" readonly="1" />
</header>
</page>
Code Description
In above code we have created a <page> inside our form view. In this page we have one button (to change the state after performing some action) and one field (e.g. state). In this field we used widget (widget="statusbar") attribute to show state field values as a statusbar.
Now that we have created our workflow with states and a button. Now its time to handle button event, On button click we want to perform some action and after performing right action we want to move to the next state. Below code shows how to change or move to the next state on button click.
@api.one
def btn_submit_to_oric(self):
if every_thing_is_fine:
# perform some action
self.write({'state':'ORIC'})
else:
# force to perform necessary action