From 482b8e2d0d1ff4d3c51ae302609da47d2ffc0256 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Fri, 17 Dec 2021 09:05:51 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20bug=20#5:=20Error=20when?= =?UTF-8?q?=20altering=20any=20case=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CRM/TwingleCampaign/BAO/CustomField.php | 31 ++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/CRM/TwingleCampaign/BAO/CustomField.php b/CRM/TwingleCampaign/BAO/CustomField.php index 04324f3..a0a9b81 100644 --- a/CRM/TwingleCampaign/BAO/CustomField.php +++ b/CRM/TwingleCampaign/BAO/CustomField.php @@ -205,15 +205,40 @@ class CRM_TwingleCampaign_BAO_CustomField { try { foreach ($options as $key => $value) { - $result[] = civicrm_api3( + + $option_value_exists = civicrm_api3( 'OptionValue', - 'create', + 'get', [ + 'sequential' => 1, 'option_group_id' => $option_group_id, 'value' => $key, - 'label' => $value, ] ); + + // If the option value does not yet exist, create it + if ($option_value_exists['count'] == 0) { + $result[] = civicrm_api3( + 'OptionValue', + 'create', + [ + 'option_group_id' => $option_group_id, + 'value' => $key, + 'label' => $value, + ] + ); + } + // If the option value already exist, update it + else { + $result[] = civicrm_api3( + 'OptionValue', + 'create', + [ + 'id' => $option_value_exists['values'][0]['id'], + 'label' => $value, + ] + ); + } } } catch (CiviCRM_API3_Exception $e) { $errorMessage = $e->getMessage();