Jump to content

Update custom field using Powershell and API


Recommended Posts

HI

We're trying to update a request using the API from a Powershell script, but nothing is happening (the code runs, and there are no errors, but the request doesn't get updated).

image.png.69af8e7e88eb60d4fdf8844c5a018f26.png

 

The requestId is updated as a global value (we've used this script to update the timeline too, and that works fine) but I can't see why the custom field isn't being updated.

Any thoughts please?

 

thanks

Link to comment
Share on other sites

Hi @Victor

 

Just tried that, and we're getting the error (below). Sorry it's a bit messy, but basically we're trying to call the 'Requests::update' function with the customFields parameters:

 

image.png.b7cfa9f6a3e21ed974398b4e97c3472f.png

 

Error message is:

 

Add-HB-Param : Cannot process argument transformation on parameter 'ParamAllowEmpty'. Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0. At line:120 char:32 + Add-HB-Param "customFields" "{"h_custom_a":"test"}" + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Add-HB-Param], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-HB-Param

Link to comment
Share on other sites

You've got double-quotes within double-quotes, which doesn't work. You'll need to use PowerShell's version of escaping the quotes, which I believe is to double-up like so:

Add-HB-Param "customFields" "{""h_custom_a"":""test""}"

but please check this in the PowerShell documentation because I'm no expert on that.

Link to comment
Share on other sites

  • 2 months later...

Here's a similar snippet of code, for those who find all the quotes and + signs confusing:
 

                $requestDetails = $requestOutput.Params.requestDetails | ConvertFrom-Json
                if ($requestDetails.h_custom_t -ne $TPEntity.Id) {
                    [string]$CustomT = $TPEntity.Id
                    $JsonCustomT = New-Object PSObject | Add-Member NoteProperty h_custom_t $CustomT -PassThru | ConvertTo-Json
                    Add-HB-Param -ParamName "requestId" -ParamValue $requestDetails.h_pk_reference
                    Add-HB-Param -ParamName "customFields" -ParamValue $JsonCustomT
                    $requestOutput = Invoke-HB-XMLMC "apps/com.hornbill.servicemanager/Requests" "update"
                    if ($requestOutput.Status -ne "ok") {
                        #
                        Write-Host "failed to set custom_t to $CustomT"
                    }
                }

What's going on here is that I'm using the h_custom_t field to store an external reference from Hornbill to the software development team's own SCRUM tracking system - the Hornbill Request Details are in $requestDetails and the SCRUM tracking story Id is in $TPEntity.Id

If they don't match (which they won't, because up till now there's only been a one-way link), then I copy the Id into  a new string $CustomT, and create a custom object from it, with a single, named field.  ConvertTo-Json does the heavy lifting.  Importantly, if the custom field itself contained quotes or other unusual characters, the ConvertTo-Json will take care of any necessary escape sequences or code conversions.

Barely a quote in sight!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...