From 56e50a8b6c3e1240d78e3cb878c304f0d3011559 Mon Sep 17 00:00:00 2001 From: Aidan Saunders Date: Mon, 10 Jun 2019 13:42:13 +0100 Subject: [PATCH] When syncing to UF record, use API instead of DAO to create email - with the addition of a default location in this change, this fixes core#1026 Bonus code tidy: if UF email matches the contact primary email, avoid unnecessary update. If update is needed, use API not direct SQL. --- CRM/Core/BAO/UFMatch.php | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/CRM/Core/BAO/UFMatch.php b/CRM/Core/BAO/UFMatch.php index 18ce81f8aa..cab931e41b 100644 --- a/CRM/Core/BAO/UFMatch.php +++ b/CRM/Core/BAO/UFMatch.php @@ -431,25 +431,23 @@ AND domain_id = %4 $contactDetails = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactId); if (trim($contactDetails[1])) { + //update if record is found but different $emailID = $contactDetails[3]; - //update if record is found - $query = "UPDATE civicrm_email - SET email = %1 - WHERE id = %2"; - $p = [ - 1 => [$emailAddress, 'String'], - 2 => [$emailID, 'Integer'], - ]; - $dao = CRM_Core_DAO::executeQuery($query, $p); + if (trim($contactDetails[1]) != $emailAddress) { + civicrm_api3('Email', 'create', [ + 'id' => $emailID, + 'email' => $emailAddress, + ]); + } } else { //else insert a new email record - $email = new CRM_Core_DAO_Email(); - $email->contact_id = $contactId; - $email->is_primary = 1; - $email->email = $emailAddress; - $email->save(); - $emailID = $email->id; + $result = civicrm_api3('Email', 'create', [ + 'contact_id' => $contactId, + 'email' => $emailAddress, + 'is_primary' => 1, + ]); + $emailID = $result->id; } CRM_Core_BAO_Log::register($contactId, -- 2.25.1