Thank you for your feedback.
Form temporarily unavailable. Please try again or contact docfeedback@servicenow.com to submit your comments.

Direct web services Perl example

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

Direct web services Perl example

This example query queries all incidents, orders by the number field, and retrieves the first 5 records.

You can easily extend this example to retrieve a set of predefined records, n number of records each query, simulating a windowed querying client. Using a windowed query mechanisms overcomes the default limitation of only getting a maximum of 250 records per query.

#!/usr/bin/perl -w

#use SOAP::Lite ( +trace =>; all, maptype => {} );
use SOAP::Lite;

# basic auth using the ITIL user
sub SOAP::Transport::HTTP::Client::get_basic_credentials {
   return 'itil' => 'itil';
}

# specify the endpoint to connect
my $soap = SOAP::Lite
    -> proxy('https://<instance name>.service-now.com/incident.do?SOAP');

my $method = SOAP::Data->name('getRecords')
    ->attr({xmlns => 'http://www.service-now.com/'});

# get all incidents with a window of 5, starting at row 0, and less than row 5 (total of 5 records)
my @params = ( SOAP::Data->name(__first_row => '0') );
push(@params, SOAP::Data->name(__last_row => '5') );
# the last row number can also be taken as the 'limit' offset by the starting first row

# order by the number field
push(@params, SOAP::Data->name(__order_by => 'number') ); 

my $result = $soap->call($method => @params);
print_fault($result);
print_results($result);

sub print_results {
  my ($result) = @_;

  my %keyHash = %{ $result->body->{'getRecordsResponse'} };

  my $i = 0;
  my $size = @{$keyHash{'getRecordsResult'}};
  for ($i=0; $i<$size; $i++) {
    my %record = %{$keyHash{'getRecordsResult'}[$i]};
    print "------------------------------ $i ----------------------------\n";
    foreach my $kk (keys %record) {
# print only the number of the incident
      if ($kk eq "number") {
        print "$kk=$record{$kk}\n";
      }
    }
  }
}

sub print_fault {
  my ($result) = @_;

  if ($result->fault) {
    print "faultcode=" . $result->fault->{'faultcode'} . "\n";
    print "faultstring=" . $result->fault->{'faultstring'} . "\n";
    print "detail=" . $result->fault->{'detail'} . "\n";
  }
}
Feedback