Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get/set Trello custom fields using the API?

Tags:

trello

api

I'm already in love with the Custom Fields feature in Trello. Is there a way to get and set custom fields via the API?

I tried using the get field API call to get a field (on a board with a custom field defined called "MyCustomField"):

curl "https://api.trello.com/1/cards/57c473503a5ef0b76fddd0e5/MyCustomField?key=${TRELLO_API_KEY}&token=${TRELLO_OAUTH_TOKEN}"

to no avail.

like image 250
mstringer Avatar asked Aug 30 '16 17:08

mstringer


People also ask

How do I find custom fields in Trello?

Show Menu > Filter Cards, then type the custom field value you want to search for. For example, a custom field called "Frobnosticator" that populates from a list. Typing the selected value ("Bob") from the list in the Filter search will filter the cards to show the ones where Frobnosticator is Bob.

How do I get an API in Trello?

You can get your API key by logging into Trello and visiting https://trello.com/app-key . Be sure to read and agree to Trello Developer Terms of Service. Your API key will be clearly labeled at the top of that page. Your API key should be a 32 character string comprised of random alphanumeric characters.

Is there a Trello API?

Trello provides a simple RESTful web API where each type of resource (e.g. a card, a board, or a member) has a URI that you can interact with. The Trello API documentation is available at https://developer.atlassian.com/cloud/trello.


2 Answers

The Custom Fields API from Trello is now officially available (announcement blog post here).

It allows users to manipulate both custom field items of boards and custom field item values on cards.

Custom Fields API documentation

Getting customFieldItems For Cards

Setting & Updating CustomFieldItems

like image 66
Employee Avatar answered Oct 15 '22 13:10

Employee


So I have a "sort of" answer to this. It requires some hackery on your part to make it work and there is more than a little manual upkeep as you add properties or values -- but it works.

I am doing this in powershell (I am NOT well versed in ps just yet and this my first really 'big' script that I have pulled together for it) since my intent is to use this with TFS Builds to automate moving some cards around and creating release notes. We are using custom fields to help us classify the card and note estimate/actual hours etc. I used this guys work as a basis for my own scripting. I am not including everything but you should be able to piece everything together.

I have left out everything with connecting to Trello and all that. I have a bunch of other functions for gettings lists, moving cards, adding comments etc. The ps module I linked above has a lot of that built in as well.

function Get-TrelloCardPluginData
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias('Id')]
        [string]$CardId

    )
    begin
    {
        $ErrorActionPreference = 'Stop'
    }
    process
    {
        try
        {
            $uri = "$baseUrl/cards/$CardId/pluginData?$($trelloConfig.String)"
            $result = Invoke-RestMethod -Uri $uri -Method GET
            return $result
        }
        catch
        {
            Write-Error $_.Exception.Message
        }
    }
}

You'll get data that looks like this:

@{id=582b5ec8df1572e572411513; idPlugin=56d5e249a98895a9797bebb9; scope=card; idModel=58263201749710ed3c706bef; value={"fields":{"ZIn76ljn-4yeYvz":2,"ZIn76ljn-c2yhZH":1}}; access=shared}

@{id=5834536fcff0525f26f9e53b; idPlugin=56d5e249a98895a9797bebb9; scope=card; idModel=567031ea6a01f722978b795d; value={"fields":{"ZIn76ljn-4yeYvz":4,"ZIn76ljn-c2yhZH":3}}; access=shared}

The fields collection is basically key/pair. The random characters correspond to the property and the value after that is what was set on the custom property. In this case it is an 'index' for the value in the dropdown. These two fields have a 'priority' (low, medium, high) and a 'classification' (Bug, Change Request, etc) for us. (We are using labels for something else).

So you'll have to create another fucntion where you can parse this data out. I am sure there are better ways to do it -- but this is what I have now:

function Get-TrelloCustomPropertyData($propertyData)
{
    $data = $propertyData.Replace('{"fields":{', '')
    $data = $data.Replace('}}', '')
    $data = $data.Replace('"', '')
    $sepone = ","
    $septwo = ":"
    $options = [System.StringSplitOptions]::RemoveEmptyEntries
    $obj = $data.Split($sepone, $options)

    $cardCustomFields = Get-TrelloCustomFieldObject

    foreach($pair in $obj)
    {
        $field = $pair.Split($septwo,$options)

        if (-Not [string]::IsNullOrWhiteSpace($field[0].Trim()))
        {
            switch($field[0].Trim())
            {
                'ZIn76ljn-4yeYvz' {
                    switch($field[1].Trim())
                    {
                        '1'{
                            $cardCustomFields.Priority = "Critical"
                        }
                        '2'{
                            $cardCustomFields.Priority = "High"
                        }
                        '3'{
                            $cardCustomFields.Priority = "Medium"
                        }
                        '4'{
                            $cardCustomFields.Priority = "Low"
                        }
                    }
                }
                'ZIn76ljn-c2yhZH' {
                    switch($field[1].Trim())
                    {
                        '1'{
                            $cardCustomFields.Classification = "Bug"
                        }
                        '2'{
                            $cardCustomFields.Classification = "Change Request"
                        }
                        '3'{
                            $cardCustomFields.Classification = "New Development"
                        }
                    }
                }
                'ZIn76ljn-uJyxzA'{$cardCustomFields.Estimated = $field[1].Trim()}
                'ZIn76ljn-AwYurD'{$cardCustomFields.Actual = $field[1].Trim()}
            }
        }
    }

    return $cardCustomFields
}

Get-TrelloCustomFieldObject is another ps function that I set up to build an object based on the properties I know that I have defined.

function Get-TrelloCustomFieldObject
{
    [CmdletBinding()]
    param()
    begin
    {
        $ErrorActionPreference = 'Stop'
    }
    process
    {
        $ccf = New-Object System.Object
        $ccf | Add-Member -type NoteProperty -name Priority -value "None"
        $ccf | Add-Member -type NoteProperty -name Classification -value "None"
        $ccf | Add-Member -type NoteProperty -name Estimated -value ""
        $ccf | Add-Member -type NoteProperty -name Actual -value ""
        return $ccf
    }
}
like image 39
bdwakefield Avatar answered Oct 15 '22 13:10

bdwakefield