Duplicate checking fields - V8.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wdbruwer
    Junior Member
    • Jan 2023
    • 10

    Duplicate checking fields - V8.0

    I have upgraded to V8.0.0.0 today as I saw the duplicate check is now available. I have selected the fields that should be checked for duplicate, however I seem to be still missing something as I can stil create duplicate entries.

    To give a background, I am using the default name field as my serial number field, as such this needs to be a unique field and can't be duplicate.

    Is there something I need to change in the backend to prevent users from being able to update/create duplicate entries?
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    It will only do a Duplicate Check, it won't prevent from creating duplicate. You can use Before Save Formula to deny all duplicate creation

    Comment

    • wdbruwer
      Junior Member
      • Jan 2023
      • 10

      #3
      Originally posted by espcrm
      It will only do a Duplicate Check, it won't prevent from creating duplicate. You can use Before Save Formula to deny all duplicate creation
      Understood thanks, sadly that is outside my scope of understanding in the world of code & Scripting, So I'm going to ask the most annoying question, how do I do that?

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #4
        you will a code like this, add it to your entity administration > Entity Manager > Your Entity > Formula > API Before Save Script and use the code below

        PHP Code:
        if (!recordService\skipDuplicateCheck()) {
            $id = record\findOne('Account', null, null, 'name=', name);
        
            if ($id) {
                recordService\throwDuplicateConflict($id);
            }
        }
        Rabii
        Web Dev

        Comment

        • wdbruwer
          Junior Member
          • Jan 2023
          • 10

          #5
          Originally posted by rabii
          you will a code like this, add it to your entity administration > Entity Manager > Your Entity > Formula > API Before Save Script and use the code below

          PHP Code:
          if (!recordService\skipDuplicateCheck()) {
          $id = record\findOne('Account', null, null, 'name=', name);
          
          if ($id) {
          recordService\throwDuplicateConflict($id);
          }
          }
          You are a star, thank you so much.

          Comment

          • rabii
            Active Community Member
            • Jun 2016
            • 1250

            #6
            you are welcome Make sure to change Account with your entity. if you face any issues let me know
            Rabii
            Web Dev

            Comment

            • yuri
              Member
              • Mar 2014
              • 8440

              #7
              This should work w/o the script. The same logic.
              If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

              Comment

              • yuri
                Member
                • Mar 2014
                • 8440

                #8
                To forbid bypass duplicate checking, one can use

                Code:
                if (recordService\skipDuplicateCheck()) {
                    recordService\throwForbidden("No duplicate check bypass allowed.");
                }
                If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

                Comment

                • wdbruwer
                  Junior Member
                  • Jan 2023
                  • 10

                  #9
                  Originally posted by rabii
                  you are welcome Make sure to change Account with your entity. if you face any issues let me know
                  Worked perfectly, changed entity & works for 2 fields that I need to keep unique. I get the pop up warning for duplicate & shows the duplicate.

                  Comment

                  • rabii
                    Active Community Member
                    • Jun 2016
                    • 1250

                    #10
                    But if you want to force the user to not create the entity when a duplicate is found, you need to add the code mentioned by Yuri above

                    PHP Code:
                    if (recordService\skipDuplicateCheck()) {
                    recordService\throwForbidden("No duplicate check bypass allowed.");
                    }
                    Meaning when system find a duplicate if the user clicks on create they will get the error (No duplicate bypass allowed) and this will stop them from creating duplicated record.
                    Rabii
                    Web Dev

                    Comment

                    • wdbruwer
                      Junior Member
                      • Jan 2023
                      • 10

                      #11
                      Originally posted by rabii
                      But if you want to force the user to not create the entity when a duplicate is found, you need to add the code mentioned by Yuri above

                      PHP Code:
                      if (recordService\skipDuplicateCheck()) {
                      recordService\throwForbidden("No duplicate check bypass allowed.");
                      }
                      Meaning when system find a duplicate if the user clicks on create they will get the error (No duplicate bypass allowed) and this will stop them from creating duplicated record.
                      Thanks, I will keep that on record, for now we happy with the pop up showing the duplicate. If I notice users ignoring that I will implement.

                      Comment

                      • shalmaxb
                        Senior Member
                        • Mar 2015
                        • 1602

                        #12
                        Hi, until now I used a custom duplicate check, which would be obsolete with the new function. With the above script it works.

                        Will I have to delete the scripts I used before version 8?

                        And is it possible to have a duplicate check depending on two fields at once?

                        I have an entity, where I organize exhibitions. Every exhibition has items, which are numbered from 1 to n. So one exhibition may have item 1 to 10, the other one 1 to 15. As it is checked now, I get a duplicate warning, when I place the number 1 in a new exhibition, when there is already a number 1 in a former exhibition.
                        So it would be necessary to have something like: If in exhibition X there is a number 1, it cannot have another number 1 (duplicate check warning). If in exhibition X there is a number 1, it may have a number 1 in exhibition Y (no duplicate check warning).
                        Last edited by shalmaxb; 10-22-2023, 02:22 PM.

                        Comment


                        • rabii
                          rabii commented
                          Editing a comment
                          AFAK you should be able to keep your custom duplicatecheck class as it is and should work even with latest version v8 and above, i am using a custom duplicate check for few entities in different projects and it works fine. here is a discussion with yuri about it https://github.com/espocrm/espocrm/i...ent-1628924603
                      • shalmaxb
                        Senior Member
                        • Mar 2015
                        • 1602

                        #13
                        thanks rabii, I see (the linked discussion sheds more light on this function). Unfortunately the built in function is a bit limited in the fields, that can be used and creating more complex conditions (at least for me).
                        I will see, if I can build in the needed functionality into my custom duplicate checkers.

                        Comment


                        • rabii
                          rabii commented
                          Editing a comment
                          the new function is a plus for a non technical users. they can just choose the fields and the crm will map them in an Or statement. However the system allows you to always create your own custom checker as it won't be possible to cover all use cases with the introduced function. If you face any issues we are here to help mate
                      • shalmaxb
                        Senior Member
                        • Mar 2015
                        • 1602

                        #14
                        I got my custom duplicate check working, but only on update of the respective field. On new record it does not work.

                        Here is my code:

                        Code:
                        <?php
                        namespace Espo\Custom\Classes\DuplicateWhereBuilders;
                        
                        use Espo\Core\Duplicate\WhereBuilder;
                        
                        use Espo\ORM\Query\Part\Condition as Cond;
                        use Espo\ORM\Query\Part\WhereItem;
                        use Espo\ORM\Query\Part\Where\OrGroup;
                        use Espo\ORM\Entity;
                        
                        class WerkeAusstellungen implements WhereBuilder
                        {
                        public function build(Entity $entity): ?WhereItem
                        {
                        $orBuilder = OrGroup::createBuilder();
                        
                        $toCheck = false;
                        
                        if ($entity->get('ausstellungsnummer') || $entity->get('vernissage')) {
                        $orBuilder->add(
                        Cond::and(
                        Cond::equal(
                        Cond::column('ausstellungsnummer'),
                        $entity->get('ausstellungsnummer')
                        ),
                        Cond::equal(
                        Cond::column('vernissage'),
                        $entity->get('vernissage')
                        )
                        )
                        );
                        
                        $toCheck = true;
                        }
                        
                        return $orBuilder->build();
                        }
                        }


                        The entity (and class) is WerkeAusstellungen, what means ArtworkExhibition. Every exhibition has a number of artworks, which are sequencial numbered from 1 to n. This way it is for every exhibiton.
                        In the exhibition I want to duplicate check the already manual entered numbers (varchar field), so that every number in one exhibition can appear only once
                        In the code "ausstellungsnummer" is that number, "vernissage" is a date field for the opening of the exhibition, which gives the condition, that the numer is for one certain exhibition.
                        When I try to update a number in a given list with an already existing number, it throws the warning for duplicate, displaying the other record with that number. Hence, it works as it should.

                        Though, when I create a new record it does not have any effect and so it is possible to enter an already existing number again.

                        Where and how will I have to declare a function to work for new records as well?

                        Comment

                        • rabii
                          Active Community Member
                          • Jun 2016
                          • 1250

                          #15
                          It seems that it is not working because you have an AND operator there, which means that both conditions should happen to apply the duplicate check (ausstellungsnummer && vernissage) meaning that there should an existing record that has same value provided for both fields. Not sure if this is what you mean because it is confusing that you have an OR condition on your if statement but you apply an AND expression on the result. when creating a new record do you need the new exhibition to have a unique (ausstellungsnummer) AND a unique (vernissage) ?
                          Rabii
                          Web Dev

                          Comment

                          Working...