OTRS change request logging from command line

English! place to talk about development, programming and coding
Post Reply
nagabhus
Znuny newbie
Posts: 5
Joined: 20 Dec 2013, 09:00
Znuny Version: otrs-3.2.2

OTRS change request logging from command line

Post by nagabhus »

Hi,

I have written a perl script to log otrs change requests through command line. The script reads a file on local disk which has the description details and uses the same data to update the description field on the change ticket. However, I'm facing an issue with this. Looks like, only the first line read, is being update on the otrs change ticket and the rest of the lines after first line is being ignored by OTRS. Any pointers as what I'm doing wrong?

Below is my piece of code. Kindly note that, I'm reading contents from a file '/home/sindhu/cr1' into an array @lines and providing the same as Description value inside ChangeAdd.

Here is my contents of cr1 with 3 lines in it.

sindhu@supercomp:~$ cat cr1
Test line 1
Test line 2
Test line3

Code: Select all


#!/usr/bin/perl -w

use Getopt::Long;
use Kernel::Config;
use Kernel::System::Encode;
use Kernel::System::Time;
use Kernel::System::Log;
use Kernel::System::DB;
use Kernel::System::Ticket;
use Kernel::System::Main;
use Kernel::System::User;	
use Kernel::System::Group;
use Kernel::System::ITSMChange;

my $user;
my $path_to_file='/home/sindhu/cr1';

GetOptions ( "Username=s" => \$user, # string
	   )
or die("Error in command line arguments\n");

open my $handle, '<', $path_to_file;
(my @lines = <$handle>);
close $handle;


print @lines;

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,
    );

my $GroupObject = Kernel::System::Group->new(
        ConfigObject => $ConfigObject,
        LogObject    => $LogObject,
        DBObject     => $DBObject,
	MainObject   => $MainObject,
	EncodeObject => $EncodeObject,
    );

my $ChangeObject = Kernel::System::ITSMChange->new(
        ConfigObject => $ConfigObject,
        EncodeObject => $EncodeObject,
        LogObject    => $LogObject,
        DBObject     => $DBObject,
        TimeObject   => $TimeObject,
        MainObject   => $MainObject,
        UserObject   => $UserObject,
	GroupObject  => $GroupObject,
    );

my %User = $UserObject->GetUserData(
        User => $user,
    );

my $ChangeID = $ChangeObject->ChangeAdd(
        ChangeTitle     => 'Test ticket by sindhu - pls ignore',       # (optional)
        Description     => @lines,        # (optional)
        Justification   => 'Change ticket through comd line succeded',         # (optional)
#        ChangeStateID   => 4,                                  # (optional) or ChangeState => 'accepted'
#        ChangeState     => 'accepted',                         # (optional) or ChangeStateID => 4
#        ChangeManagerID => 5,                                  # (optional)
#        ChangeBuilderID => 6,                                  # (optional)
#        CategoryID      => 7,                                  # (optional) or Category => '3 normal'
#        Category        => '3 normal',                         # (optional) or CategoryID => 4
#        ImpactID        => 8,                                  # (optional) or Impact => '4 high'
#        Impact          => '4 high',                           # (optional) or ImpactID => 5
#        PriorityID      => 9,                                  # (optional) or Priority => '5 very high'
        Priority        => '3 normal',                      # (optional) or PriorityID => 6
#        CABAgents       => [ 1, 2, 4 ],     # UserIDs          # (optional)
#        CABCustomers    => [ 'tt', 'mm' ],  # CustomerUserIDs  # (optional)
#        RequestedTime   => '2006-01-19 23:59:59',              # (optional)
#        ChangeFreeKey1  => 'Sun',                              # (optional) change freekey fields from 1 to ITSMChange::FreeText::MaxNumber
#        ChangeFreeText1 => 'Earth',                            # (optional) change freetext fields from 1 to ITSMChange::FreeText::MaxNumber
        UserID          => $User{'UserID'},
    );
   

print "Change added. Change ID  is $ChangeID";

[/quote]


Additionally, when the file contains even number of lines, I get an error like below. I'm clueless about this error message. Kindly help.

sindhu@supercomp:~/scripts$ cat ~/cr1
Test line 1
Test line 2
sindhu@supercomp:~/scripts$ ./CreateTicket.pl --Username nagabhus
Test line 1
Test line 2
Odd number of elements in hash assignment at /opt/otrs/Kernel/System/ITSMChange.pm line 191.
ERROR: ?LogPrefix?-10 Perl: 5.14.2 OS: linux Time: Wed Dec 25 14:11:55 2013

Message: Need UserID!

Traceback (16146):
Module: Kernel::System::ITSMChange::ChangeAdd (v1.285.2.1) Line: 195
Module: ./CreateTicket.pl (unknown version) Line: 90

Use of uninitialized value $ChangeID in concatenation (.) or string at ./CreateTicket.pl line 113.
You do not have the required permissions to view the files attached to this post.
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: OTRS change request logging from command line

Post by reneeb »

nagabhus wrote:Hi,

Code: Select all

[...]
my $ChangeID = $ChangeObject->ChangeAdd(
        ChangeTitle     => 'Test ticket by sindhu - pls ignore',       # (optional)
        Description     => @lines,        # (optional)
[...]
This should be

Code: Select all

[...]
my $ChangeID = $ChangeObject->ChangeAdd(
        ChangeTitle     => 'Test ticket by sindhu - pls ignore',       # (optional)
        Description     => join( '', @lines ),
[...]
You can't pass an array as the value of an Hash in Perl. Each value has to be a scalar value (either a scalar variable or a reference). So the value for the Description has to be a single (scalar) value. By using "join", you concatenate the values of the array "@lines" and get a single value.
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