Eventhandler or custom module for parentticketnumber

Moderator: crythias

Post Reply
mindchanger
Znuny newbie
Posts: 8
Joined: 06 May 2020, 06:15
Znuny Version: 6.0.27
Real Name: Marius Müller

Eventhandler or custom module for parentticketnumber

Post by mindchanger »

Dear all.

I'm thinking about a possible solution for the problem of an unknown parent ticket number for a created (split) child ticket.
Example:
We often split Tickets for further processing like hardware preparation. The problem there is, that the parent ticket number is not directly known as a kind of a dynamic field or something like this. The guys need to manually copy and paste this parent ticket number for putting it into a texttemplate for informing other colleagues.

Idea 1: Take the event module of otrs and search for the linked ticketnumber and fill the dynamic field "parentticketnumber" with the given parent ticket number

idea 2: Use the generic agent and trigger for ticketcreate, then run a custom module which should do the same like idea 1.

I tried to use the given example from crythias (https://forums.otterhub.org/viewtopic.php?t=10090 and this nearly same request https://forums.otterhub.org/viewtopic.php?t=39213) concerning idea1 with the event module
but it is not compatible for OTRS 6 any more.

Which idea would you prefer for solving this problem?

Thanks in advance.
Last edited by mindchanger on 15 Apr 2021, 15:10, edited 1 time in total.
---
OTRS 6.0.27
Debian 9
MySQL
skullz
Znuny superhero
Posts: 621
Joined: 24 Feb 2012, 03:58
Znuny Version: LTS and Features
Real Name: Mo Azfar
Location: Kuala Lumpur, MY
Contact:

Re: Eventhandler or custom module for parentticketnumber

Post by skullz »

Basically, both is the same. You are executing the custom module upon some ticket action (create, etc ..)..
Either way should make the job done as long you know how to write the module (calling api, update dynamic field etc), sure..

Additionally, the xml for linked url is using for version 4/5.
For OTRS 6, the xml is a bit different and suppose to be in Kernel/Config/Files/XML/ directory instead

https://github.com/OTRS/otrs/blob/d7d3c ... t.xml#L246

Good luck
mindchanger
Znuny newbie
Posts: 8
Joined: 06 May 2020, 06:15
Znuny Version: 6.0.27
Real Name: Marius Müller

Re: Eventhandler or custom module for parentticketnumber

Post by mindchanger »

Hi Skullz.

Thanks for your reply.

I tried to transfer your input into the given examples of crythias, but I think that I build in some mistakes :-) or better to say I cannot find my own failure. Just to see if the event works...

I've set up the following file:
/opt/otrs/Kernel/System/Ticket/Event/GetParentTicketNumber.pm

Code: Select all

# --
# Kernel/System/Ticket/Event/EventModulePostTemplate.pm - event module
# Copyright (C) 2001-2015 xxx, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::Ticket::Event::GetParentTicketNumber;

use strict;
use warnings;

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

    # allocate new hash for object
    my $Self = {};
    bless( $Self, $Type );

    # get needed objects
    for (
        qw(
        ConfigObject CustomerGroupObject CustomerUserObject DBObject EncodeObject GroupObject
        HTMLUtilsObject LinkObject LockObject LogObject LoopProtectionObject MainObject
        PriorityObject QueueObject SendmailObject ServiceObject SLAObject StateObject
        TicketObject TimeObject TypeObject UserObject ValidObject
        )
        )
    {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }

    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;
        }
    }

    return 1 if $Param{Event} ne 'TicketCreate';

    my %Ticket = $Self->{TicketObject}->TicketGet(
        TicketID => $Param{Data}->{TicketID},
        UserID   => 1,
    );
    return 1 if !%Ticket;
    return 1 if $Ticket{State} eq 'Open';

    # do some stuff
    $Self->{TicketObject}->HistoryAdd(
        TicketID     => $Param{Data}->{TicketID},
        CreateUserID => 1,
        HistoryType  => 'Misc',
        Name         => 'New ticket with state other than Open!',
    );
    return 1;
}

1;
and the corresponding XML file:
/opt/otrs/Kernel/Config/Files/XML/GetParentTicketNumber.xml

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<otrs_config version="2.0" init="Application">
<Setting Name="Ticket::EventModulePost###8899-GetParentTicketNumber" Required="0" Valid="1">
        <Description Translatable="1">When agent creates a ticket, find out the parent ticket number.</Description>
        <Navigation>Core::Event::Ticket</Navigation>
        <Value>
                <Hash>
                        <Item Key="Module">Kernel::System::Ticket::Event::GetParentTicketNumber</Item>
                        <Item Key="Event">TicketCreate</Item>
                        <Item Key="Action">AgentTicketPhone|AgentTicketEmail</Item>
                        <Item Key="Transaction">1</Item>
                </Hash>
        </Value>
</Setting>
</otrs_config>
Sadly I cannot recognize any kind of reaction or triggering, neither error messages nor log file entries.

Any idea?

Thanks in advance and best regards

Jan
---
OTRS 6.0.27
Debian 9
MySQL
skullz
Znuny superhero
Posts: 621
Joined: 24 Feb 2012, 03:58
Znuny Version: LTS and Features
Real Name: Mo Azfar
Location: Kuala Lumpur, MY
Contact:

Re: Eventhandler or custom module for parentticketnumber

Post by skullz »

i would try to test it based on otrs 6 script instead..

https://github.com/OTRS/otrs/tree/rel-6 ... cket/Event
mindchanger
Znuny newbie
Posts: 8
Joined: 06 May 2020, 06:15
Znuny Version: 6.0.27
Real Name: Marius Müller

Re: Eventhandler or custom module for parentticketnumber

Post by mindchanger »

Dear all.
Sorry, I needed to spend some time on other topics, but this popped up again :-)
I optimized the whole thing a bit, but I am still not able to catch the correct event. Assuming that it is "LinkObjectLinkAdd".

Now to the code:
xml file:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<otrs_config version="2.0" init="Application">
    <Setting Name="Ticket::EventModulePost###3200-GetParentTicketNumber" Required="0" Valid="0">
        <Description Translatable="1">When agent creates a ticket, whether or not the ticket is automatically locked to the agent.</Description>
        <Navigation>Core::Event::LinkObject</Navigation>
        <Value>
            <Hash>
                <Item Key="Module">Kernel::System::Ticket::Event::GetParentTicketNumber</Item>
                <Item Key="Event">TicketCreate|LinkObjectLinkAdd</Item>
                <Item Key="Action">AgentTicketPhone|AgentTicketEmail</Item>
                <Item Key="Transaction">1</Item>
            </Hash>
        </Value>
    </Setting>



</otrs_config>
and the pm file:

Code: Select all

# --
# Kernel/System/Ticket/Event/EventModulePostTemplate.pm - event module
# Copyright (C) 2001-2015 xxx, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::Ticket::Event::GetParentTicketNumber;

use strict;
use warnings;

use List::Util qw(first);
use Mail::Address;

#use Kernel::System::VariableCheck qw(:all);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::System::CustomerUser',
    'Kernel::System::CheckItem',
    'Kernel::System::DB',
    'Kernel::System::DynamicField',
    'Kernel::System::DynamicField::Backend',
    'Kernel::System::Email',
    'Kernel::System::Group',
    'Kernel::System::HTMLUtils',
    'Kernel::System::JSON',
    'Kernel::System::Log',
    'Kernel::System::NotificationEvent',
    'Kernel::System::Queue',
    'Kernel::System::SystemAddress',
    'Kernel::System::TemplateGenerator',
    'Kernel::System::Ticket',
    'Kernel::System::Ticket::Article',
    'Kernel::System::DateTime',
    'Kernel::System::User',
    'Kernel::System::CheckItem',
);



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

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

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

    return $Self;
    
 sub Run {

    my ( $Self, %Param ) = @_;

    my $filename = "/opt/otrs/Custom/file_".$Param{Event}.".txt";
    open(my $fh, '>', $filename) or die "Cant open.";
    print $fh "1";
    close $fh;



    if ($Param{Event} eq 'LinkObjectLinkAdd') {
      my $filename = "/opt/otrs/Custom/event_catched.txt";
    open(my $fh, '>', $filename) or die "Cant open.";
    print $fh "1";
    close $fh;

   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 ($Param{Event} eq 'LinkObjectLinkAdd') {
      my $filename = "/opt/otrs/Custom/event.txt";
    open(my $fh, '>', $filename) or die "Cant open.";
    print $fh "1";
    close $fh;

        if (%LinkList) { # is empty :(
           my $filename = "/opt/otrs/Custom/summary.txt";
           open(my $fh, '>', $filename) or die "Cant open.";
           print $fh "Parent Ticket found.";
           close $fh;
        }
    }
    return 1;
}
1;

Any ideas how to catch the "LinkObjectLinkAdd"- Event? The Ticket create event is been catched during the "Ticket-Split-Action"- no problem with that, but it ignores the link event completely.

Thanks in advance
---
OTRS 6.0.27
Debian 9
MySQL
Post Reply