[rt-users] Pulling CustomFieldValues

Mathew Snyder theillien at yahoo.com
Wed Nov 29 04:28:48 EST 2006


Stephen Turner wrote:
>> -----Original Message-----
>> From: rt-users-bounces at lists.bestpractical.com 
>> [mailto:rt-users-bounces at lists.bestpractical.com] On Behalf 
>> Of Mathew Snyder
>> Sent: Wednesday, November 22, 2006 3:35 AM
>> To: rt-users at lists.bestpractical.com
>> Subject: [rt-users] Pulling CustomFieldValues
>>
>> I'm trying to pull together a script which will print out all 
>> the values from a
>> particular CF.  It will work in conjunction with a bit of 
>> code already provided
>> to me by Roy El-Hames.
>>
>> I think I've figured out that I need to use 
>> RT::CustomFieldValues in order to
>> make use of the LimitToCustomField subroutine.  What I can't 
>> figure out is how
>> to pass the name of the CF to the subroutine.  Being a perl 
>> novice this eludes me.
>>
>> This is what I have so far:
>>
>> #!/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;
>>
>> CleanEnv();   ##Cleaning the env
>> RT::LoadConfig();   ## Loading RT config
>> RT::Init();    ## Initialise RT
>>
>> my $users = new RT::Users(RT::SystemUser);
>> $users->LimitToPrivileged;
>> my $CustomField = new RT::CustomFieldValues(Profiles);
>> $CustomField->LimitToCustomField;
>>
>> while ( $user = $users->Next) {
>>    next if $user->Name eq "root";
>>    print $user->Name . "\n";
>> }
>> exit;
>>
>> Can someone help me out and point me in the right direction?
>>
>> Thanks,
>> Mathew
>>
> 
> Mathew,
> 
> You can get at the values by using the  LimitToCustomField method on a
> CustomFieldValues object, but you need to supply the custom field's Id as an
> argument: 
> 
> $CustomField->LimitToCustomField (VALUE => $cf_id);
> 
> assuming you've got the CF id in $cf_id.
> 
> So first you'd have to load up a CustomField object and get the Id from
> there. Incidentally, it might be misleading to call the CustomFieldValues
> object $CustomField.
> 
> Another approach is to load the CustomField object and use the object's
> Values() method. This returns a CustomFieldValues object which you can then
> iterate through to see the CustomFieldValue objects.
> 
> Steve
> 
> 
Steve,

This is the code I now have:
#!/usr/bin/perl
use warnings;
use strict;
use lib "/usr/local/rt-3.6.1/lib";
use RT;
use RT::CustomField;
use RT::Interface::CLI qw(CleanEnv);

CleanEnv();
RT::LoadConfig();
RT::Init();
my $cf_name = "Profiles";

my $cf = RT::CustomField->new(RT::SystemUser);
$cf->Values($cf_name);
foreach my $cf_key (${cf}){
        print $$cf_key{"KEY"} . "\n";
};
exit;

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



More information about the rt-users mailing list