Using ServiceNow PERL API to access cmdb_ci_outage table

icalderus
Kilo Contributor

Hello All!

Second post here, thanks in advance for the help.

I had previously written the forums asking for help with querying the cmdb_ci_outage table , piping those results to a flat file. I was able to do so successfully using the ServiceNow ODBC on windows xp. However, things have changed.

The scope of the project is now such that Windows is being tossed out altogether, and I have been asked to use the Service Now Perl API to accomplish the same thing, that is , access the cmdb_ci_outage table and pipe the output into a flat file.

As such I must say I am completely new to the Perl API. From what I gather there are some functions that can get incident info, enter incident info, etc. However my need is to specifically query the cmdb_ci_outage table using the Perl API.

Does ANYONE have any ideas or sample clues/code that may help? I must add I havent touched Perl in quite some time, however I am fairly decent with ksh code so I gather I can suffice.

thanks guys!

17 REPLIES 17

sshall
Mega Expert

The reference on the WIKI is pretty thorough. http://wiki.servicenow.com/index.php?title=Perl_Web_Services_Client_Examples Adapting their example for retrieving multiple records would look like this:



#!/usr/bin/perl -w
#use SOAP::Lite ( +trace => all, maptype => {} );
use SOAP::Lite;

sub SOAP::Transport::HTTP::Client::get_basic_credentials {
return 'itil' => 'itil';
}

my $soap = SOAP::Lite
-> proxy('http://demo.service-now.com/cmdb_ci_outage.do?SOAP');

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

# order results by sys_id
my @params = ( SOAP::Data->name(__order_by => 'sys_id') );

my %keyHash = %{ $soap->call($method => @params)->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 "$kk=$record{$kk}\n";
}
}


EDIT to add: You will also want to look at TEXT::CSV or something similar for writing the results to a .csv instead of printing them to the screen.


icalderus
Kilo Contributor

thanks a ton. i will be checking this out shortly.

will update with more questions if need be. thanks!


icalderus
Kilo Contributor

hello there,

taking a look at this i am not sure how i would run a select against the cmbd_ci_outage table specifically.

any clues or ideas?

appreciated


icalderus
Kilo Contributor

hey there,

i got the script to run on my environment. i was lame and didnt pay attention to my site specific URL and username and password for the get credentials. i can now get output.

now to nag even further, how would i insert a specific SQL against this table? for example , lets say i want to see the following field results:

cmdb_ci_outage.dv_task_number,
cmdb_ci_outage.begin,
cmdb_ci_outage.end,
cmdb_ci_outage.message,
cmdb_ci_outage.short_description,

how would i incorporate that into this script? is there a specific function we can use to achieve these desired results? i havent touched perl in years, my apologies and thanks for your patience in advance.