Odoo 9中自定义工作流活动未在数据库更新的原因排查
wkf_workitem Hey there, let's break down why your custom MRP workflow status isn't syncing to the wkf_workitem table in Odoo 9—this is a common gotcha when building custom workflows, so I've got a few key angles to check:
You're modifying the state field directly instead of using workflow signals
Odoo 9's workflow engine is signal-driven. If your code does something likeself.write({'state': 'new_state'})without triggering the corresponding workflow signal, the engine has no idea the state changed. Instead, you should callself.signal_workflow('your_signal_name')—this tells the workflow engine to process the transition, update thewkf_workitemtable, and handle any associated actions automatically.Workflow instance isn't initialized for new records
Every record needs a correspondingwkf_instanceto track its workflow state. Check if your custom MRP model is properly linked to the workflow definition (via theres_modelfield in the workflow). Also, if you've overridden thecreate()method in your model, make sure you're calling the parentcreate()method—Odoo's base logic handles creating the workflow instance automatically when a record is created, but overridingcreate()without calling super can break this.Workflow transitions are misconfigured
Head to the workflow editor (Settings > Technical > Workflows > Workflows) for your MRP model and verify:- The transition's
act_fromandact_tomatch the correct state nodes. - The signal assigned to the transition matches the one you're triggering in your code.
- Any conditions on the transition are being met (e.g., required fields are filled, boolean flags are set). If a condition fails, the transition won't execute, and
wkf_workitemwon't update.
- The transition's
Triggers for workflow signals are missing
If users are changing the state via a form button or onchange, make sure that action is linked to triggering the workflow signal. For example, a button's action should be set to "Execute Workflow Signal" instead of directly writing to the state field. If you're using an onchange handler, ensure it callssignal_workflow()after validating the new state.Permissions or transaction issues
Check if the user performing the state change has access rights to thewkf_workitemandwkf_instancemodels (these are technical models, so you may need to adjust access rights in your custom module). Also, look for uncommitted transactions in your custom code—if you're wrapping the state change in a transaction that's rolled back, thewkf_workitemupdate won't persist.Check workflow logs for clues
Enable debug mode, then go to Settings > Technical > Workflows > Workflow Instances. Find the instance linked to your MRP record and look at the workflow logs (wkf_log). This will show you if signals were received, transitions were attempted, and any errors that occurred during the workflow process.
Start with verifying you're using workflow signals instead of direct state edits—this is the most frequent culprit. If that checks out, work through the configuration and instance setup steps.
内容的提问来源于stack exchange,提问作者Muhammad Sajid

