How to Get Parent "TicketID" from Child Ticket?

Moderator: crythias

Post Reply
VARD32
Znuny newbie
Posts: 6
Joined: 01 Mar 2017, 04:28
Znuny Version: 5.002

How to Get Parent "TicketID" from Child Ticket?

Post by VARD32 »

Hi!

In the module of the TicketCreate event, i have TicketID of the Child Ticket!
How can i get the Parent TicketID of the Parent ticket?

My Code:

Code: Select all

sub new {
    my ( $Type, %Param ) = @_;

    my $Self = {};
    bless( $Self, $Type );

    $Self->{LinkObject} = Kernel::System::LinkObject->new(%Param);
   
    return $Self;
}

sub Run {

    my ( $Self, %Param ) = @_;
    
    # check needed stuff
    for (qw(Data Event Config)) {
        if ( !$Param{$_} ) {
            $Self->{LogObject}->Log(
                Priority => 'error',
                Message  => "Need $_!"
            );
            return;
        }
    }

    if ($Param{Event} eq 'TicketCreate') {
       
   my $TicketID = $Param{Data}->{TicketID}; # Now, we have child TicketID.

        my %LinkList = $Self->{LinkObject}->LinkKeyListWithData(
           Object1   => 'Ticket',
           Key1      => $Param{Data}->{TicketID}, #child TicketID
           Object2   => 'Ticket',
           Type      => 'ParentChild',
           Direction => 'Source',
           State     => 'Valid',
           UserID    => 1,
        );

        if (%LinkList) { # is empty :(
           my $filename = "/usr/local/otrs/Custom/Kernel/summary.txt";
           open(my $fh, '>', $filename) or die "Cant open.";
           print $fh "Parent Ticket found.";
           close $fh;
        }      
    }       
    return 1;
}
but the code is not work properly ((. %LinkList always empty or null ((
Any ideas?
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: How to Get Parent "TicketID" from Child Ticket?

Post by reneeb »

The link isn't available at the ticket create event. If the link is added automatically, you should check TicketLinkAdd event.
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
VARD32
Znuny newbie
Posts: 6
Joined: 01 Mar 2017, 04:28
Znuny Version: 5.002

Re: How to Get Parent "TicketID" from Child Ticket?

Post by VARD32 »

Thanks for reply, reneeb!

"TicketLinkAdd" event? is this "boxed" Event?
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: How to Get Parent "TicketID" from Child Ticket?

Post by reneeb »

Sorry, should be LinkObjectLinkAdd (https://github.com/OTRS/otrs/blob/maste ... ct.pm#L583)
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
VARD32
Znuny newbie
Posts: 6
Joined: 01 Mar 2017, 04:28
Znuny Version: 5.002

Re: How to Get Parent "TicketID" from Child Ticket?

Post by VARD32 »

Thanks for reply, reneeb.
I was check your code, but it does not work ((

In the "Run" procedure of my event module, i have add the folowing code:

Code: Select all

sub Run {

    my ( $Self, %Param ) = @_;
    
    my $filename = "/usr/local/otrs/Custom/Kernel/file_".$Param{Event}.".txt";
    open(my $fh, '>', $filename) or die "Cant open.";
    print $fh "1";
    close $fh;
    
    return 1;
}
After creating child ticket, i found "file_TicketCreate.txt" on the disk only. I think, the "LinkObjectLinkAdd" event is not catching here.
In the configuration xml-file both event is present:

<ConfigItem Name="Ticket::EventModulePost###EventModulePostTemplate" Required="0" Valid="1">
<Description Lang="en"></Description>
<Group>Company</Group>
<SubGroup>Company::EventModule</SubGroup>
<Setting>
<Hash>
<Item Key="Module">Kernel::System::Ticket::Event::EventModulePostTemplate</Item>
<Item Key="Event">(TicketCreate|LinkObjectLinkAdd)</Item>
</Hash>
</Setting>
</ConfigItem>
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: How to Get Parent "TicketID" from Child Ticket?

Post by reneeb »

Try

Code: Select all

<ConfigItem Name="LinkObject::EventModulePost###EventModulePostTemplate" Required="0" Valid="1">
<Description Lang="en"></Description>
<Group>Company</Group>
<SubGroup>Company::EventModule</SubGroup>
<Setting>
<Hash>
<Item Key="Module">Kernel::System::Ticket::Event::EventModulePostTemplate</Item>
<Item Key="Event">(LinkObjectLinkAdd)</Item>
</Hash>
</Setting>
</ConfigItem>
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
Post Reply