RotherOSS / otobo

OTOBO is one of the most flexible web-based ticketing systems used for Customer Service, Help Desk, IT Service Management. https://otobo.io/
GNU General Public License v3.0
263 stars 75 forks source link

DynamicField Reference Agent: Empty value doesn't work #3719

Open StefanAbel-OTOBO opened 3 months ago

StefanAbel-OTOBO commented 3 months ago

When you create a Dynamic Field of type (Reference) Agent the Empty value option doesn't work at all, which causes the alphabetically first agent to be selected automatically.

I reproduced ths on ov270

This is my DF configuration: image

This is the result (it looks the same e.g. in a Process Activity Dialog or in AgentTicketPhone): image

stefanhaerter commented 1 month ago

Related: https://github.com/RotherOSS/ITSMConfigurationManagement/issues/19

stefanhaerter commented 1 month ago

I managed to track the root bug down. There are a number of things which come into play to create the environment, but in the end it boils down to:

Double quote precedes minus precedes alphanumeric character

in regard to perl sort. Please feel free to ask me for further details, but I didn't want to clutter this comment.

I see two possible ways to solve the root cause:

sub BuildSelection {
    [...]
    if ( !defined $Param{SelectedID} && !defined $Param{SelectedValue} ) {
        $Param{SelectedID} = '';
    }
    [...]
}
stefanhaerter commented 1 month ago

For the interested reader, the place where the mentioned sorting takes place is here:

https://github.com/RotherOSS/otobo/blob/9670cb1d6814a847a241626a5428cf75291adc08/Kernel/Output/HTML/Layout.pm#L5920-L5926

svenoe commented 1 month ago

Is reproducible with a standard dropdown dynamic field including "-" as value. (Probably just " suffices.) See probably: https://github.com/RotherOSS/otobo/blob/fecfc99615e486d34aa887cd180c7b4317a2e175/var/httpd/htdocs/js/Core.UI.InputFields.js#L568 which, if no option is selected returns the first option, and line 594 where an empty key is skipped. Problem arises when stuff (") is sorted in front of the empty value (-). (See also backend sorting above.)

MichaelThumes commented 1 month ago

steps to reproduce:

Expected Behaviour:

DF dropdown has empty value pre-selected

Observed Behaviour:

DF dropdown has one of the weird values from above pre-selected , eg '"-"'

root cause

Combination of the backend sorting and the way the frontend handles selection (see above comments)

possible approaches

MichaelThumes commented 1 month ago

option a) patch the backend Kernel/Output/HTML/Layout.pm

diff --git a/Kernel/Output/HTML/Layout.pm b/Kernel/Output/HTML/Layout.pm
index 86b163855..ae3a6ba35 100644
--- a/Kernel/Output/HTML/Layout.pm
+++ b/Kernel/Output/HTML/Layout.pm
@@ -5919,6 +5919,9 @@ sub _BuildSelectionDataRefCreate {
         }
         else {
             @SortKeys = sort {
+                # assure the empty option, if any, is always sorted first                
+                if($a eq '' ) { return -1; }
+                if($b eq '' ) { return  1; }
                 lc( $DataLocal->{$a} // '' )
                     cmp lc( $DataLocal->{$b} // '' )
             } ( keys %{$DataLocal} );

That seems to work but adds 2 condition checks in the inner sort loop., albeit cheap ones.

Alternative is less condition checks but more typing:

diff --git a/Kernel/Output/HTML/Layout.pm b/Kernel/Output/HTML/Layout.pm
index 86b163855..fa7d013cd 100644
--- a/Kernel/Output/HTML/Layout.pm
+++ b/Kernel/Output/HTML/Layout.pm
@@ -5918,10 +5918,15 @@ sub _BuildSelectionDataRefCreate {
             # already done before the translation
         }
         else {
+            my $EmptyValue = delete $DataLocal->{''};
             @SortKeys = sort {
                 lc( $DataLocal->{$a} // '' )
                     cmp lc( $DataLocal->{$b} // '' )
             } ( keys %{$DataLocal} );
+            if( defined $EmptyValue) {
+                $DataLocal->{''} = $EmptyValue;
+                unshift @SortKeys, '';
+            }
             $OptionRef->{Sort} = 'AlphanumericValue';
         }

option b) - hack the frontend Core.UI.InputFields.js

diff --git a/var/httpd/htdocs/js/Core.UI.InputFields.js b/var/httpd/htdocs/js/Core.UI.InputFields.js
index 431745e85..7273b591e 100644
--- a/var/httpd/htdocs/js/Core.UI.InputFields.js
+++ b/var/httpd/htdocs/js/Core.UI.InputFields.js
@@ -565,7 +565,7 @@ Core.UI.InputFields = (function (TargetNS) {
                 Selection = $.unique($SelectObj.val());
                 SelectionLength = Selection.length;
             } else {
-                Selection = [ $SelectObj.val() ];
+                Selection = [ PossibleNone ? '||-' : $SelectObj.val() ];
                 SelectionLength = 1;
             }