JSON posting to update Trello Custom Field from VBA

353 Views Asked by At

I'm using VBA in Excel with the amazing VBA-Tools by Tim Hall and I'm able to do pretty much everything trello related, create card, list, comments, etc

What's been frustating to me the last couple of days is that I cannot put a value in the custom field of a card, I tried everything I can think of

This is the code

 Sub atualizaCustomField(listaNome As String, cardNome As String, idCF As String, valor As String)
  Dim Client As New WebClient
  Client.BaseUrl = "https://api.trello.com/1/"
  Dim Request As New WebRequest

  Request.ContentType = "application/json"

  Request.Method = WebMethod.HttpPut
  Request.Resource = "cards/{idCard}/customField/{idCustomField}/item"
  Request.AddUrlSegment "idCard", pegaIDCard(listaNome, cardNome)
  Request.AddUrlSegment "idCustomField", idCF
  
  Request.AddQuerystringParam "key", ApplicationKey
  Request.AddQuerystringParam "token", UserToken
  Debug.Print Request.FormattedResource
   
  Dim Response As WebResponse
  'Set Response = Client.Execute(Request)
  Set Response = Client.PostJson(Client.BaseUrl & Request.FormattedResource, valor)
  Debug.Print valor
  Debug.Print Response.StatusCode & ": " & Response.Content
End Sub

What's different about this is that you have to post a JSON and not simply make the request like everything else, must match this example (from trello developer site):

{
  "value": {
    "text": "<string>"
  }
}

in my code the variable "valor" holds this string.

1

There are 1 best solutions below

1
On

I know this was 9 months ago, but I ran into the same issue and found a solution that may be useful to others.

As the OP said, the "value" field needs to have json formatting and be placed in the request body. This is achieved by passing a Dictionary type to the .AddBodyParameter method.

Dim body as Object
Set body = New Dictionary
body.Add "text", "<string>"
Request.AddBodyParameter "value", body