Get Owner from specific ticket ID

Hello :slight_smile:

I am currently building a script and I am trying to get the Owner of a Specific Ticket ID.
So then I can load it into a new User.

for example
$getOwnerofTicketID =

my $NewOwner = RT::User->new($RT::SystemUser);
$NewOwner->Load($getOwnerofTicketID);

Is this a RT::Scrip in the web UI? A user defined action or condition?

This is a script for a queue via the web ui

Condition : onCreate
Action: UserDefined

You can get the owner object with the following:

my $owner = $self->TicketObj->OwnerObj;

https://docs.bestpractical.com/rt/5.0.0/RT/Ticket.html#OwnerObj

1 Like

I am trying to get the Owner of the previous ticket

for example

my $ticket = $self->TicketObj->id;
my $newTicketID = $ticket - 1; 
my perviousTicketOwner = (get Owner of $newTicketID)

my $NewOwner = RT::User->new($RT::SystemUser);
$NewOwner->Load($perviousTicketOwner);

You can load the old ticket and use the same method:

my $prev_ticket = RT::Ticket->new( RT->SystemUser );
$prev_ticket->Load( $id );

my $owner = $prev_ticket->OwnerObj;
1 Like

So this way,
ex, ticket: 9001, Owner ‘a’
ticket: 9000, Owner ‘b’

$owner will get the owner b?

I am not sure what that is showing exactly. If you share the code it would be more helpful, whichever ticket you load by ID you should be able to just grab the owner from it then

1 Like

Sure thing, thanks again, i’m quite new to rt :slight_smile:

my $ticket = $self->TicketObj->id;
my $perv_ticket_id = $ticket - 1; 

my $perv_ticket_owner = RT::Ticket->new( RT->SystemUser );
$perv_ticket_owner->Load( $perv_ticket_id );

my $perv_owner = $perv_ticket_owner->OwnerObj;

my @members = ('user1', 'user2');

my $assignTicket;
my $buckets = [];
my $bucket_count =scalar(@members);

while (my ($ArrayIndex, $ArrayItem) = each @members) {
   if($ArrayItem eq $perv_owner) {
  	$assignTicket = $members[$ArrayIndex-1];
  }
}

my $new_owner = RT::User->new($RT::SystemUser);
$new_owner->Load($assignTicket);


# trying to change owner
$RT::Logger->info("Auto assign ticket #". $self->TicketObj->id ." to owner #". $new_owner );
my ($status, $msg) = $self->TicketObj->_Set(Field => 'Owner', Value => $new_owner->id, RecordTransaction => 0);
unless( $status ) {
$RT::Logger->error( "Impossible to assign the ticket to $new_owner: $msg" );
return undef;
}
return 1;

This bit is confusing, what it the goal here? It seems you just want to get the previous owner still? Which you already have right?

my ($status, $msg) = $self->TicketObj->_Set(Field => 'Owner', Value => $new_owner->id, RecordTransaction => 0);

I think calling _Set is dangerous since its an internal method, you can use SetOwner instead:
https://docs.bestpractical.com/rt/5.0.0/RT/Ticket.html#SetOwner

1 Like

Sorry not the pervious owner, the next owner inside the array of members

my @members = ('user1', 'user2', 'user3');

while (my ($ArrayIndex, $ArrayItem) = each @members) {
   if($ArrayItem eq $perv_owner) {
  	$assignTicket = $members[$ArrayIndex+1];
  }
}

And we have to use rt v. 4.2.

Ah OK, does the scrip work right now? If not what error logging are you seeing?

Right now I can see the case where you match on ‘user3’ and then the $ArrayIndex+1 is out of the range of the array ( it should go back to 0 ).

Depending on your workflow it may be worth checking out this extension:

1 Like

yes, the loop should go back to the beginning of the array :slight_smile:
The funny thing is, I do not have access to the error logs :laughing:

Lol that makes it more of challenge for sure!

1 Like

Try this:

my $prev_ticket_id = $self->TicketObj->id - 1;

my $prev_ticket = RT::Ticket->new( RT->SystemUser );
my ($ret, $msg) = $prev_ticket->Load( $prev_ticket_id );
RT::Logger->error( "Could not load previous ticket from id #$prev_ticket_id : $msg" ) unless $ret;

my $prev_owner = $prev_ticket->OwnerObj;

my @members = ('user1', 'user2', 'user3');

my $new_owner_name;
my $index = 0;
foreach my $member ( @members ) {
    if ( $member eq $prev_owner->Name ) {
        $new_owner_name = $index + 1 >= scalar @members ? $members[0] : $members[$index + 1];
        last;
    }
    $index = $index + 1;
}
unless ( $new_owner_name ) {
    RT::Logger->error( "No owner found :(" );
    return 0;
}

my $new_owner = RT::User->new( RT->SystemUser );
($ret, $msg) = $new_owner->Load( $new_owner_name );
unless ( $new_owner_name ) {
    RT::Logger->error( "Could not load user: $new_owner_name" );
    return 0;
}

# trying to change owner
RT::Logger->info("Auto assign ticket #". $self->TicketObj->id ." to owner #". $new_owner_name );
($ret, $msg) = $self->TicketObj->SetOwner( $new_owner->Id );
unless( $ret ) {
  RT::Logger->error( "Impossible to assign the ticket to $new_owner_name: $msg" );
  return 0;
}

return 1;
1 Like

Very nice indeed :wink:
When I am creating a new ticket, it isn’t assigning an owner tho

Without logs its hard to know the issue, but you should make sure the users who it tries to assign the ticket to have the rights to own a ticket.

1 Like

Oh I see the issue here, it is because the owner is set to ‘nobody’ :laughing:

Just reading through this, am I right in thinking the goal is to assign ticket owners from a group (such as service desk staff) so a newly arrived ticket gets the “next” owner in your staff list? A sort of round robin assignment?

Does it need to take into account tickets already owned by these staff members in case some tickets take longer to handle than others? Otherwise some unlucky person might end up with all the crappy long winded tickets. :slight_smile:

1 Like

Yes exactly, so the script will assign the previous ticket to the next person in the list.
Now there are going to be exceptions, for certain people it will skip 2 persons in the list :slight_smile: