Skip to content

Changes the Action: Contact: Get By Email* to search any email address instead of just primary email and fixes issue causing new duplicate contacts to be created if the contact being searched for returns more than 1 result

Changes the Action: Contact: Get By Email* to search any email address instead of just primary email, relates to #27 (closed)

And fixes issue causing new duplicate contacts to be created if the contact being searched for returns more than 1 result.

This code incorrectly assumes that there are no duplicate contacts in the CRM. D'oh!

When the action executes, if there is more than 1 contact is returned then an error is raised.

{

    "count": 2,
    "is_error": 1,
    "error_message": "Expected one Contact but found 2"
}

And in response to the error, a new contact is created, a duplicate contact.

protected function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
    $contactType = ContactActionUtils::getContactType($this->configuration->getParameter('contact_type'));
		try {
			$params['email'] = $parameters->getParameter('email');
      $params['contact_type'] = $contactType['contact_type']['name'];
      if ($contactType['contact_sub_type']) {
        $params['contact_sub_type'] = $contactType['contact_sub_type']['name'];
      }
			$params['return'] = 'id';
			$contactId = civicrm_api3('Contact', 'getvalue', $params);
		} catch (\Exception $e) {
      $createParams['email'] = $parameters->getParameter('email');
      $createParams['first_name'] = $parameters->getParameter('first_name');
      $createParams['last_name'] = $parameters->getParameter('last_name');
      $createParams['contact_type'] = $contactType['contact_type']['name'];
      if ($contactType['contact_sub_type']) {
        $createParams['contact_sub_type'] = $contactType['contact_sub_type']['name'];
      }
			$result = civicrm_api3('Contact', 'create', $createParams);
			$contactId = $result['id'];
		}

		$output->setParameter('contact_id', $contactId);
	}

What should happen is that the results should be limited to 1 contact and the oldest (lowest) contact ID used. That way, the oldest contact is always updated if there are duplicates and no new duplicate contacts are created.

This PR includes fixes for the other contact find/create actions which had the same issue.

Also standardised variable names for params and contact id whilst reviewing these actions, minor code clean-up. Makes it easier to find bugs if the code looks the same.

Agileware Ref: CIVICRM-2077 CIVICRM-2078

Edited by justinfreeman (Agileware)

Merge request reports

Loading