Check duplicate failed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oraculo1
    Junior Member
    • Feb 2021
    • 22

    #1

    Check duplicate failed?

    Hi,

    Version 8 allows you to define the fields to check for duplicates. If the value exists, a warning pops up indicating that it could be a duplicate. If you cancel it, it doesn't save it, but if you accept it, it does. The problem is that if, for example:
    I have two users:
    -user1 and user2
    Each user can only create, edit, and view their own information.
    If user1 adds a contact with the phone number 222222222, and user2 adds another contact with the same phone number, the check doesn't pop up, which ultimately results in duplicate records.
    Any ideas, please?

    Thanks,
  • victor
    Active Community Member
    • Aug 2022
    • 1024

    #2
    The simplest solution might be to create a single team for these users, where they can see each other's contacts. In that case, the duplicate search rule will work.

    A more complex solution would be to use https://docs.espocrm.com/administrat...icate-checking.

    Comment


    • oraculo1
      oraculo1 commented
      Editing a comment
      Hi, victor,

      I considered creating teams, but it doesn't work for the client, as they want each team to be able to see only what they create, which causes quite a few problems. Regarding the Before-Save API option you mentioned, where is that code snippet actually included? I don't know if it's in the field's formula, in the entity. Honestly, I'm completely unfamiliar with that part.
      Thanks again.
  • heint
    Junior Member
    • Jun 2025
    • 28

    #3
    greetings, oraculo1,

    To add duplicate checking to your Contact entity for the Phone field, please follow the Administration > Entity Manager > Contact > Formula > API Before Save Script route.

    Copy this formula to the box:

    1. If you want to duplicate check on creation:
    Code:
    if (entity\isNew() && !recordService\skipDuplicateCheck()) {
    $id = record\findOne('Contact', null, null, 'phoneNumber=', phoneNumber);
    
    if ($id) {
    recordService\throwDuplicateConflict($id);
    }
    }
    2. If you want to duplicate check on update only:
    Code:
    if (!recordService\skipDuplicateCheck()) {
    $id = record\findOne('Contact', null, null, 'phoneNumber=', phoneNumber);
    
    if ($id) {
    recordService\throwDuplicateConflict($id);
    }
    }
    And click Save after this.​

    Comment


    • oraculo1
      oraculo1 commented
      Editing a comment
      Hi heint, it's work. IF i want the same for another fields, could tell me how will be the snippet? By other way, is there a way to hide the info od duplicate record in message and only see "record exists" or similar? Or if just exists, don't show save button.
      Thanks a lot.
  • heint
    Junior Member
    • Jun 2025
    • 28

    #4
    oraculo1
    Here is the code where duplicate notification has changed. You can edit the "This Phone number already exists in the system" message to any suitable for you.
    Code:
    if (!recordService\skipDuplicateCheck()) {
    $id = record\findOne('Contact', null, null, 'phoneNumber=', phoneNumber);
    
    if ($id) {
    recordService\throwBadRequest("This Phone number already exists in the system.");
    }
    }
    You can easily adjust the snippet by yourself:

    Contact - stands for the target entity you would like to check.
    phoneNumber - stands for the field of this entity you would like to check. The name of the field you can find by following the route on the screenshot attached:

    Click image for larger version  Name:	image.png Views:	0 Size:	17.8 KB ID:	121743
    Last edited by heint; Today, 11:17 AM.

    Comment


    • oraculo1
      oraculo1 commented
      Editing a comment
      heint, where can i found this code? And if i want to make the same for other fields, how can i make it? Thanks for your patience.
  • heint
    Junior Member
    • Jun 2025
    • 28

    #5
    oraculo1,

    Do you want to additionally add the duplicate checking for more fields of Contact entity so that the formula will check for phone field duplicates and some other fields (email, for example) as well? Please, correct me if I'm wrong.

    Comment

    Working...