Add Article by ArticleCreate - problem with no UserObject

English! place to talk about development, programming and coding
Post Reply
duffy
Znuny newbie
Posts: 16
Joined: 29 Nov 2011, 11:57
Znuny Version: 3.3.8
Real Name: Rado Hrabcak

Add Article by ArticleCreate - problem with no UserObject

Post by duffy »

Hello,

I am trying to exetuce external module from Generic Agent. Invocation works well. But when debugging i see Got no UserObject in debug log file. Here is my piece of code:

Code: Select all

package Kernel::System::Ticket::Event::AddNote;

use strict;
use warnings;

use vars (qw(@ISA);

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

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

    # check needed objects
    for (
    	qw(ConfigObject EncodeObject TicketObject LogObject MainObject TimeObject DBObject UserObject)
    ) 
    {
	$Self->{$_} = $Param{$_} || die "Got no $_!";
    }

    return $Self;
  

    return $Self;
}
Even when i tried to add:

Code: Select all

 use Kernel::Config;
    use Kernel::System::Encode;
    use Kernel::System::Log;
    use Kernel::System::Main;
    use Kernel::System::Time;
    use Kernel::System::DB;
    use Kernel::System::User;

    my $ConfigObject = Kernel::Config->new();
    my $EncodeObject = Kernel::System::Encode->new(
        ConfigObject => $ConfigObject,
    );
    my $LogObject = Kernel::System::Log->new(
        ConfigObject => $ConfigObject,
        EncodeObject => $EncodeObject,
    );
    my $MainObject = Kernel::System::Main->new(
        ConfigObject => $ConfigObject,
        EncodeObject => $EncodeObject,
        LogObject    => $LogObject,
    );
    my $TimeObject = Kernel::System::Time->new(
        ConfigObject => $ConfigObject,
        LogObject    => $LogObject,
    );
    my $DBObject = Kernel::System::DB->new(
        ConfigObject => $ConfigObject,
        EncodeObject => $EncodeObject,
        LogObject    => $LogObject,
        MainObject   => $MainObject,
    );
    my $UserObject = Kernel::System::User->new(
        ConfigObject => $ConfigObject,
        LogObject    => $LogObject,
        MainObject   => $MainObject,
        TimeObject   => $TimeObject,
        DBObject     => $DBObject,
        EncodeObject => $EncodeObject,
    );

Any ideas what am I doing wrong?

Regards
OTRS 3.3.x on Linux with PostgreSQL database and LDAP
eandrex
Znuny expert
Posts: 213
Joined: 04 Nov 2012, 23:58
Znuny Version: OTRS 4.x
Real Name: Esteban
Company: NORTON DE COLOMBIA

Re: Add Article by ArticleCreate - problem with no UserObject

Post by eandrex »

Well, first off, do you really need UserObject in your module? ($Self->{UserObject}->SomeMethod())
if you dont, just remove UserObject from that loop and you are done.

if you really need it, change your code to something like this:

Code: Select all

package Kernel::System::Ticket::Event::AddNote;

use strict;
use warnings;
use Kernel::System::User;
use vars (qw(@ISA);

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

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

    # check needed objects
    for (
    	qw(ConfigObject EncodeObject TicketObject LogObject MainObject TimeObject DBObject)
    ) 
    {
	$Self->{$_} = $Param{$_} || die "Got no $_!";
    }
    $Self->{UserObject} = Kernel::System::User->new(%Param);

  

    return $Self;
}
note that you are missing "Run" method
duffy
Znuny newbie
Posts: 16
Joined: 29 Nov 2011, 11:57
Znuny Version: 3.3.8
Real Name: Rado Hrabcak

Re: Add Article by ArticleCreate - problem with no UserObject

Post by duffy »

Thanks!

It helped, I have Run method:

Code: Select all

sub Run {

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

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

      my $ArticleID = $Self->{TicketObject}->ArticleCreate(
            TicketID => 26559,
            ArticleType => 'note-external',
            From => 'GenericAgent',
            Subject => 'TEST FROM CUSTOM MODULE',
            Body => 'TEST NOTE EXTRNAL FROM CUSTOM MODULE',
        );
}

1;
Now, having no error in new method, now error in Run: "Message: Need Data!" ;(
OTRS 3.3.x on Linux with PostgreSQL database and LDAP
eandrex
Znuny expert
Posts: 213
Joined: 04 Nov 2012, 23:58
Znuny Version: OTRS 4.x
Real Name: Esteban
Company: NORTON DE COLOMBIA

Re: Add Article by ArticleCreate - problem with no UserObject

Post by eandrex »

Code: Select all

sub Run {

    my ( $Self, %Param ) = @_;
    
    for (qw(TicketID)) {
        if ( !$Param{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_ !" );
            return;
        }
    }

      my $ArticleID = $Self->{TicketObject}->ArticleCreate(
            TicketID => $Param{TicketID},
            ArticleType => 'note-external',
            From => 'GenericAgent',
            Subject => 'TEST FROM CUSTOM MODULE',
            Body => 'TEST NOTE EXTRNAL FROM CUSTOM MODULE',
        );
        
        return 1;
}

1;
duffy
Znuny newbie
Posts: 16
Joined: 29 Nov 2011, 11:57
Znuny Version: 3.3.8
Real Name: Rado Hrabcak

Re: Add Article by ArticleCreate - problem with no UserObject

Post by duffy »

Thanks!

Almost there, i try to run my module (code below), from GenericAgent, it works well when I run it manually, but when I trigger run on TicketStateUpdate filtering closed statesI got those errors:

[Tue Feb 24 22:05:50 2015] -e: Use of uninitialized value $Data{"UserType"} in string eq at /opt/otrs/Custom/Kernel/Output/HTML/DashboardAgentTime.pm line 136.
[Tue Feb 24 22:05:50 2015] -e: Use of uninitialized value $Data{"UserType"} in hash element at /opt/otrs/Custom/Kernel/Output/HTML/DashboardAgentTime.pm line 141.
[Tue Feb 24 22:05:50 2015] -e: Use of uninitialized value $Data{"UserID"} in hash element at /opt/otrs/Custom/Kernel/Output/HTML/DashboardAgentTime.pm line 141.
ERROR: OTRS-CGI-10 Perl: 5.14.2 OS: linux Time: Tue Feb 24 22:06:06 2015

Message: Need TicketID !

Still missing something?

Code: Select all

/opt/otrs/Custom/Kernel/System/Ticket/Event/AddNoteExternalOnTicketClose.pm
# --
# Kernel/System/GenericAgent/AutoPriorityIncrease.pm - generic agent auto priority increase
# Copyright (C) 2001-2013 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::AddNoteExternalOnTicketClose;

use strict;
use warnings;

use strict;
use warnings;
use Kernel::System::User;
use vars qw(@ISA);

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

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

    # check needed objects
    for (
       qw(ConfigObject EncodeObject TicketObject LogObject MainObject TimeObject DBObject)
    )
    {
	$Self->{$_} = $Param{$_} || die "Got no $_!";
    }
	$Self->{UserObject} = Kernel::System::User->new(%Param);

    return $Self;

}

sub Run {

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

    for (qw(TicketID)) {
        if ( !$Param{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_ !" );
            return;
        }
    }

    my $ArticleID = $Self->{TicketObject}->ArticleCreate(
	TicketID => $Param{TicketID},
        ArticleType => 'note-external',
        From => 'GenericAgent',
        Subject => 'TEST FROM CUSTOM MODULE',
        Body => 'TEST NOTE EXTRNAL FROM CUSTOM MODULE',
	UserID => 2,
	SenderTypeID => 2,
	HistoryType => 'AddNote',
	HistoryComment => 'Added note-external',
	Charset => 'UTF-8',
	MimeType => 'text/plain',
	);

 return 1;

}

1;
Regards
OTRS 3.3.x on Linux with PostgreSQL database and LDAP
Post Reply