if ($address->id) {
// first get custom field from master address if any
if (isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) {
- $address->copyCustomFields($params['master_id'], $address->id);
+ $address->copyCustomFields($params['master_id'], $address->id, $hook);
}
if (isset($params['custom'])) {
if (!empty($customValue['id'])) {
$cvParam['id'] = $customValue['id'];
}
+ elseif (empty($cvParam['is_multiple']) && !empty($entityID)) {
+ // dev/core#3000 Ensure that if we are not dealing with multiple record custom data and for some reason have got here without getting the id of the record in the custom table for this entityId let us give it one last shot
+ $rowId = CRM_Core_DAO::singleValueQuery("SELECT id FROM {$cvParam['table_name']} WHERE entity_id = %1", [1 => [$entityID, 'Integer']]);
+ if (!empty($rowId)) {
+ $cvParam['id'] = $rowId;
+ }
+ }
if (!array_key_exists($customValue['table_name'], $cvParams)) {
$cvParams[$customValue['table_name']] = [];
}
$this->assertNotContains('Alabama', $result['values']);
}
+ public function testUpdateSharedAddressWithCustomFields() {
+ $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
+
+ $params = $this->_params;
+ $params['custom_' . $ids['custom_field_id']] = "custom string";
+
+ $firstAddress = $this->callAPISuccess($this->_entity, 'create', $params);
+
+ $contactIdB = $this->individualCreate();
+
+ $secondAddressParams = array_merge(['contact_id' => $contactIdB, 'master_id' => $firstAddress['id']], $firstAddress);
+ unset($secondAddressParams['id']);
+ $secondAddress = $this->callAPISuccess('Address', 'create', $secondAddressParams);
+ // Ensure an update to the second address doesn't cause a "db error: already exists" when resaving the custom fields.
+ $this->callAPISuccess('Address', 'create', ['id' => $secondAddress['id'], 'contact_id' => $contactIdB, 'master_id' => $firstAddress['id']]);
+ }
+
}