Update Task to Closed Complete via REST API and 'Closed' and 'Closed By' Fields Not Updating

Dustdreams27
Kilo Contributor

EDIT TO ORIGINAL POST: I have a strange issue where I am updating a task's state via the REST API to closed complete and it is successfully updating the task. I verify that it added a work note and the state is reflecting closed complete. However, when I search under closed tasks, the task is not present. I've also verified that the RITM and Request both reflect closed as well.

HERE IS THE KEY: I noticed that the 'Closed' and 'Closed_By' fields are not being automatically updated and are left blank. Which is why I am unable to locate the tasks in any closed task reports. When I first attempted to update some tasks I was only defining the 'Work_Notes' and 'State' fields be updated from the REST API. E.g.

# Specify Request Body
$Body1 = @{
'work_notes' = 'Closed via REST API'
'state' = '3'
}

But after I figured out the tasks were not reflecting closed in all of my reports due to the 'Closed' and 'Closed_by' fields being left blank, I tried to send those values down from REST API as well. E.g.

# Specify Request Body
$Body1 = @{
'work_notes' = 'Closed via REST API'
'closed_at' = '04-07-2018 03:19:46 PM'
'closed_by' = '6a207866130e16000080b027d144b0c6'
'state' = '3'
}

But that did not work and the 'Closed' and 'Closed By' fields still remain blank and thus are not reporting correctly. Is there a Business Rule that when you close a task in the UI it automatically applies the current authenticated user to 'Closed By' and the current date-time to 'Closed'? Perhaps that business rule does not apply when updating a task's state from the REST API?

 

Here is the entire Powershell script that I am using to update tasks through the ServiceNow REST API.

### Setup ServiceNow Auth and Headers
$SNowUser = "myusername"
$SNowPass = Cat "C:\ServiceNow\SecureString\Pass.txt" | ConvertTo-Securestring
$SNowCreds = New-Object —TypeName System.Management.Automation.PSCredential —ArgumentList $SNowUser, $SNowPass
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add('Accept','application/json')
$Headers.Add('Content-Type','application/json')

### Update SN Task State and Add Work-Notes
$SysID = "885f6824dbdd0380c1b87b6b8c961900"
# ServiceNow URI
$Uri1 = "https://myinstance.service-now.com/api/now/table/sc_task/$SysID"

# Specify HTTP Method
$Method1 = "PUT"

# Specify Request Body
$Body1 = @{
'work_note' = 'Test'
'state' = '3'
}

# Convert Body to JSON
$BodyJson1 = $Body1 | ConvertTo-Json

# Send HTTP Request ###
$Response1 = Invoke-WebRequest -Credential $SNowCreds -Headers $Headers -Method $Method1 -Uri $Uri1 -Body $BodyJson1 -ContentType "application/json" -UseBasicParsing
$JsonPUT1 = $Response1.Content | ConvertFrom-Json
$PUTObject1 = $JsonPUT1.result

 

7 REPLIES 7

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi,

 

In out of the box reports they use actual end field as a filter and in your case this field would be blank. Please check value for this field.

 

Hope this helps.

 

Regards

Ujjawal

Thank you for the suggestion, but I am not using out of the box reports. I have many different custom reports and filters where I am unable to locate the tasks I have closed from the REST API. For example, I have updated at least 10 tasks to closed complete in the past couple of hours and have a filter saved that is VERY simple. It searches the Task Table for task type = catalog tasks. I sort the results by Closed and the most recent results are from a couple of days ago. The closed tasks are no where to be found. However, if I expand the filter to task type = requested item, the related requested items from the tasks I closed are present.

Can you please share the setup of the report.

It is exactly how I explained in my previous reply. I am viewing the task table and the only filter I apply is (Task Type is Catalog Task) and then sorting by the Closed date/time. This filter/report should show ALL tasks by the most recent closed. See screenshot below. The below tasks are tasks that I manually closed out through the ServiceNow UI. They appear correctly, but the tasks closed through the REST API are not displaying.

 

find_real_file.png