Announcement

Collapse
No announcement yet.

Checking for Duplicate records

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Checking for Duplicate records

    Hi,

    When we enter that record twice for entity type "Comany" and "Person", the system checks and alert a warning that the record is duplicate (see screencap1 attached). This does not happen with any other entity types. How can i enable the duplicate check for such entities?

    I know this may have been resolved before i am unable to locate it because of the search phrase. Assistance is most appreciated.
    Attached Files

  • #2
    Hi,
    please check this thread https://forum.espocrm.com/forum/gene...icate-checking.

    Comment


    • #3
      Hi Maximus ,

      Honestly, i am not a fully familiar with various aspects of the codes in Espocrm but I went through the forum link. There was so much variations but none address my problem. The entity we defined is a custom entity of the type "base". I used one of the examples but it gave me error 500 when i tested. This is what i did:

      The original code in custom/espo/services/Billrecord.php:


      Code:
      <?php
      
      namespace Espo\Custom\Services;
      
      class Billrecord extends \Espo\Core\Templates\Services\BasePlus
      {
      }
      Change to :

      Code:
      <?php
      namespace Espo\Custom\Services;
      
      use \Espo\ORM\Entity as Entity;
      
      class Lead extends \Espo\Modules\Crm\Services\Lead
      {
      protected function getDuplicateWhereClause(Entity $entity, $data)
      {
      return array(
      'AND' => array(
      array(
      'name' => $entity->get('name'),
      ),
      )
      );
      }
      }
      Last edited by murugappan; 07-24-2020, 08:02 AM.

      Comment


      • esforim
        esforim commented
        Editing a comment
        You can do it murugappan! I made a post in last thread but haven't gotten any confirmation so I left it in the dust corner. Hopefully your success can help out.

    • #4
      In the file /custom/Espo/Custom/Services/Billrecord.php you need to have this code:
      PHP Code:
      <?php

      namespace Espo\Custom\Services;

      use 
      \Espo\ORM\Entity;

      class 
      Billrecord extends \Espo\Core\Templates\Services\BasePlus
      {
          protected function 
      getDuplicateWhereClause(Entity $entity$data)
          {
              if (!
      $entity->get('name')) {
                  return 
      false;
              }
              return [
                  
      'name' => $entity->get('name')
              ];
          }
      }

      Comment


      • #5
        Hi Maximus ,

        The code you posted worked for 1 field(?). So i modified the code to include another field. The following is the code but it didnt work. Gives me "bad server response" error:

        Code:
        <?php
        
        namespace Espo\Custom\Services;
        
        use \Espo\ORM\Entity;
        
        class Billrecord extends \Espo\Core\Templates\Services\BasePlus
        {
        protected function getDuplicateWhereClause(Entity $entity, $data)
        {
        if (!$entity->get('name') && (!$entity->get('claimnumber')) {
        return false;
        }
        return [
        'name' => $entity->get('name'),
        'claimnumber' => $entity->get('claimnumber'),
        
        ];
        }
        }
        2. I could not understand the symbols. The 'name' symbol is that the db name or entity name?

        Appreciate your help
        Last edited by murugappan; 07-24-2020, 03:09 PM.

        Comment


        • #6
          Hi murugappan
          I could not understand the symbols. The 'name' symbol is that the db name or entity name?
          My understanding is that 'name' refers to the field 'name' of the Billrecord record, so the filter is saying:

          "For an entity to be considered a duplicate, the value of the field 'name' of an existing record is equal to the value of the new entity's 'name' attribute"

          In your filter it would add the condition that the value of the exiting record's 'claimnumber' field be equal to the value of the 'claimnumber' attribute in the new entity.

          I suggest that you check the back end log (data/logs/) to see what kind of error is being triggered (Error 500 is just a generic flag) and an indication of what function in what script and line number is throwing the error to be able to further investigate.

          Comment


          • murugappan
            murugappan commented
            Editing a comment
            Hi, the code that i submitted, is that correct way to check for using 2 fields to check for duplicate?

          • telecastg
            telecastg commented
            Editing a comment
            I think that you have to remove the last "comma" in the return array like this:

            return [
            'name' => $entity->get('name'),
            'claimnumber' => $entity->get('claimnumber')
            ];

            Otherwise the "comma" after "('claimnumber')" indicates that array has a "third" element which is NULL and that is probably throwing off Espo, but if this doesn't help, check the back-end log to learn more about the error.

            If you are not familiar with PHP troubleshooting you can check this post https://forum.espocrm.com/forum/deve...gging-php-code

        • #7
          UPDATE: Solved by Maximus, please see: https://forum.espocrm.com/forum/gene...king#post68196



          Hi murugappan ,

          Did you manage to get the duplicate checker to work?

          I made a post in another thread but I think your thread but be more relevant, I can't get pass the error 500. I notice that your code you have
          class Billrecord, Billrecord being your entity name?
          and then you got
          Services\BasePlus being your entity type (Base Plus type). Why is that? In the example they both use class Lead and Services\Lead.

          Other thread: https://forum.espocrm.com/forum/gene...icate-checking

          Code:
          <?php
          
          namespace Espo\Custom\Services;
          
          use Espo\ORM\Entity;
          
          class RealEstateProperty extends \Espo\Modules\Crm\Services\RealEstateProperty
          {
          protected $checkForDuplicatesInUpdate = false; // set true to enable for update
          
          // copied original method
          protected function getDuplicateWhereClause(Entity $entity, $data)
          {
          $whereClause = [
          'OR' => []
          ];
          $toCheck = false;
          if ($entity->get('addressStreet') || $entity->get('addressCity')) {
          $part = [];
          $part['addressStreet'] = $entity->get('addressStreet');
          $part['addressCity'] = $entity->get('addressCity');
          $whereClause['OR'][] = $part;
          $toCheck = true;
          }
          if (
          ($entity->get('emailAddress') || $entity->get('emailAddressData'))
          &&
          (
          $entity->isNew() ||
          $entity->isAttributeChanged('emailAddress') ||
          $entity->isAttributeChanged('emailAddressData')
          )
          ) {
          if ($entity->get('emailAddress')) {
          $list = [$entity->get('emailAddress')];
          }
          if ($entity->get('emailAddressData')) {
          foreach ($entity->get('emailAddressData') as $row) {
          if (!in_array($row->emailAddress, $list)) {
          $list[] = $row->emailAddress;
          }
          }
          }
          foreach ($list as $emailAddress) {
          $whereClause['OR'][] = [
          'emailAddress' => $emailAddress
          ];
          $toCheck = true;
          }
          }
          if (!$toCheck) {
          return false;
          }
          return $whereClause;
          }
          }
          Last edited by esforim; 03-02-2021, 12:27 AM.

          Comment

        Working...
        X