Skip to Content
Menu
This question has been flagged
1 Reply
6030 Views

I have an automated action that triggers on Update and Create.  It is intended to be used to prevent duplicate emails being imported into the email contact list.  When a duplicate is found I get the error "Record does not exist or has been deleted".

I would like it to just delete that record or cancel importing that record. Is the problem happening because the record has  not yet been created? My objective is to just skip over that record and not create it and move on to the next one.

new_value = record.email
existing_records = env['mail.mass_mailing.contact'].search([('email', '=ilike', new_value),('id','!=',record.id)])
if existing_records:
record.unlink()

Avatar
Discard
Author

Some help on this would be greatly appreciated if someone could help me out.

Author

Are there really no experts out there who can help to answer this?

Best Answer

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

Avatar
Discard
Related Posts Replies Views Activity
1
May 22
4433
0
Nov 21
21
0
Nov 21
3565
1
Mar 23
2447
1
Jan 23
2666