from odoo import models


class StockMoveLine(models.Model):
    _inherit = "stock.move.line"

    def _action_done(self):
        res = super()._action_done()
        for line in self.filtered(lambda l: l.result_package_id):
            line.result_package_id.auto_assign_packaging()
        return res

from odoo import models


class StockMoveLine(models.Model):
    _inherit = "stock.move.line"

    def _action_done(self):
        res = super()._action_done()
        # _action_done in stock module sometimes delete a move line, we
        # have to check if it still exists before reading/writing on it
        for line in self.exists().filtered(lambda l: l.result_package_id):
            line.result_package_id.auto_assign_packaging()
        return res

Fix "record does not exist" when validating move line
Fix "record does not exist" when validating move line

When _action_done is called on a transfer, it may delete a part of
the move lines. The extension of `_action_done()` that assigns
a packaging fails with "Record does not exist or has been deleted".

Check if the if lines still exist before writing on them.

  from odoo import models
  
  
  class StockMoveLine(models.Model):
      _inherit = "stock.move.line"
  
      def _action_done(self):
          res = super()._action_done()
+         # _action_done in stock module sometimes delete a move line, we
+         # have to check if it still exists before reading/writing on it
-         for line in self.filtered(lambda l: l.result_package_id):
+         for line in self.exists().filtered(lambda l: l.result_package_id):
              line.result_package_id.auto_assign_packaging()
          return res
  
Fix "record does not exist" when validating move line
## Code Before:
from odoo import models


class StockMoveLine(models.Model):
    _inherit = "stock.move.line"

    def _action_done(self):
        res = super()._action_done()
        for line in self.filtered(lambda l: l.result_package_id):
            line.result_package_id.auto_assign_packaging()
        return res

## Instruction:
Fix "record does not exist" when validating move line
## Code After:
from odoo import models


class StockMoveLine(models.Model):
    _inherit = "stock.move.line"

    def _action_done(self):
        res = super()._action_done()
        # _action_done in stock module sometimes delete a move line, we
        # have to check if it still exists before reading/writing on it
        for line in self.exists().filtered(lambda l: l.result_package_id):
            line.result_package_id.auto_assign_packaging()
        return res
