Hi,
The error message "Record does not exist or has been deleted" typically
occurs when you attempt to perform an operation on a record that doesn't
exist. In your code, you're trying to delete a record using record.
unlink(), but it seems like the record may not exist or hasn't been
created yet when the action is triggered, leading to this error.
new_value = record.email
existing_records = env['mail.mass_mailing.contact'].search([('email', '=ilike', new_value), ('id', '!=', record.id)])
if existing_records:
raise ValidationError("A record with this email already exists.")
By
raising a ValidationError when a duplicate email is found, you will
prevent the creation of a new record and generate an error message. This
will effectively skip over the duplicate record and move on to the next
one, without creating duplicates in the first place.
or
If you want to resolve the "Record does not exist or has been deleted"
issue when attempting to delete a record with record.unlink(), you need
to ensure that the record exists and that it hasn't been deleted before
calling the unlink method.
new_value = record.email
existing_records = env['mail.mass_mailing.contact'].search([('email', '=ilike', new_value), ('id', '!=', record.id)])
if existing_records:
# Check if the record exists before attempting to delete it
if record.exists():
record.unlink()
else:
# Handle the case where the record doesn't exist
# You can choose to log a message or take other actions as needed
pass
Here's
how you can create a constraint in your code to prevent the creation of
a new record when a duplicate email is found and generate an error
message:
from odoo import models, fields, api
from odoo.exceptions import ValidationError
class MassMailingContact(models.Model):
_name = 'mail.mass_mailing.contact'
email = fields.Char(string='Email', required=True, unique=True)
@api.constrains('email')
def _check_duplicate_email(self):
for record in self:
existing_records = self.search([('email', '=ilike', record.email), ('id', '!=', record.id)])
if existing_records:
raise ValidationError("A record with this email already exists.")
Hope it helps
Some help on this would be greatly appreciated if someone could help me out.
Are there really no experts out there who can help to answer this?