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

SusanWinKY
Kilo Sage

Hi Dustdreams27,

 

We're having the same issue.  The OOB business rule, Set Closure Fields (Task Table), does not seem to work when the task is closed by REST API.

 

To implement a workaround fix for this, we created a new business rule that runs against the sc_req_item table to populate these values.  It is set to run before insert/update and has this configuration.

Condition:

current.active.changesTo(false)

Script:

if (current.closed_by.nil())
     current.closed_by = gs.getUserID();
if (current.closed_at.nil())
     current.closed_at = gs.nowDateTime();

 

Note:  We're still testing this so I can't confirm whether or not it's going to work.


Susan Williams, Lexmark

We just completed testing and, unfortunately, this new BR did not work.  We'll most likely end up contacting HI support for assistance.


Susan Williams, Lexmark

S_C Pressley
Kilo Guru

Have you tried creating a Flow logic for this? You can auto update fields with conditions and no scripting