-my ($CustomField, $Value);
-while( my($k, $v) = each %ARGS ) {
- next unless $k =~ /^Object-.*?-\d*-CustomField-(\d+)-Values?$/;
- ($CustomField, $Value) = ($1, $v);
- last;
+# Only autocomplete the last value
+my $term = (split /\n/, $ARGS{term} || '')[-1];
+
+my $abort = sub {
+ $r->content_type('application/json; charset=utf-8');
+ $m->out(JSON( [] ));
+ $m->abort;
+};
+
+unless ( exists $ARGS{ContextType} and exists $ARGS{ContextId} ) {
+ RT->Logger->debug("No context provided");
+ $abort->();
+}
+
+# Use _ParseObjectCustomFieldArgs to avoid duplicating the regex.
+# See the docs for _ParseObjectCustomFieldArgs for details on the data
+# structure returned. There will be only one CF, so drill down 2 layers
+# to get the cf id, if one is there.
+
+my %custom_fields = _ParseObjectCustomFieldArgs(\%ARGS, IncludeBulkUpdate => 1);
+my $CustomField;
+foreach my $class ( keys %custom_fields ){
+ foreach my $id ( keys %{$custom_fields{$class}} ){
+ ($CustomField) = keys %{$custom_fields{$class}{$id}};
+ }
+}
+
+unless ( $CustomField ) {
+ RT->Logger->debug("No CustomField provided");
+ $abort->();
+}
+
+my $SystemCustomFieldObj = RT::CustomField->new( RT->SystemUser );
+my ($id, $msg) = $SystemCustomFieldObj->LoadById( $CustomField ) ;
+unless ( $id ) {
+ RT->Logger->debug("Invalid CustomField provided: $msg");
+ $abort->();