Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

get history of changes for an incident field (Assigned To)

Mario Landa
Kilo Explorer

Hello guys,

I need the history of changes for the Assigned to Field, I am working in the sandbox and enabling auditing for the incident table as suggested here:
https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/administer/security/task/t_EnableAuditingForATable.html

Yields nothing when I make the API call. Querying sys_dictionary , sys_audit, sys_history_line or sys_history does not work either. I dont know what to do, im using python. Here is one example, the requests that come back are empty or invalid, how else can I do this? Is there a way to get the " Field changes " table? thank you!

# Set the request parameters
    url = env + 'api/now/table/sys_audit/' + sys_id
    
    # Set proper headers
    headers = {"Content-Type":"application/json","Accept":"application/json"}
    
    # Do the HTTP request
    response = requests.get(url, auth=(user, pwd), headers=headers ,data="{\"fieldname\":\"assigned_to\"}")
    
    # Check for HTTP codes other than 200
    if response.status_code != 200: 
        print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
        exit()
    
    # Decode the JSON response into a dictionary and use the data
    data = response.json()
    return data
1 ACCEPTED SOLUTION

Hi Mario,

Could you set my response to correct if this query retrieved the correct details for you?

Cheers,

Steve

View solution in original post

9 REPLIES 9

this does not work unfortunately, I dont know what else to do.

Can you remove the below marked in bold and try

 

response = requests.get(url, auth=(user, pwd), headers=headers)


Please mark this response as correct or helpful if it assisted you with your question.

Steve Socha
Tera Guru

Hi Mario,

I believe you're looking for the sys_history_line, but you'll have to do some dot-chasing to access the correct attribute. Try performing a table query with sysparm_query=set.id=${incident_id}

 

Using the Table API, this would come out to a GET call to

https://INSTANCE_NAME.service-now.com/api/now/table/sys_history_line?sysparm_query=set.id=05f7875cdb982b00f2e4d2984b961966

Possibly with an addition on the query to only look for assigned_to entries, which could be arranged by appending ^field=assigned_to to the end of the URL

Hi Mario,

Could you set my response to correct if this query retrieved the correct details for you?

Cheers,

Steve

The response returns a result object and then you can check if the field modified is "assigned_to" and parse it like this:

for ticket in getServiceAssignedToHistory(sys_id goes here)["result"]:
    if(ticket["field"] == 'assigned_to'):
        print("old user :")
        print(ticket["old"])
        print("new user")
        print(ticket["new"])

Thank you very much! It works now!