I recently start using pysnow-module for automating my work at Servicedesk. My problem is that i don't know how to create Requested Items.
I have managed to create new incidents and new requests, also update them. I have also succeeded in modifying Requested Items, but i'm stuck at not knowing how to turn Requests into Requested Items.
I've tried following pieces of code, all starting with this
s = pysnow.Client(instance='Secret domain', user='Secret-user', password='Secret')Then continuing with following tries:
def new_ritm(): result = s.insert(table='sc_req_item', payload={'field1': 'value1', 'field2': 'value2'})
print(result['number'])
luo_uusi_ritm()
File "XXXXXXXXXXXXXXXXXX", line 51, in <module>luo_uusi_ritm()
File"XXXXXXXXXXXXXXXXXX", line 47, in new_ritmresult = s.insert(table='sc_req_item', payload={'field1': 'value1', 'field2': 'value2'})
File "XXXXXXXXXXXXXXXXXX", line 90, in insert return r.insert(payload) File "XXXXXXXXXXXXXXXXXX", line 108, in insert return self._get_content(response)
File "XXXXXXXXXXXXXXXXXX", line 290, in _get_contentreturn content_json['result']
KeyError: 'result'Also tried modifying REQ, insert new values:
result = s.insert(table='sc_req_item', payload={'sc_req_item.request':
'REQ0994143'})But get an error:
"File "xxxxxxxxxxxxx, line 290, in _get_content
return content_json['result']
KeyError: 'result'EDIT: Suggestion 1: I couldn't add the
"request_params={'sysparm_display_value': 'true'})" , because it gives me an "Unexpected argument" error
I entered the following code:
s = pysnow.Client(instance=XXXX,user=XXXXX,password=XXXXXX) try: result = s.insert(table='sc_req_item', payload={'field1': 'value1', 'field2': 'value2'}) except pysnow.UnexpectedResponse as e: print("%s, details: %s" % (e.error_summary, e.error_details))Which gives me an error:
Traceback (most recent call last):
File "XXXXXXXXXXXX", line 63, in stack_experiment()
File "XXXXXXXXX", line 59, in stack_experiment result = s.insert(table='sc_req_item', payload={'field1': 'value1','field2': 'value2'})
File "XXXXXXXXXXXXX", line 90, in insert return r.insert(payload)
File "XXXXXXXXXXXXX", line 108, in insert return self._get_content(response)
File "XXXXXXXXXXX", line 290, in _get_content return content_json['result']
KeyError: 'result'
Suggestion 2:
Maybe you are confused using Client.insert instead of Request.insert
I am confused about it. Should i change the starting code "s = pysnow.Client(etc."?
I went to read the docs one more time and i've understood for new requests, i should use pysnow.Request, so i tried once more with:
r = pysnow.Request(instance='Secret domain', user='Secret-user', password='Secret', method='POST', table='sc_req_item')
try: result = r.insert(payload={'field1': 'value1', 'field2': 'value2'})
except pysnow.UnexpectedResponse as e: print("%s, details: %s" % (e.error_summary, e.error_details))And i got an error:
Traceback (most recent call last): File "xxxx", line 7, in <module> r = pysnow.Request(instance='xxxx', user='xxxx', password='xxxxx', method='POST', table='sc_req_item') File "xxxxx", line 29, in __init__ self.default_payload = kwargs.pop('default_payload')
KeyError: 'default_payload' 1 Answer
Comment: My problem is with Requested Items.
Try the following, and [Edit] your Question to adding the Output of
print(...or anyTraceback:try: result = s.insert(table='sc_req_item', payload={'field1': 'value1', 'field2': 'value2'}) except pysnow.UnexpectedResponse as e: print("%s, details: %s" % (e.error_summary, e.error_details))Maybe you are confused using
Client.insertinstead ofRequest.insert?pysnow.Request — Creates a new request object
insert(payload)Inserts a new record with the payload passed as an argument
Parameters: payload – The record to create (dict)
Returns: Created recordSetting request parameters
The request_params dict argument can be used to set request parameters. This example returns names from fields with linked tables, instead of the standard URL for ServiceNow dot walking.import pysnow # Create new client with SN request parameters sn = pysnow.Client(instance=instance, user=username, password=password, request_params={'sysparm_display_value': 'true'})
Try this example from the Documentation:
1pysnow: Getting a single record
See the pysnow.Request.get_one() documentation for more details.
import pysnow # Create client object s = pysnow.Client(instance='myinstance', user='myusername', password='mypassword') # Query for 'INC01234' on table 'incident' r = s.query(table='incident', query={'number': 'INC01234'}) # Fetch one record and filter out everything but 'number' and 'sys_id' res = r.get_one(fields=['number', 'sys_id']) # Print out the result print(res)