Setting Catalog Item variables via ReST

Stephen Graham
Giga Expert

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.

1 ACCEPTED SOLUTION

Stephen Graham
Giga Expert

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   

}

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron

Hi Stephen,

which http method you are using and the endpoint url?

can you share the detail?

can you try to send body as below?

{
    "description":  "test description",
    "variables":  [
                  {
                      "test_field":  "test value"
                  }
                  ]
}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks Ankur - I've tried the above body but I get the same result; it updates the description, but not the test_field variable.

I'm using a PATCH method and the uri is: https://<instance>.service-now.com/api/now/table/sc_req_item/6720ebaadb5bb3004a582dcb0b961940?sysparm_display_value=all&sysparm_fields=variables.test_field,description

 

Hello Ankur,

 

I have two custom field in sc_request table, while creating Sr from order now api I need to pass the value of that variable, any idea how to achieve it

Chandra Shekha1
Giga Contributor

   "sysparm_quantity": "1",

   "sysparm_requested_for":"Cinde",

   "special_instructions":"Order it from API",

   "variables": { 

      "Is this a replacement for a lost or broken iPhone?": "no",

      "Monthly data allowance": "500MB",

     "Color":  "Space Gray",

     "Storage": "64GB"

    

   }

}

 

 

Above one is working fine in my instance.

 

API: 

https://dev87324.service-now.com/api/sn_sc/servicecatalog/items/d0b15e33d7033100a9ad1e173e24d49e/ord...