Execute custom module with generic agent

English! place to talk about development, programming and coding
Post Reply
EXG133
Znuny expert
Posts: 217
Joined: 06 Aug 2012, 18:12
Znuny Version: 3.1.7 & 4.04

Execute custom module with generic agent

Post by EXG133 »

We have an attempt to count the number of Changes linked to a Ticket and write this value to a dynamic field.

So I have an XML:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<otrs_config version="1.0" init="Application">
    <ConfigItem Name="Kernel::System::PopulateCountChange###111-PopulateCountChange" Required="0" Valid="0">
        <Description Translatable="1">Module to count changes linked to a ticket</Description>
        <Group>Ticket</Group>
        <Setting>
            <Hash>
                <Item Key="Module">Kernel::System::PopulateCountChange</Item>
            </Hash>
        </Setting>
    </ConfigItem>
</otrs_config>
and a pm file:

Code: Select all

# --
# 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::PopulateCountChange;

use strict;
use warnings;

use Kernel::System::ITSMConfigItem;
use Kernel::System::GeneralCatalog;
use Kernel::System::DB;
use Kernel::System::ObjectManager;

use Kernel::System::GenericAgent;
use Kernel::System::DynamicField;
use Kernel::System::DynamicField::Backend;
use Kernel::System::Ticket;

local $Kernel::OM = Kernel::System::ObjectManager->new();

my $DynamicFieldValueObject;
my $FieldName;
my $FieldID;

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

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

    $Self->{DBSlaveObject} = $Param{DBSlaveObject} || $Kernel::OM->Get('Kernel::System::DB');

    return $Self;
}


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

    $DynamicFieldValueObject = $Kernel::OM->Get('Kernel::System::DynamicFieldValue');

    $FieldName = "ChangeCount";
    $FieldID = $Self->_GetIdDynamicField();

    $Self->_GetTicketsWithChanges;
}

sub _GetIdDynamicField() {
    my $Self = shift;
    my $returnvalue = 0;
    my $SQL = "select id from dynamic_Field where name = 'ChangeCount' ";
    $Self->{DBSlaveObject}->Prepare( SQL => $SQL );
    while ( my @Row = $Self->{DBSlaveObject}->FetchrowArray() ) {
        $returnvalue = $Row[0];
    }
    return $returnvalue;
}

sub _GetTicketsWithChanges {
    my $Self = shift;
    my $SQL = "SELECT source_key, " .   #  "Ticket#"
              "       count(source_key)" .   #  "number of Changes"
              "FROM link_relation" .
              "where source_key is not null" .
              "  and (    (source_object_id in (select id from link_object where name in ('Ticket')))" .
              "       and (target_object_id in (select id from link_object where name in ('ITSMChange'))))" .
              "group by source_key" .
              "order by source_key" ;
    $Self->{DBSlaveObject}->Prepare( SQL => $SQL );
    while ( my @Row = $Self->{DBSlaveObject}->FetchrowArray() ) {
        $Self->_PopulateDynamicField(\$Row[0], \$Row[1]);
    }
    return;
}

sub _PopulateDynamicField {
    my ( $TicketId, $AantalChanges ) = @_;
    my $Success = $DynamicFieldValueObject->ValueSet(
        FieldID  => $FieldID,  # ID of the dynamic field
        ObjectID => $TicketId, # ID of the current object that the field, must be linked to, e. g. TicketID
        Value    => [{ ValueText => $AantalChanges},],
        UserID   => 1,
    );
}

1;
When I try to run this through Generic Agent we get the error message:

“Can’t call method “new” without a package or object reference at /opt/otrs/Kernel/System/GenericAgent.pm line 1337”

What is the best method to debug this? I can't figure out where it's going wrong.
Post Reply