JSON Body example Changing Asset State of Asset via API

I can’t seem to figure this out. Admittedly I am doing this via powershell and the Invoke-RestMethod but I’ve managed to successfully change the Asset Name with no issues the same way. This is the PowerShell script I’m using:

$APIKey = 'MyApiKey'
$EncodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $APIKey, $null)))
$HTTPHeaders = @{}
$HTTPHeaders.Add('Authorization', ('Basic {0}' -f $EncodedCredentials))

$URL = 'https://MyDomain.freshservice.com/api/v2/assets/1347'

$JSON = Get-Content C:\Users\Path\To\JSONFile.json -Raw

$Params = @{

Method      = 'Put'
Uri         = $URL
Headers     = $HTTPHeaders
ContentType = 'application/json'
Body		= $JSON
}
Invoke-RestMethod @Params 

If I set the contents of my Json File as the below, it successfulyl changes the name:

{
    "name":  "MKC-02926"
}

If however I set the contents of the file as the below, it doesn’t change the asset_state.

{
    
	"type_fields": {
                "asset_state": "Retired"
            }
}

I tried both asset_state and asset_state_25 since I noticed the documentations seems to switch between both. However, both return a “The remote server returned an error: (400) Bad Request” message.

Anyone able to point me in the direction of what exactly I’m doing wrong?

I don’t think it is possible to update asset_state using the Update Asset endpoint in the Freshservice REST API. From the API docs, it looks like only read operations are possible on the fields of an asset type.

It’s funny, I’ve spent literal weeks trying to figure this out and I come upon the solution less than half an hour after posting it here.

It’s not asset_state and it’s not asset_state_25. I’m going to go out a limb here and say it’s unique for everyone.

First, find an asset that is the same “Type” as the asset you want to retire. Then run the below:

$APIKey = 'YourAPIKey'
$EncodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $APIKey, $null)))
$Header = @{}
$Header.Add('Authorization', ('Basic {0}' -f $EncodedCredentials))


invoke-RestMethod -URI 'https://YourDomain.freshservice.com/api/v2/assets/1347' -Headers $Header -ContentType "application/json" -Method Get | select asset -expandproperty asset

Look in the “Asset” section for something that starts with “asset_type_” followed by some numbers. In our example, it will be asset_type_27548748258

Then run the below command:

invoke-RestMethod -URI 'https://domain.freshservice.com/api/v2/asset_types/26000345210/fields' -Headers $Header -ContentType "application/json" -Method Get | select asset_type_fields -expandproperty asset_type_fields | select fields -expandproperty fields | select name -expandproperty name | Where-Object -Property Name -like '*asset_state_*'

You’ll get a result sthat starts with asset_state_ followed by some numbers.

Let’s say the one we find is asset_state_54827395617

Now, to retire an asset, you can do the below:

$APIKey = 'YourAPIKey'
$EncodedCredentials = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $APIKey, $null)))
$Header = @{}
$Header.Add('Authorization', ('Basic {0}' -f $EncodedCredentials))

$parameters = @{

        asset = @{                 
            type_fields = @{
                asset_state_54827395617 = "Retired"
            }
        }
    }
	
invoke-RestMethod -URI 'https://YourDomain.freshservice.com/api/v2/assets/idoftheassethere' -Headers $Header -ContentType "application/json" -Method Put -Body (ConvertTo-Json $parameters)
1 Like

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.