Thank you for your feedback.
Form temporarily unavailable. Please try again or contact docfeedback@servicenow.com to submit your comments.
Versions
  • Madrid
  • London
  • Kingston
  • Jakarta
  • Istanbul
  • Helsinki
  • Geneva
  • Store
Close

Perl API examples

Log in to subscribe to topics and get notified when content changes.

Perl API examples

This reference provides examples of the Perl API.

Using the ServiceNow.pm module

Creating an incident:

#!/usr/bin/perl -w

use ServiceNow;
use ServiceNow::Configuration;

my $CONFIG = ServiceNow::Configuration->new();

$CONFIG->setSoapEndPoint("https://<instance name>.service-now.com/");
$CONFIG->setUserName("admin");
$CONFIG->setUserPassword("admin");

my $SN = ServiceNow->new($CONFIG);

my $number = $SN->createIncident({"short_description" => "this incident was created from the Perl API", "category" => "hardware"});

print $number ."\n";

Querying an Incident:

#!/usr/bin/perl -w

use ServiceNow;
use ServiceNow::Configuration;

my $CONFIG = ServiceNow::Configuration->new();

$CONFIG->setSoapEndPoint("https://<instance name>.service-now.com/");
$CONFIG->setUserName("admin");
$CONFIG->setUserPassword("admin");

my $SN = ServiceNow->new($CONFIG);

my @incidents = $SN->queryIncident({'number' => 'INC00002'});
my $count = scalar(@incidents);
print "number of incidents=" . $count . "\n";
foreach my $incident (@incidents) {
    print "Incident number: $incident->{'number'}\n";
    print "Assignent Group: $incident->{'assignment_group'}\n";
    print "Opened by:       $incident->{'opened_by'}\n";
    print "Opened by DV:    $incident->{'dv_opened_by'}\n";
    print "SD:       $incident->{'short_description'}\n";
    print "TW:       $incident->{'time_worked'}\n";
    print "\n"
}

Querying Journal fields:

#!/usr/bin/perl -w

use ServiceNow;
use ServiceNow::Configuration;

my $CONFIG = ServiceNow::Configuration->new();

$CONFIG->setSoapEndPoint("https://<instance name>.service-now.com/");
$CONFIG->setUserName("admin");
$CONFIG->setUserPassword("admin");

my $SN = ServiceNow->new($CONFIG);

my @journals = $SN->queryJournal('INC00002');

print $journals[0]->{'element'} . " = " . $journals[0]->{'value'} ."\n";
print $journals[1]->{'element'} . " = " . $journals[1]->{'value'} ."\n";

Using the ITIL Objects

Creating an Incident:

#!/usr/bin/perl -w

use ServiceNow;
use ServiceNow::Configuration;
use ServiceNow::ITIL::Incident;

my $CONFIG = ServiceNow::Configuration->new();

$CONFIG->setSoapEndPoint("https://<instance name>.service-now.com/");
$CONFIG->setUserName("admin");
$CONFIG->setUserPassword("admin");

# setting incident values as a hash map in the insert argument
my $incident = ServiceNow::ITIL::Incident->new($CONFIG);
my $sys_id = $incident->insert({"short_description" => "this incident was created from the Perl API", "category" => "hardware"});
print $sys_id ."\n";

# setting incident values by making setValue calls to the incident object
$incident = ServiceNow::ITIL::Incident->new($CONFIG);
$incident->setValue("short_description", "this incident was created from the Perl API - 2");
$incident->setValue("category", "hardware");
$sys_id = $incident->insert();
print $sys_id ."\n";

Querying for Incidents:

#!/usr/bin/perl -w

use ServiceNow;
use ServiceNow::Configuration;
use ServiceNow::ITIL::Incident;

my $CONFIG = ServiceNow::Configuration->new();

$CONFIG->setSoapEndPoint("https://<instance name>.service-now.com/");
$CONFIG->setUserName("admin");
$CONFIG->setUserPassword("admin");

my $incident = ServiceNow::ITIL::Incident->new($CONFIG);
$incident->addQuery("assignment_group", "Service Desk");
$incident->addQuery("category", "Hardware");
$incident->query();
while($incident->next()) {
        print "number=" . $incident->getValue("number") . "\n";
        print "sd=" . $incident->getValue("short_description") . "\n";
        print "opened_by Display Value= " . $incident->getDisplayValue("opened_by") . "\n";
        print "opened_by sys_id= " . $incident->getValue('opened_by');
}

Adding an attachment to a newly created Incident:

#!/usr/bin/perl -w

use ServiceNow;
use ServiceNow::Configuration;
use ServiceNow::ITIL::Incident;

my $CONFIG = ServiceNow::Configuration->new();

$CONFIG->setSoapEndPoint("https://<instance name>.service-now.com/");
$CONFIG->setUserName("admin");
$CONFIG->setUserPassword("admin");


my $incident = ServiceNow::ITIL::Incident->new($CONFIG);
$incident->setValue("short_description", "test incident for attachment 2");
$incident->insert();
$incident->attach("/Users/davidloo/Desktop/test_files/number_test.xls");
Feedback