[rt-users] Pulling CustomFieldValues

Mathew Snyder theillien at yahoo.com
Thu Dec 7 04:23:24 EST 2006


Stephen Turner wrote:
>   
> 
>> -----Original Message-----
>> From: Mathew Snyder [mailto:theillien at yahoo.com] 
>> Sent: Wednesday, November 29, 2006 4:29 AM
>> To: sturner at MIT.EDU
>> Cc: rt-users at lists.bestpractical.com
>> Subject: Re: [rt-users] Pulling CustomFieldValues
>>
>> Steve,
>>
>> This is the code I now have:
>>
>> my $cf = RT::CustomField->new(RT::SystemUser);
>> $cf->Values($cf_name);
>> foreach my $cf_key (${cf}){
>>         print $$cf_key{"KEY"} . "\n";
>> };
>>
>> Values() appears to return a reference to a hash.  If I run 
>> this code I get one
>> blank line back.  I guess I don't know how to iterate through 
>> the hash to print
>> out each item.  How would I go about that?
>>
>> Mathew
>>
> 
> Hi Mathew,
> 
> You're getting close! The Values method actually returns a CustomFieldValues
> object, even though a print will show a hash. This object represents a
> collection of CustomFieldValue objects and you can iterate through the value
> objects using the RT collections API (see RTx::SearchBuilder):
> 
> My $cfvalues = $cf->Values($cf_name);
> while (my $cfval = $cfvalues->Next() ){
>         print $cfval->Name . "\n";
> };
> 
> You can see this in action on the custom field 'modify' screen - (
> /Admin/CustomFields/Modify.html ). This calls
> /Admin/Elements/EditCustomFieldValues which displays a list of the existing
> values for the CF.
> 
> Steve
> 
> 

This is another version that will print out the names of all privileged users
and is supposed to, again, print out the values of a particular custom field.
It will print out the users but not the values.

#!/usr/bin/perl
use lib "/usr/local/rt-3.6.1/lib";
use RT;
use RT::Users ; ## you may not need this but what the he
use RT::CustomFieldValues;
use RT::Interface::CLI qw(CleanEnv);
use warnings;
use strict;

CleanEnv();   ##Cleaning the env
RT::LoadConfig();   ## Loading RT config
RT::Init();    ## Initialise RT
my $cf_name = 'Profiles';

my $users = new RT::Users(RT::SystemUser);
$users->LimitToPrivileged;
my $cf = new RT::CustomField(RT::SystemUser);
my $cfvalues = $cf->Values($cf_name);

while (my $user = $users->Next) {
   next if $user->Name eq "root";
   print $user->Name . "\n";
}

while (my $cfval = $cfvalues->Next) {
   print $cfval . "\n";
}

exit;

I tried looking at Modify.html to see how it goes about loading the values into
the fields but that was mostly a jumble of text to me.

Mathew



More information about the rt-users mailing list