[rt-users] Question about autogenerating custom fields
Gene LeDuc
gleduc at mail.sdsu.edu
Fri Apr 6 12:24:23 EDT 2007
Hi Enrique,
Here are the functions I use for populating and reading CFs in scrips and
templates. I just wrapped function headers around the the code I found in
the wiki. The get_custom_list function uses brute force to find the CF Id
because I got tired of trying to figure out how to get it by name, but it
works for me (I only have a template version because I haven't had to read
multiple values in a scrip yet, but converting it for scrip use should be
trivial).
For scrips:
### Sets custom field value
### usage: set_custom($field_name, $field_value);
sub set_custom {
(my $CFName, my $CFValue) = @_;
my $CF_Obj = RT::CustomField->new($self->CurrentUser);
$CF_Obj->LoadByName(Name=>$CFName,);
$CF_Obj->AddValueForObject(Object=>$self->TicketObj, Content=>$CFValue,);
}
### Returns custom field value (single value) or undefined
### usage: $value = get_custom($field_name);
sub get_custom {
my $target_name = $_[0];
my $val = $self->TicketObj->FirstCustomFieldValue($target_name);
return $val
if defined $val;
return undef;
}
For templates:
### Sets custom field value (works for single and multiple values)
### usage: set_custom($field_name, $field_value);
sub set_custom {
(my $target_name, my $val) = @_;
my $CF_Obj = RT::CustomField->new($Ticket->CurrentUser);
$CF_Obj->LoadByName(Name=>$target_name,);
$CF_Obj->AddValueForObject(Object=>$Ticket, Content=>$val,);
}
### Returns custom field value (single value only) or undefined
### usage: $value = get_custom($field_name);
sub get_custom {
my $target_name = $_[0];
my $val = $Ticket->FirstCustomFieldValue($target_name);
return $val
if defined $val;
return undef;
}
### Returns custom field values (multiple values) or undefined
### usage: @list = get_custom_list($field_name);
sub get_custom_list {
my $target_name = $_[0];
my @ret;
my $CustomFields = $Ticket->QueueObj->TicketCustomFields();
while (my $CustomField = $CustomFields->Next()) {
if ($CustomField->Name eq $target_name) {
my $CustomFieldValues = $Ticket->CustomFieldValues($CustomField->Id);
while (my $val = $CustomFieldValues->Next()) {
push @ret, $val->Content;
}
return @ret
if defined @ret;
return undef;
}
}
return undef;
}
Regards,
Gene
At 06:13 AM 4/6/2007, Enrique Gaona wrote:
>Gene,
>Thanks for the suggestion. I'll look through the Contributions for some
>examples.
>
>Enrique
More information about the rt-users
mailing list