Set emails to UNIQUE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drychly
    Junior Member
    • May 2025
    • 4

    #1

    Set emails to UNIQUE

    Hi, is it possible to set the email address field for Users and Contacts as UNIQUE in EspoCRM to prevent duplicates? If so, how can this be configured?
  • lazovic
    Super Moderator
    • Jan 2022
    • 1045

    #2
    Hi drychly,

    You can either configure the fields for duplicate check in the Contact and User entity settings (Administration > Entity Manager > {entity_name} > Edit) or use the API Before-Save Script: https://docs.espocrm.com/administrat...licateconflict.

    Comment

    • drychly
      Junior Member
      • May 2025
      • 4

      #3
      Hi @lazovic,

      I am not able to do it via Entity Manager. My current EspoCRM version is 9.1.2.

      These are the options I see there:
      Click image for larger version

Name:	image.png
Views:	9
Size:	116.1 KB
ID:	118186
      I will try then via API Before-Save Script.

      Thank you

      Comment

      • lazovic
        Super Moderator
        • Jan 2022
        • 1045

        #4
        drychly,

        You need to go directly to the Administration > Entity Manager > Contact > Edit (button):

        Click image for larger version

Name:	image.png
Views:	5
Size:	64.8 KB
ID:	118188

        Here you can see the "Duplicate check fields" option at the bottom of the page.

        Comment

        • drychly
          Junior Member
          • May 2025
          • 4

          #5
          Ah, I see. Thanks. But this will only warn user that the e-mail is duplicate, and will allow to create the contact anyways.

          Comment

          • lazovic
            Super Moderator
            • Jan 2022
            • 1045

            #6
            drychly,

            In this case, you can use the API Before-Save Script.

            In Administration > Entity Manager > Contact > Formula > API Before Save Script:
            Code:
            $id = record\findOne('Contact', null, null, 'emailAddress=', emailAddress);
            
            if ($id) {
                recordService\throwBadRequest('There is already a contact with this email address in the system.')
            }
            In Administration > Entity Manager > User > Formula > API Before Save Script:
            Code:
            $id = record\findOne('User', null, null, 'emailAddress=', emailAddress);
            
            if ($id) {
                recordService\throwBadRequest('There is already a user with this email address in the system.')
            }

            Comment

            • drychly
              Junior Member
              • May 2025
              • 4

              #7
              Thanks, it works.

              Comment

              Working...