- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2019 10:41 AM
I have a requirement to update some variables on a request item via ReST after the item has been logged by an end-user.
I can successfully set the standard fields, but not the variables. When I pass the below body into my PATCH command, the description gets set correctly, but the variable does not:
{
"description": "test description",
"variables": {
"test_field": "test value"
}
}
The 'test_field' is a Single Line Text field in ServiceNow.
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2019 10:47 AM
In case anyone else has the same issue, here's the PowerShell script I used to update two variables (Test Field and Test Field2) on an existing RITM:
$instance = "https://<instance>.service-now.com/api/"
$user = ""
$pass = ""
$sys_id = "86d6aa471b233300de6ffc88cc4bcba6" #The sys_id of the RITM
$table = "sc_item_option_mtom" #this table holds the relationship between an RITM and its variables
#Set TLS to 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
# Specify endpoint uri
$uri = "$instance/now/table/$table"+"?sysparm_query=request_item=$sys_id" #queries for the sys_id of the RITM in the sc_item_option_mtom table
# Specify HTTP method
$method = "GET" #We are just doing a GET because at this point we just want to retrieve the link records, which we can use later to populate the variables in the sc_item_option table
# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -UseBasicParsing
$response = $response | ConvertFrom-Json #Convert response to a PSObject
$sc_item_option = $response.result.sc_item_option.value #This is the value of the sc_item_option field in the sc_item_option_mtom table. This is a reference field so it will be a sys id. If there are multiple variables, then a list would be returned
foreach ($id in $sc_item_option) #loop through each variable
{
$table2 = 'sc_item_option'
$uri2 = "$instance/now/table/$table2/$id"+"?sysparm_display_value=all"
$method2 = 'PATCH'
#perform a GET to find out what the variable's display value is (i.e. which question it relates to)
$response2 = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri2 -UseBasicParsing
$response2 = $response2 | ConvertFrom-Json
$variableName = $response2.result.item_option_new.display_value #This is the display name of the question in ServiceNow
$variableValue = $response2.result.value.value #This is current value (answer) of the question in ServiceNow
$method2 = 'PATCH' #PATCH to update the field
#Instantiate empty body object to populate later
$body = @{
}
#Populate the fields with different values depending on the question. The $variableName must exactly match the text of the question on the catalog item in ServiceNow
If ($variableName -eq 'Test Field')
{
$body.add('value', 'testing field 1')
}
elseif ($variableName -eq 'Test Field2')
{
$body.clear() #empty the body
$body.add('value', 'testing field 2')
}
$bodyJson = $body | ConvertTo-Json
$response2 = Invoke-WebRequest -Headers $headers -Method $method2 -Uri $uri2 -Body $bodyJson -UseBasicParsing
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 02:30 AM
Thanks Chandra,
I think that only works for creating new orders - do you know how to get it to work for updating existing request items?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2019 10:47 AM
In case anyone else has the same issue, here's the PowerShell script I used to update two variables (Test Field and Test Field2) on an existing RITM:
$instance = "https://<instance>.service-now.com/api/"
$user = ""
$pass = ""
$sys_id = "86d6aa471b233300de6ffc88cc4bcba6" #The sys_id of the RITM
$table = "sc_item_option_mtom" #this table holds the relationship between an RITM and its variables
#Set TLS to 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
# Specify endpoint uri
$uri = "$instance/now/table/$table"+"?sysparm_query=request_item=$sys_id" #queries for the sys_id of the RITM in the sc_item_option_mtom table
# Specify HTTP method
$method = "GET" #We are just doing a GET because at this point we just want to retrieve the link records, which we can use later to populate the variables in the sc_item_option table
# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -UseBasicParsing
$response = $response | ConvertFrom-Json #Convert response to a PSObject
$sc_item_option = $response.result.sc_item_option.value #This is the value of the sc_item_option field in the sc_item_option_mtom table. This is a reference field so it will be a sys id. If there are multiple variables, then a list would be returned
foreach ($id in $sc_item_option) #loop through each variable
{
$table2 = 'sc_item_option'
$uri2 = "$instance/now/table/$table2/$id"+"?sysparm_display_value=all"
$method2 = 'PATCH'
#perform a GET to find out what the variable's display value is (i.e. which question it relates to)
$response2 = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri2 -UseBasicParsing
$response2 = $response2 | ConvertFrom-Json
$variableName = $response2.result.item_option_new.display_value #This is the display name of the question in ServiceNow
$variableValue = $response2.result.value.value #This is current value (answer) of the question in ServiceNow
$method2 = 'PATCH' #PATCH to update the field
#Instantiate empty body object to populate later
$body = @{
}
#Populate the fields with different values depending on the question. The $variableName must exactly match the text of the question on the catalog item in ServiceNow
If ($variableName -eq 'Test Field')
{
$body.add('value', 'testing field 1')
}
elseif ($variableName -eq 'Test Field2')
{
$body.clear() #empty the body
$body.add('value', 'testing field 2')
}
$bodyJson = $body | ConvertTo-Json
$response2 = Invoke-WebRequest -Headers $headers -Method $method2 -Uri $uri2 -Body $bodyJson -UseBasicParsing
}