API duplicate check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sobolevfff
    Member
    • Mar 2020
    • 81

    API duplicate check

    Please help me to solve the problem with duplicates when receiving data via API.
    I have an entity TradingAccount that has a unique key tradingAccountID. When changing data in an external source, sometimes a change comes to the CRM, and sometimes a new record.

    I wrote to ignore duplicates in the file, but it didn't help.
    in custom/Espo/Custom/Resources/metadata/entityDefs/TradingAccount.json

    "tradingAccountID": {
    "type": "int",
    "tooltip": true,
    "duplicateIgnore":true,
    "isCustom": true



    I tried to create a php file to check for a duplicate, as in this example, but I did not succeed, since in the example 2 keys are checked, and I have one, I just could not transform it to fit my needs and got a bunch of errors.
  • sobolevfff
    Member
    • Mar 2020
    • 81

    #2
    I created such a file, but now updates come in, but then they do not come.

    <?php

    namespace Espo\Custom\Services;

    use Espo\ORM\Entity;

    class CheckDuplicate extends \Espo\Modules\Crm\Services\TradingAccount
    {
    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('tradingAccountID')) {
    $toCheck = true;
    $whereClause['OR']['tradingAccountID'] = $entity->get('tradingAccountID');
    }

    if (!$toCheck) return null;

    return $whereClause;
    }
    }
    Last edited by sobolevfff; 03-24-2021, 11:26 AM.

    Comment

  • sobolevfff
    Member
    • Mar 2020
    • 81

    #3
    The problem with duplicates was resolved, but another problem with updates appeared. The updates come only for the last record. If a record was created earlier (the penultimate and later) and there were changes in the external source, then it sends them as a new record and the CRM ignores it along with the updates. Changes come only for the most recent entry. Help solve this problem please
    Last edited by sobolevfff; 03-24-2021, 11:02 AM.

    Comment

    • sobolevfff
      Member
      • Mar 2020
      • 81

      #4
      If I change this parameter to true or false, then nothing changes.
      protected $checkForDuplicatesInUpdate = false;


      Maybe someone can use something from this file for the updates I need?
      EspoCRM – Open Source CRM Application. Contribute to espocrm/espocrm development by creating an account on GitHub.
      Last edited by sobolevfff; 03-24-2021, 11:32 AM.

      Comment

      • sobolevfff
        Member
        • Mar 2020
        • 81

        #5
        item @yuri @telecastg Help me PLEASE!
        I have such a problem with all entities that receive data via API from an external data source. There is a unique ID by which the record can be identified and many different fields that need to be updated.
        Last edited by sobolevfff; 03-24-2021, 04:19 PM.

        Comment

        • sobolevfff
          Member
          • Mar 2020
          • 81

          #6
          Updating needs to be done for many values. I'm sure it can be done nicely for an array, but I don't know how. I tried to insert update one value at a time for balance if the key is repeated, but it didn't help. All the same, only the last record is updated.
          @item @yuri @telecastg Help me PLEASE!

          <?php
          namespace Espo\Custom\Services;
          use Espo\ORM\Entity;
          class TradingAccount extends \Espo\Core\Templates\Services\BasePlus
          {
          // copied original method
          protected function getDuplicateWhereClause(Entity $entity, $data)
          {
          $whereClause = [
          'OR' => []
          ];
          $toCheck = false;
          if ($entity->get('tradingAccountID')) {
          $toCheck = true;
          $whereClause['OR']['tradingAccountID'] = $entity->get('tradingAccountID');
          }
          if (!$toCheck) {
          $entityManager = $this->getEntityManager();
          $tadingAccountID = $entity->get('tradingAccountID');
          $tradingAccountBalance = $entity->get('tradingAccountBalance');
          $tadingAccountFirst = $entityManager
          ->getRepository('TradingAccount')
          ->where([
          'tradingAccountID' => $tadingAccountID,
          ])
          ->findOne();
          if($entity->isNew() && $tadingAccountFirst) {
          $entityManager
          ->getRepository('TradingAccount')
          ->updateColumns($tadingAccountFirst, [
          'tradingAccountBalance' => $tradingAccountBalance, // update at the first entity
          ]);
          }
          }
          return $whereClause;
          }
          }
          Last edited by sobolevfff; 03-24-2021, 06:39 PM.

          Comment


          • esforim
            esforim commented
            Editing a comment
            I understand the frustration of trying to get it to work, but I don't recommend multiple @mention within a short few hours is the right way to go about requesting for help. I do appreciate your documentations though.
        • sobolevfff
          Member
          • Mar 2020
          • 81

          #7
          I also created a hook with beforeSave, but it didn't work either.


          <?php
          namespace Espo\Custom\Hooks\TradingAccount;

          use Espo\ORM\Entity;

          class CheckDuplicateTradingAccount extends \Espo\Core\Hooks\Base
          {
          public static $order = 1;
          public function beforeSave(Entity $entity, array $options = array())
          {
          $entityManager = $this->getEntityManager();
          $tadingAccountID = $entity->get('tradingAccountID');
          $tradingAccountBalance = $entity->get('tradingAccountBalance');
          $tadingAccountFirst = $entityManager
          ->getRepository('TradingAccount')
          ->where([
          'tradingAccountID' => $tadingAccountID,
          ])
          ->findOne();

          if($entity->isNew() && $tadingAccountFirst) {
          $entityManager
          ->getRepository('TradingAccount')
          ->updateColumns($tadingAccountFirst, [
          'tradingAccountBalance' => $tradingAccountBalance,
          ]);
          }
          }
          }

          Comment

          • sobolevfff
            Member
            • Mar 2020
            • 81

            #8
            Well, really no one knows how to solve this problem? Please help, I really need to fix it!







            Comment

            • sobolevfff
              Member
              • Mar 2020
              • 81

              #9
              Someone, please help!

              Comment

              Working...