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

icalderus
Kilo Contributor

Thanks Scott,

I appreciate it, Service Now is asking me to define a custom view and query that view on the backend.

As of now I dont have system admin rights to even create a view in Service Now, so I am waiting on that.

However I was able to rummage some PERL up last night that got me a conditional return on the cmdb_ci_outage table.

Scott, what would be the way to add a AND to this condition? In other words lets say I wanted to get results where sys_created_by='xyz' AND ( a second condition)..

this is what I have now :

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

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

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

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

# get incident by sys_id
my @params = ( SOAP::Data->name(sys_created_by => 'xyz123') );
my %keyHash = %{ $soap->call($method => @params)->body->{'getRecordsResponse'} };

# iterate through all fields and print them
#foreach my $k (keys %keyHash) {
# print "$k=$keyHash{$k}\n";
#}

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";
}
}


You can match conditions on specific fields by adding multiple parameters:



my @params = ( SOAP::Data->name(sys_created_by => 'xyz123') );
push(@params, SOAP::Data->name(state => '1') );
push(@params, SOAP::Data->name(description => 'Outage') );


The challenge with that method is you can't form complex queries easily (Condition 1 AND Condition 2 OR Condition 3). The above example is equivalent to the following encoded query:
sys_created_by=xyz123^state=1^description=Outage

If you use the __encoded_query element, you can pass more complex queries in the SOAP request. You can generate the encoded query string by filtering a list view in ServiceNow using the Filter List Breadcrumb and copying the encoded query from there. See: http://wiki.servicenow.com/index.php?title=Using_Filters_and_Breadcrumbs Make sure you right-click on the rightmost portion of the blue query link when you copy, otherwise you might miss some of the query conditions.

Instead of specifying individual field values like the example above, copy the encoded query from ServiceNow and paste it as the value to the __encoded_query parameter like this:


push(@params, SOAP::Data->name('__encoded_query' => 'active=true^business_duration<javascript:gs.getDurationDate('3 0:0:0')^ORcategory=software') );


icalderus
Kilo Contributor

Thanks yet again!

I was able to succefully add an additional constraint to the program to refine the results. I simply added one line of the push code from the first example you gave above.

As far as the second option goes , that sounds very interesting being able to conduct more complex queries.

In the end though I think we run into the same thing, and that is how to join the data once it is refined.

Once I am granted admin rights to our service now system I think I can define a view , create the join in the user interface and THEN query this view using very similar code such as in the examples we are passing just now. Once I can query that custom view I can refine that data even further using date/time stamp constraints. Ultimately I THINK that sounds workable.

What do you think Scott?

thanks again! AndI will keep toying around with the PERL in the meantime just for the heck of it, I havent touched it in years. Fun stuff.

I will most likely be back again with yet another question, thanks a ton in advance!