Contacts with Same phone number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fioreid
    Junior Member
    • Oct 2022
    • 16

    Contacts with Same phone number

    How to avoid creating a contact if there is another contact saved with the same number but similar/different name?
  • lazovic
    Super Moderator
    • Jan 2022
    • 809

    #2
    Hi fioreid,

    I think this solution will suit you (requires some programming skills): https://docs.espocrm.com/development/duplicate-check/.

    Comment

    • fioreid
      Junior Member
      • Oct 2022
      • 16

      #3
      I would prefer using a workflow, there is a formula record\exists('Contact', 'phoneNumber=', phoneNumber). Is this the right for me? I don't quite understand it.

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #4
        You need to create a custom contact duplicateWhereBuilders class:

        1- First step create a file Contact.php under custom/Espo/Custom/Classes/DuplicateWhereBuilders :
        Code:
        <?php
        
        namespace Espo\Custom\Classes\DuplicateWhereBuilders;
        
        use Espo\Core\ORM\Entity as CoreEntity;
        
        use Espo\Core\{
            Duplicate\WhereBuilder,
            Field\EmailAddressGroup,
            Field\PhoneNumberGroup,
        };
        
        use Espo\ORM\{
            Query\Part\Condition as Cond,
            Query\Part\WhereItem,
            Query\Part\Where\OrGroup,
            Entity,
        };
        
        /**
         * @implements WhereBuilder<CoreEntity>
         */
        class Contact implements WhereBuilder
        {
            public function build(Entity $entity): ?WhereItem
            {
                assert($entity instanceof CoreEntity);
        
                $orBuilder = OrGroup::createBuilder();
        
                $toCheck = false;
        
                if ($entity->get('firstName') || $entity->get('lastName')) {
                    $orBuilder->add(
                        Cond::and(
                            Cond::equal(
                                Cond::column('firstName'),
                                $entity->get('firstName')
                            ),
                            Cond::equal(
                                Cond::column('lastName'),
                                $entity->get('lastName')
                            )
                        )
                    );
        
                    $toCheck = true;
                }
        
                if (
                    ($entity->get('emailAddress') || $entity->get('emailAddressData')) &&
                    (
                        $entity->isNew() ||
                        $entity->isAttributeChanged('emailAddress') ||
                        $entity->isAttributeChanged('emailAddressData')
                    )
                ) {
                    foreach ($this->getEmailAddressList($entity) as $emailAddress) {
                        $orBuilder->add(
                            Cond::equal(
                                Cond::column('emailAddress'),
                                $emailAddress
                            )
                        );
        
                        $toCheck = true;
                    }
                }
        
                if (
                    ($entity->get('phoneNumber') || $entity->get('phoneNumberData')) &&
                    (
                        $entity->isNew() ||
                        $entity->isAttributeChanged('phoneNumber') ||
                        $entity->isAttributeChanged('phoneNumberData')
                    )
                ) {
                    foreach ($this->getPhoneNumberList($entity) as $phoneNumber) {
                        $orBuilder->add(
                            Cond::equal(
                                Cond::column('phoneNumber'),
                                $phoneNumber
                            )
                        );
        
                        $toCheck = true;
                    }
                }
        
                if (!$toCheck) {
                    return null;
                }
        
                return $orBuilder->build();
            }
        
            /**
             * @return string[]
             */
            private function getEmailAddressList(CoreEntity $entity): array
            {
                if ($entity->get('emailAddressData')) {
                    /** @var EmailAddressGroup $eaGroup */
                    $eaGroup = $entity->getValueObject('emailAddress');
        
                    return $eaGroup->getAddressList();
                }
        
                if ($entity->get('emailAddress')) {
                    return [
                        $entity->get('emailAddress')
                    ];
                }
        
                return [];
            }
        
            private function getPhoneNumberList(CoreEntity $entity): array
            {
                if ($entity->get('phoneNumberData')) {
                    /** @var PhoneNumberGroup $eaGroup */
                    $eaGroup = $entity->getValueObject('phoneNumber');
        
                    return $eaGroup->getNumberList();
                }
        
                if ($entity->get('phoneNumber')) {
                    return [
                        $entity->get('phoneNumber')
                    ];
                }
        
                return [];
            }
        }

        2 - second step create if not exists custom/Espo/Custom/Resources/metadata/recordDefs/Contact.json :
        Code:
        { "duplicateWhereBuilderClassName": "Espo\\Custom\\Classes\\DuplicateWhereBuilders\\Contact" }
        Rabii
        Web Dev

        Comment


        • item
          item commented
          Editing a comment
          nice rabii,

          very usefull
      • fioreid
        Junior Member
        • Oct 2022
        • 16

        #5
        rabii ok I will try thank you.

        Comment


        • rabii
          rabii commented
          Editing a comment
          you are welcome. This should do the job i have tested it and it works, when creating a new contact the system will check if any existing contact has a phone number similar to the new then a modal windows with message (The record you are creating might already exist) will show up with existing contact.
      Working...