How to avoid creating a contact if there is another contact saved with the same number but similar/different name?
Announcement
Collapse
No announcement yet.
Contacts with Same phone number
Collapse
X
-
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
- Likes 3
Comment
-
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.
-
Comment