Monday, August 16, 2010

otrs -- batch create new customers

In case you don't know, OTRS is a ticketing system for with many features to manage customer telephone calls and e-mails. For more info see

Working with version 2.4 I realized that there was no option for batch customer import. On the other hand the APIs where sitting there, and there was even a script for user creation (otrs.adduser). Thus I decided to roll-out my own otrs.addCustomerUser:


#!/usr/bin/perl -w
# --
# otrs.addCustomerUser - Add User from CLI
# Copyright (C) elias chatzigeorgiou (
# --
# based on: otrs.addUser,v 1.17 2009/04/23 13:47:27
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or see
# --

use strict;
use warnings;

use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";

use vars qw (%opts);
use Getopt::Std;
getopt( 'flpge', \%opts );
unless ( $ARGV[0] ) {
        "$FindBin::Script [-f firstname] [-l lastname] [-p password] [-g groupname] [-e email] username\n";
    print "\tif you define -g with a valid group name then the user will be added that group\n";
    print "\n";

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

# create common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject);
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject}  = Kernel::System::Log->new( %CommonObject, LogPrefix => 'otrs.addUser' );
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{DBObject}   = Kernel::System::DB->new(%CommonObject);
$CommonObject{UserObject} = Kernel::System::CustomerUser->new(%CommonObject);

my %Param;
undef %Param;

#user id of the person adding the record
$Param{UserID} = '1';

$Param{ValidID} = '1';

$Param{Source}        = 'CustomerUser';
$Param{UserFirstname} = $opts{f};
$Param{UserLastname}  = $opts{l};
$Param{UserCustomerID}= $ARGV[0];
$Param{UserLogin}     = $ARGV[0];
$Param{UserPassword}  = $opts{p};
$Param{UserEmail}     = $opts{e};

=item CustomerUserAdd()

to add new customer users

    my $UserLogin = $CustomerUserObject->CustomerUserAdd(
        Source         => 'CustomerUser', # CustomerUser source config
        UserFirstname  => 'Huber',
        UserLastname   => 'Manfred',
        UserCustomerID => 'A124',
        UserLogin      => 'mhuber',
        UserPassword   => 'some-pass', # not required
        UserEmail      => '',
        ValidID        => 1,
        UserID         => 123,


if ( $Param{UID} = $CommonObject{UserObject}->CustomerUserAdd( %Param, ChangeUserID => 1 ) ) {
    print "User added. user  id is $Param{UID}\n";



Powered by Blogger.