Skip to content

luisnuxx/ServiceNow_GlideRecord_API

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ServiceNow GlideRecord API

Using ServiceNow GlideRecord API in Python you can run GlideRecord commands right from your own computer and/or automate ServiceNow tasks.

Current capabilities are:

  • Query records using filters (sysparm_query)
  • Create new records in any given table on ServiceNow
  • Update existing records
  • Remove a record or a set of records all at once, using filters

###GlideRecord API for ServiceNow in Python ####Installation: #####Using PIP (Recommended) If your version of Python supports PIP package manager, installation is as simple as running this command:

pip install GlideRecord

If not, it is recommended that you install pip from the following URL, and retry the above command: http:https://pip.readthedocs.org/en/latest/installing.html

Also please note that in Linux systems you may need to run the above commands as root:

sudo pip install GlideRecord

#####Manual installation using Git If you have Git, you can install GlideRecord API by running these commands in order:



#####Need help? If you need any help regarding installation and/or usage of GlideRecord API, feel free to contact me via my email: [email protected]



####Examples:

#####Read data in a table, using filters: The below code snippet reads records stored in the 'incident' table that have status 'active' and are created by someone with a specific caller ID.

from GlideRecord import *

#Set which table to query
gr = GlideRecord("incident")

#Set the url to the server were the ServiceNow instance is
gr.set_server("https://sfustg.service-now.com/")

#Set user credentials to send REST requests
#Only one of the next two lines should be uncommented
#Uncomment the first one if: You will want to provide username/password from command line (recommended)
#Uncomment the second on if: You want to provide username/password in plain text (not recommended)
gr.get_credentials()
#gr.set_credentials("YOUR USERNAME GOES HERE", "YOUR PASSWORD GOES HERE")

#Add search filters
gr.addQuery("active", "true")
gr.addEncodedQuery('caller_id=76239f4b875a78006fa670406d434d39')

#Limits the number of results, to avoid making the server unnecessarily busy (The default is 100 results per query)
#if you want to retrieve all records, simple use gr.setRowCount("")
gr.setRowCount(50)

#Query ServiceNow
gr.query()

#How many rows returned?
print "Number of results: %s" % gr.getRowCount()

#While the cursor has not reached end of the results
while gr.next():
    print gr.getRow()
    print "\r\n"


#####Update existing records, using filters: The below code snippet updates exactly 150 of the records stored in the 'incident' table that have status 'active' and contact-type 'self-service'.

from GlideRecord import *

#Set which table to query
gr = GlideRecord("incident")

#Set the url to the server were the ServiceNow instance is
gr.set_server("https://sfustg.service-now.com/")

#Set user credentials to send REST requests
#One of the next two lines should be commented
#Comment the first one if: You will want to provide username/password from command line (recommended)
#Comment the second on if: You want to provide username/password in plain text (not recommended)
gr.get_credentials()
#gr.set_credentials("YOUR USERNAME GOES HERE", "YOUR PASSWORD GOES HERE")

#The following command indicates that only 150 of the results should be updated (the default is 100 per query)
#Also if you want to update all records, simple use gr.setRowCount("")
gr.setRowCount(150)

gr.addEncodedQuery('active=true^contact_type=self-service')

#Set the caller ID of these 100 records to 
gr.setValues("caller_id", "Behnam Azizi [bazizi]")


#####Delete a set of records returned by a filter: The below code snippet deletes all 'incident' records that status 'active' and have the specified caller ID.

from GlideRecord import *

#Set which table to query
gr = GlideRecord("incident")

#Set the url to the server were the ServiceNow instance is
gr.set_server("https://sfustg.service-now.com/")

#Set user credentials to send REST requests
#Only one of the next two lines should be uncommented
#Uncomment the first one if: You will want to provide username/password from command line (recommended)
#Uncomment the second on if: You want to provide username/password in plain text (not recommended)
gr.get_credentials()
#gr.set_credentials("YOUR USERNAME GOES HERE", "YOUR PASSWORD GOES HERE")

#Add search filters
gr.addQuery("active", "true")
gr.addEncodedQuery('caller_id=76239f4b875a78006fa670406d434d39')

#Note that by default, only up to 100 rows are affected, if you want to delete all records returned by the filter,
#simply use gr.setRowCount("")
gr.deleteMultiple()


#####Insert a new record into any given table: The below code snippet inserts a new record into the 'incident' table, using the specified values.

from GlideRecord import *

#Set which table to query
gr = GlideRecord("incident")

#Set the url to the server were the ServiceNow instance is
gr.set_server("https://sfustg.service-now.com/")

#Set user credentials to send REST requests
#Only one of the next two lines should be uncommented
#Uncomment the first one if: You will want to provide username/password from command line (recommended)
#Uncomment the second on if: You want to provide username/password in plain text (not recommended)
gr.get_credentials()
#gr.set_credentials("YOUR USERNAME GOES HERE", "YOUR PASSWORD GOES HERE")

record_info = {
    "caller_id" : "James Bond [jbond]",
    "u_phone_number" : "123456",
    "u_service" : "Quality Assurance",
    "short_description" : "Creating a record using GlideRecord API for Python",
    "description" : "ServiceNow GlideRecord API allows you to Create a record using Python", 
    "assignment_group" : "ServiceNow QA Team"
}

gr.insert(record_info)



####Unit Testing Some unit testing is added as well to ensure everything works as expected. To do the unit testing yourself, create a Python script similar to the following and run it:

#####Sample Unit Test Script:

from GlideRecord import *

#Set which table to query
gr = GlideRecord("incident")

#Set the url to the server were the ServiceNow instance is
gr.set_server("https://sfustg.service-now.com/")

gr.get_credentials()
gr.unittest()

If everything works as expected, the following output is shown in the command line:

Test 1: Testing insertion of exactly 3 new records PASSED

Test 2: Testing retrieval of exactly 3 records PASSED

Test 3: Testing deletion of exactly 1 record PASSED

Test 4: Testing update of exactly 2 records PASSED

Test 5: Clearing traces of the tests (removing records created) PASSED




####Available Member Functions:

Funcation name Description Example
set_server(server_name) Used to indicate which server to query gr.set_server("ubc.service-now.com")
set_credentials(username, password) Used to indicate which username/password is used to query ServiceNow gr.set_credentials("hpotter", "somepassword")
get_credentials() When this function is called, user credentials are read from command line gr.get_credentials()
addQuery(key, value) Adds a filter to the query gr.addQuery("active", "true")
addEncodedQuery(filter) Used to add a sysparm_query to the GlideRecord gr.addEncodedQuery("active=true")
query() queries the table query()
clearQuery() clears the filters that were added to the query gr.clearQuery()
getRow() returns an array containing the table row where cursor is pointing to gr.getRow()
getValue(column_name) returns the value of a column in the current row gr.getValue('u_phone_number')
getHeaders() returns the table headers (names of table columns) in an array gr.getHeaders()
getQuery() Returns the whole query string that is applied on the table print gr.getQuery()
getRowCount() Returns the number of results returned by a query print "Number of rows are %s" % gr.getRowCount()
next() returns 'true' if cursor has not reached the end of results while gr.next(): print gr.getRow()
hasNext() returns true if the cursor is not at the end of results gr.hasNext()
insert(json_data) used to insert a new record into a table gr.insert({"u_phone_number":"12345"})
deleteMultiple() used to delete all records that match the queries gr.deleteMultiple()
setValues(key, value) Sets the values of cells in a column gr.setValues('u_phone_number', '12345')
delete() Delete a single record. The syparm_sys_id of the record needs to be added to query beforehand gr.delete()
getValue(key) Get the value of a cell in the row where cursor is pointing gr.getValue('u_phone_number')
==============================================================================================


###License Information:

The MIT License (MIT)
Copyright (c) 2014 Behnam Azizi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

GlideRecord API for ServiceNow in Python

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Python 100.0%