Get Owner from specific ticket ID

Ah, right. One other idea that struck me is that you might want consider getting the members of the list from an RT group, rather than hard coding them. Depending on how your RT is set up and administered that would allow staff to easily be added & removed from the list without flushing the Mason cache or the person doing the updates having rights to alter the scrip.

Just a thought.

1 Like

This is a very good thought :smiley:
What is coming into mind is, will users still keep the same sequence?
After checking the RT::Group Documentation
replace the use of array
@members
with the following

push @members, {
        Name        => 'Group Example',
        Description => 'Group Example',
        Members     => { Users =>  [ ],
                         Groups => [ qw/ exampleGroup/ ] },
    };

Any feedback would be appreciated as I do not have access to error logs :slight_smile:

I was thinking more instantiate an RT::Group object, use the LoadUserDefinedGroup() method to load a named group, and then use UserMembersObj() to get a list of user objects of members you could iterate through to populate your @members array. That should also handle recursive group membership IIRC.

1 Like

my new account got limited for 20h, so i’m using this one for now
So I am thinking something like this

my @get_group = RT::Group->LoadUserDefinedGroup('exampleGroup')
while ( my $member = $get_group->Next ) {

    my $memberLoader = RT::User->new($RT::SystemUser);
    $memberLoader->Load($member);
    push @members, $memberLoader;
}

Use the UserMembersObj() method on the loaded RT::Group object to get an RT::Users object that lists member RT::User objects and then iterate through that to populate your @members array. You’ll probably want just the Name field from each RT::User object to push onto the @members array if you want the rest of your code to stay as it is.

Bare in mind that if you don’t want to use the UserMembersObj() method on the RT::Group object you’ll have to deal with RT::Group members that are themselves RT::Group objects (as RT groups can have sub-groups as members, which in turn can have sub-groups as members, etc, etc). I’d avoid having to hand craft that if you can.

1 Like

So more something like this :slight_smile:

my @get_group = RT::Group->UserMembersObj('exampleGroup', Recursively => 0)

while ( my $member = $get_group->Next ) {

    my $memberLoader = RT::User->new($RT::SystemUser);
    $memberLoader->Load($member->Name);
    push @members, $memberLoader;
}

You need to create the RT::Group object (as you’ve done before above), call the LoadUserDefinedGroup() method with the name of your group (so 'exampleGroup' in this case) and then call UserMembersObj() method on that now instantiated RT::Group object. You’ll get an RT::Users object that you can then iterate throught with ->Next to get individual RT::User objects for each group member. On each of those you can then call the Name method to get the name of the user, which is what you push onto your @members array.

Note that RT returns new objects as scalar references, not arrays. See how @knation created the $prev_ticket variable to hold the reference to the new RT::Ticket object. The RT::Group works in the same basic way. You then make the method calls on that scalar reference, as he did with the Load() call. Once you’ve got your head round that, lots of the RT scripting is much easier to understand.

1 Like

So first I loaded the LoadUserDefinedGroup(), which contains all the Group information
From there, I loaded the Users using UserMembersObj() from the Group information
Then I iterated the list of users from the array of UserMembersObj(), which in turn should push the names of the users to my @member array

my @get_group = RT::Group->LoadUserDefinedGroup('exampleGroup')
my $saveMember = RT::User->new( RT->SystemUser );
my @get_users_group = RT::Group->UserMembersObj($saveMember->Load( $get_group) , Recursively => 0)

while ( my $member = $get_users_group->Next ) {
    
    my $memberLoader = RT::User->new( RT->SystemUser );
    $memberLoader->Load($member->Name);
    push @members, $memberLoader;
}

What are your thoughts? :slight_smile:

You’re still not quite getting it I’m afraid! You create an RT object such as RT::Group using the new method and the resulting reference to the underlying structure gets assigned to a scalar variable (one with a name starting with a $, not an @). In other words, @get_group should be $get_group and it should come from a call to new(), not LoadUserDefinedGroup().

Look up above at how @knation created the new RT::Ticket object and work from that example for creating a new object (using RT::Group rather than RT::Ticket obviously).

Once you have your $get_group variable you can then call the LoadUserDefinedGroup() and UserMembersObj() methods on that directly. The first loads the group you want into the object whilst the second returns a new object that you can put into a new scalar variable (say $group_members) that is an RT::Users object containing the members of the group.

You then do your while loop much as you have it but noting that $member will now come from $group_members->Next and $member is already an RT::User object because that’s what ->Next is returning to you. You don’t need to load it into a new object, just push $member->Name onto your @members array.

If you’re not comfy with the difference between Perl scalars, lists (aka arrays), hashes and references to objects, it might be worth taking a timeout to read up on that. There’s quite a few Perl variable explanations available online (and in paper if you prefer reading physical books). If you’ve not dealt with objects in Perl before it might be a bit deep for jumping straight in and learning the RT Perl API at the same time. Having a solid understanding of the Perl part will really help you.

1 Like