Default value custom field

Hi all,
I have a global ticket custom field named “Phone” (type single value) and on ticket creation, in the edit phase, I would like it to take the WorkPhone loaded from LDAP as default value (but the user can change this value). It’s possible? RT 5.0.3

this is what i load from ldap:

Import the following properties of the user from LDAP upon login

‘attr_map’ => {
‘Name’ => ‘sAMAccountName’,
‘EmailAddress’ => ‘mail’,
‘RealName’ => ‘cn’,
‘WorkPhone’ => ‘telephoneNumber’,
},

Thanks, Paolo

Do you have a WorkPhone custom field applied to RT User records?

Hi,
no my only custom field is the ticket custom field “Phone” (a global cf).
I load WorkPhone from ldap (i use ldap authentication) and i can view it through RT_SiteConfig.pm: Set($MoreAboutRequestorExtraInfo, ‘WorkPhone’); in the Requestor’s information.

I understood that WorkPhone is a default field of RT User records.

I would like my custom ticket field “Phone” to load as default the value of the requestor’s default field WorkPhone

Oh right its a default value, I believe you can create a scrip that runs on ticket create and checks if the user has a value for workphone, and if it does then set the ticket CF to that value

Something like:

my $phone = $self->TicketObj->Requestors->UserMembersObj->First->WorkPhone;
my($ret, $msg) = $self->TicketObj->AddCustomFieldValue(Field => "WorkPhone", Value => $phone));

RT::Logger->error($msg) if $ret;

return $ret;

Yes it works, but I would like it to be seen in the edit phase, instead it appears only after creation in this way

I tried with this scrip

Description: Set <CF name> to <XXX> by default
Action: User defined
Custom action preparation code: return 1;
Custom action cleanup code:
  my $CFName = 'myCF';
  my $DefaultValue = 'myDefValue';
  my $RecTransaction = 1;

  my $QueueObj = $self->TicketObj->QueueObj;
  my $CFObj = RT::CustomField->new( $QueueObj->CurrentUser );
  $CFObj->LoadByNameAndQueue( Name => $CFName, Queue => $QueueObj->id );
  unless( $CFObj->id ) {
    $CFObj->LoadByNameAndQueue( Name => $CFName, Queue => 0 );
    unless( $CFObj->id ) {
      $RT::Logger->warning("custom field '$CFName' isn't global or defined for queue '". $QueueObj->Name ."'");
      return undef;
    }
  }

  unless( $self->TicketObj->FirstCustomFieldValue( $CFObj->id ) ) {
    my( $st, $msg ) = $self->TicketObj->AddCustomFieldValue(
                                          Field => $CFObj->id,
                                          Value => $DefaultValue,
                                          RecordTransaction => $RecTransaction );
    unless( $st ) {
      $RT::Logger->warning( "Couldn't set $DefaultValue as value for CF $CFName:". $msg );
      return undef;
    }
  }
  return 1;
Template: Global template: Blank

So just to be clear: you want the work phone number of the user filling in the form to appear as the default for this custom field when you bring up the /Ticket/Create.html form?

If that’s the case, you probably want to forget scrips, because they’ll always be run when you actually have a ticket, which is after that form has been submitted. Instead you might want to consider if a callback might help? The /Ticket/Create.html has a callback called “FormStart”, which you might be able to use to populate %ARGS (which is passed by reference in $ARGSRef in the callback parameters) with an entry that has the key CustomField-+the CF ID. You probably only want to populate that hash value if that key doesn’t already exist. The value you’d put in if it didn’t exist would then be something like $session{'CurrentUser'}->WorkPhone. That callback happens before the form is created, so by the time the /Elements/EdutCustomFields block is called you’ll have already slipped this in as a “default” value in the %ARGS hash.

Worth a shot maybe?

the ideal would be if you could put the workphone field here in the default value

Surely that’s what I need, I just have to figure out exactly how to do it (I’ve been using rt recently)

I almost solved, i created the file
<rt>/local/html/Callbacks/CustomFieldDefaultValues/Elements/EditCustomFields/MassageCustomFields

<%INIT>

unless ($CustomFields) {
    return 1;
}

while (my $customField = $CustomFields->Next) {
    my $id          = $customField->Id;
    my $key         = 'Field-'. $id;
 
    if ($id eq 2) {
      my $cf_value = $session{'CurrentUser'}->WorkPhone;
      $m->notes($key, 'Telefono123');
    } elsif ($id eq 3) {
      $m->notes($key, 'Nome PC 123');
    }
}

</%INIT>
<%ARGS>
    $CustomFields => undef
</%ARGS>

I need to put in $cf_value the Workphone…
$session{‘CurrentUser’}->WorkPhone gives the error
[1528] RT::CurrentUser::WorkPhone Unimplemented in HTML::Mason::Commands, any solution?

Thanks, Paolo

my $cf_value = $session{'CurrentUser'}->UserObj->WorkPhone;

1 Like

It works!!! Perfect!!
Thanks a lot to @knation and @GreenJimll

Sorry about leading you astray a bit there!

It was the right way! :slight_smile: