Scrip to enhance Custom field Default value

Hello,

I really apreciate the “Default values” for custom field on a queue, which I use a lot at ticket creation.
Now when ticket come from a queue where no default values are defined and are move to a cue where they should be, the default values are not triggered :confused:

So I’d like to setup a scrip doing this in pseudo-code :

On queue change, FOR (each custom field) {
IF (the custom field should have a default value in the target queue AND the custom field is currently empty)
{ populate the custom filed with the default value }
ELSEIF (the custom field is the “due date” or “priority” or “final priority”)
{ populate with the default value }
}

Could you help me write down this scrip ?
Here where i’m now

Condition: On queue Change
Action: User-defined
Template: Global Blank
Stage: TransactionBatch

#get the target queue in the transaction object
my $trans = $self->TransactionObj;
my $targetqueue = $trans->…?

#check all tickets custom fields
my $ticket = $self->TicketObj;
my $CustomFields = $ticket->QueueObj->TicketCustomFields();
while (my $CustomField = $CustomFields->Next()) {
my $cfname = $CustomField->Name;

if ( $ticket->FirstCustomFieldValue($cfname) eq ‘’ AND SHOULDHAVEDEFAULT) {
#populate the custom filed with the default value
$ticket->AddCustomFieldValue( Field => $CustomField, Value => ‘THEDEFAULTVALUE’ );
}
elseif ( ($cfname eq ‘Priority’) or ($cfname eq ‘Final Priority’) or ($cfname eq ‘Due Date’) ) {
#populate with the default value
$ticket->AddCustomFieldValue( Field => $CustomField, Value => ‘THEDEFAULTVALUE’ ); }
}

A quick peek in lib/RT/CustomField.pm shows there are (in 4.4.2 at least) methods:

  • SupportDefaultValues - returns 0 if the CF does not support default values

  • DefaultValues - returns the default values for a CF, or undef if there are not any

  • SetDefaultValues - lets you set the default values for a CF

  • CleanupDefaultValues - make sure available default values reflect the type of CF (eg arrays of default values can only be used with Select CF types).

Oddly, I don’t see these methods mentioned in the latest CustomField.pm documentation. An oversight by the Best Practical folk in the documentation when they were added maybe?

BTW notice that Condition: On queue Change means “execute this after the ticket is in new queue. Not before.” Because it’s done when the move transaction is completed.

1 Like

@GreenJimll thanks for the details i should get it done, and post it here.
I’m always struggling to find where the doc is, you made my day

@Petr_Hanousek nice point, this should simply the script

I was in 4.4.1 and those methods are here too, but didn’t work.
So I upgraded to 4.4.3 just to be sure, but we’re not here yet.

here’s the actual script :

my $queueobj =  $self->TicketObj->QueueObj;
my $queuename = $queueobj->Name;

#check all tickets custom fields
my $ticket = $self->TicketObj;
my $CustomFields = $ticket->QueueObj->TicketCustomFields();

while (my $CustomField = $CustomFields->Next()) {
  my $cfname = $CustomField->Name;

  if ( $CustomField->SupportDefaultValues() ) {
    #populate the custom filed with the default value
    my $msg = $CustomField->SetDefaultValues; 
  $RT::Logger->debug( "[Scrip SetDefaultCF] returned ".$msg );
  }
} 

while the scrip is correctly going through all CF, they all return 1 on SupportDefaultValues methods.
Then the SetDefaultValues methods does NOT set the intended default value but returns 1.
A quick look at lib/RT/CustomField.pm would say to me that it return 1 because the “old content” is the same as the “new content”, which actually is.
I tried applying the method to different objects (ticket,customfields object), without success (worse results with errors actually)

So either The method is not intended so actually set the value, or I’m missing some arguments or something

after changing the scrip to

my $queueobj =  $self->TicketObj->QueueObj;
my $queuename = $queueobj->Name;

#check all tickets custom fields
my $ticket = $self->TicketObj;
my $CustomFields = $ticket->QueueObj->TicketCustomFields();

while (my $CustomField = $CustomFields->Next()) {
  my $cfname = $CustomField->Name;

  $RT::Logger->debug( "[Scrip SetDefaultCF] working on CF named ".$cfname );
  
  if ( $CustomField->SupportDefaultValues() ) {

    #populate the custom filed with the default value
    my ($ret, $msg) = $CustomField->SetDefaultValues(
        Object => RT->System,
        Values => 'Default-' . $CustomField->id . '-Value'
); 

  $RT::Logger->debug( "[Scrip SetDefaultCF] returned value ".$ret." and message ".$msg );

  }
}

I have a result !! but not the expected one

The message is :
[Scrip SetDefaultCF] returned value 1 and message Default values changed from (no value) to Default-53-Value ((eval 509):26)

Which changed the Default values for the target queue (Admin/Queues/DefaultValues.html), and not on the ticket itself :sob:

so i’m back to code …

if code changed to

my $queueobj =  $self->TicketObj->QueueObj;
my $queuename = $queueobj->Name;

#check all tickets custom fields
my $ticket = $self->TicketObj;

my  $CustomFields = $ticket->CustomFields();

$RT::Logger->debug( "[Scrip SetDefaultCF] CustomFields object = ".$CustomFields );

while (my $CustomField = $CustomFields->Next()) {
  my $cfname = $CustomField->Name;
  $RT::Logger->debug( "[Scrip SetDefaultCF] working on CF named ".$cfname );
  
  if ( $CustomField->SupportDefaultValues() ) {
    my $ret = $CustomField->SetDeffaultValue();
    $RT::Logger->debug( "[Scrip SetDefaultCF] Set CF default value returned ".$ret );
  }
}

I have a “Nonexistant field? ((eval 573):22)” returned message …
looking for help here

I think you might have too many f’s in that method.

Looking at the code though, do you not to call DefaultValues() to get the potential default values for that CustomField, and then assign them to the CF as normal? I would have thought that SetDefaultValues() sets up the possible default values that a CF can take - you want to use those if they exist to actually set the value(s) that the CF has.

The double f was a typo generating the ““Nonexistant field?” message.
With the correct ortograph, I still can’t manage to either GET or SET default values.
Your logic makes sense, but really I’m unable to obtain satisfying results.
I can understand the code, but i’m not enough familiar with the whole RT structure and logic, so can’t figure out what the params for the functions should be.

I also tried to find where RT does the job when creating a ticket … but didn’t.
So as for now I’m out of ideas.
Your comments are always welcome.

Whatever I put as parameter to the function
my $ret = $CustomField->SetDefaultValues(Object => RT->System,$queueobj);

If the parameter is of an accepted type (not trowing error), the function always returns 1 without apparently doing anything.